http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4778
优先队列优化下的DP,dp[i]记录的是吃到i天的最小花费,最后dp[N]即为答案。
View Code
const int MM = 111111; #define debug puts("wrong") typedef long long int64; //typedef __int64 int64; int64 N, cnt; struct Info{int64 x,y;}p[MM]; struct Tpoint{ int64 val,dead; //Tpoint(int v,int d):val(v),dead(d) {} bool friend operator<(Tpoint x,Tpoint y) { if(x.val!=y.val) return x.val>y.val; else return x.dead>y.dead; } }; priority_queue<Tpoint>que; void get_data() { int64 i,j,k; for(i=1;i<=N;i++) scanf("%lld",&p[i].x); for(i=1;i<=N;i++) scanf("%lld",&p[i].y); } int64 dp[MM]; void solve() { int64 i,j,k,tmp, mx; Tpoint tt, t1, t2; while(!que.empty()) que.pop(); t1.val=p[1].x, t1.dead=p[1].y; dp[1]=p[1].x; que.push(t1); for(i=2;i<=N;i++) { t1.val=dp[i-1]+p[i].x; t1.dead=i+p[i].y-1; que.push(t1); while(!que.empty() && que.top().dead<i) que.pop(); dp[i]=que.top().val; } printf("%lld\n", dp[N]); } int main() { while(scanf("%lld",&N)!=EOF) get_data(),solve(); return 0; }