标签:col color namespace put cstring play else stdin inf
朴素做法暴力DP,O(nk)过不去。。。
1 #include <cmath> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #define N1 2005 6 #define ll long long 7 using namespace std; 8 9 int n,p; 10 int l[N1],r[N1],a[N1]; 11 ll f[N1][N1]; 12 ll linf=0x3f3f3f3f3f3fll; 13 14 int main() 15 { 16 freopen("a.txt","r",stdin); 17 scanf("%d%d",&n,&p); 18 for(int i=1;i<=n;i++) scanf("%d%d%d",&l[i],&r[i],&a[i]); 19 for(int i=0;i<=n;i++) for(int k=0;k<=p;k++) f[i][k]=linf; 20 f[0][p]=0; l[n+1]=r[n]+1; 21 for(int i=1;i<=n;i++) 22 { 23 for(int k=0;k<p;k++) 24 { 25 if(a[i]%p<=k) 26 { 27 if(a[i]/p<=r[i]-l[i]) 28 { 29 f[i][k-a[i]%p]=f[i-1][k]+a[i]/p; 30 if(a[i]/p+1<=l[i+1]-l[i]) f[i][p]=min(f[i][p],f[i-1][k]+a[i]/p+1); 31 } 32 } 33 if(a[i]%p>k) 34 { 35 if(a[i]/p+1<=r[i]-l[i]) 36 { 37 f[i][k+p-a[i]%p]=f[i-1][k]+a[i]/p+1; 38 if(a[i]/p+2<=l[i+1]-l[i]) f[i][p]=min(f[i][p],f[i-1][k]+a[i]/p+2); 39 } 40 } 41 } 42 if(a[i]%p) 43 { 44 if(a[i]/p<=r[i]-l[i]) 45 { 46 f[i][p-a[i]%p]=f[i-1][p]+a[i]/p; 47 if(a[i]/p+1<=l[i+1]-l[i]) f[i][p]=min(f[i][p],f[i-1][p]+a[i]/p+1); 48 } 49 }else{ 50 if(a[i]/p-1<=r[i]-l[i]) 51 { 52 f[i][0]=f[i-1][p]+a[i]/p-1; 53 if(a[i]/p<=l[i+1]-l[i]) f[i][p]=min(f[i][p],f[i-1][p]+a[i]/p); 54 } 55 } 56 57 } 58 ll ans=linf; 59 for(int k=0;k<=p;k++) ans=min(ans,f[n][k]*p+p-k); 60 if(ans==linf) puts("-1"); else printf("%lld\n",ans); 61 return 0; 62 }
标签:col color namespace put cstring play else stdin inf
原文地址:https://www.cnblogs.com/guapisolo/p/13834193.html