【POJ】3017 Cut the Sequence
2012-07-13 20:50
162 查看
#include<cstdio> #define MAXN 100010 #define MIN(a,b) ((a)<(b)?(a):(b)) typedef __int64 LL; LL limit,a[MAXN],dp[MAXN]; int n,Q[MAXN]; LL DoIt() { LL sum; int i,j,k,front,rear; for(i=1;i<=n;i++) { if(a[i]>limit) return -1; } sum=dp[0]=0; front=0; rear=-1; for(i=k=1;i<=n;i++) { sum+=a[i]; for(;sum>limit;k++) sum-=a[k]; for(;front<=rear&&Q[front]<k;front++); for(;front<=rear&&a[Q[rear]]<=a[i];rear--); Q[++rear]=i; dp[i]=dp[k-1]+a[Q[front]]; for(j=front;j<rear;j++) dp[i]=MIN(dp[i],dp[Q[j]]+a[Q[j+1]]); } return dp ; } int main() { int i; while(~scanf("%d%I64d",&n,&limit)) { for(i=1;i<=n;i++) scanf("%I64d",&a[i]); printf("%I64d\n",DoIt()); } return 0; }
相关文章推荐
- POJ 3017 Cut the Sequence 单调队列
- poj 3017 Cut the Sequence
- poj 3017 cut the sequence
- POJ 3017 Cut the Sequence(单调队列+set)
- POJ 3017|Cut the Sequence|动态规划|单调队列
- POJ 3017 Cut the Sequence 【DP+单调队列优化+平衡树】
- poj 3017 Cut the Sequence(DP+单调队列)
- poj 3017 Cut the Sequence(DP+单调队列+set)
- poj 3017 Cut the Sequence dp
- POJ-3017 Cut the Sequence DP+单调队列+堆
- POJ 3017 Cut the Sequence(dp+单调队列)
- POJ 3017 Cut the Sequence (单调队列优化DP)
- poj 3017 Cut the Sequence(单调队列优化 )
- POJ 3017 Cut the Sequence
- POJ-3017 Cut the Sequence(DP单调队列优化 + 平衡树)
- Poj 3017 Cut the Sequence (DP,单调队列优化,数据结构优化)
- poj 3017 Cut the Sequence(dp单调队列优化)
- POJ_3017 Cut the Sequence 单调队列+dp+BST
- poj 3017 Cut the Sequence dp+单调队列优化
- POJ - 3017 Cut the Sequence(单调队列+dp)