
题目地址
题解
一开始很智障的每一步都求了一个背包…
实际上只要求一次,然后$O(1)$回答询问。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <cstdlib> #include <algorithm> #include <cstring> #include <cctype> #define INF 2000000000 using namespace std; typedef long long ll; int n,b,v[1005],s[100005],a[1005]={0}; int (){ int x=0;char c=getchar(); while(c<'0'||c>'9')c=getchar(); while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); return x; } void init(){ n=read(),b=read(); for(int i=1;i<=b;i++)v[i]=read(); for(int i=1;i<=n;i++)a[i]=read(); } void solve(){ int ans=0,cur=a[1]; memset(s,0x7f,sizeof(s)); s[0]=0; for(int j=1;j<=b;j++){ int *A=s,*B=s+v[j]; for(;B-s<=100000;A++,B++){ *B=min(*B,*A+1); } } for(int i=1;i<=n;i++){ cur=a[i]-a[i-1]; if(a[i-1])cur++; if(s[cur]==0x7f7f7f7f){ ans=-1;break; }else ans+=s[cur]; } printf("%dn",ans); } int main(){ init(); solve(); return 0; }
|
近期评论