题意:有n个宝藏,在x轴上,每个宝藏在某个时间会消失,问最少吃完所有宝藏的时间是多少,否则输出no solution
分析:区间DP,f[i][j][01]代表i到j区间内的全部吃完,停留在左/右端,不过这个时间空间也是水,不知道怎么出的题目。。
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> int n,f[10005][10005][2],p[10005],t[10005]; int read(){ char ch=getchar();int f=1,t=0; while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();} while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();} return t*f; } int main(){ while (scanf("%d",&n)!=EOF){ for (int i=1;i<=n;i++){ p[i]=read();
t[i]=read();
}for (int i=n-1;i>=1;i--) for (int j=i+1;j<=n;j++){ f[i][j][0]=std::min(f[i+1][j][0]+p[i+1]-p[i],f[i+1][j][1]+p[j]-p[i]); f[i][j][1]=std::min(f[i][j-1][1]+p[j]-p[j-1],f[i][j-1][0]+p[j]-p[i]); if (f[i][j][0]>=t[i]) f[i][j][0]=100000000; if (f[i][j][1]>=t[j]) f[i][j][1]=100000000; } int x=std::min(f[1][n][0],f[1][n][1]); if (x<100000000) printf("%d ",x); else printf("No solution "); memset(f,0,sizeof (f)); } }