Original Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=1300
Test instructions
The topic is too long. See for yourself.
Exercises
Read the topic, you will find this is a silly DP problem, slope optimization is good
Code:
#include <iostream>#include<cstring>#include<vector>#include<cstdio>#defineMax_n 500using namespaceStd;typedefLong Longll;ll n;ll sum[max_n];ll dp[max_n];ll a[max_n];ll p[max_n];DoubleYintt) { returndp[t];}DoubleXintt) { returnsum[t];}DoubleSlope (intUintv) {return(Y (U)-y (v))/(X (U)-X (v));}intQue[max_n];intT;intMain () {Cin.sync_with_stdio (false); CIN>>u; while(t--) {cin>>N; Memset (DP,0,sizeof(DP)); memset (que,0,sizeof(que)); memset (SUM,0,sizeof(sum)); for(inti =1; I <= N; i++) {cin>> A[i] >>P[i]; Sum[i]= Sum[i-1] +A[i]; } intFront =0, rear =0; Que[rear++] =0; for(inti =1; I <= N; i++) { while(Rear-front >1&& Slope (Que[front], Que[front +1]) <= P[i]) front++; intj =Que[front]; Dp[i]= Dp[j] + (Sum[i]-sum[j] +Ten) *P[i]; while(Rear-front >1&& Slope (Que[rear-1], Que[rear-2]) >= Slope (Que[rear-1], i)) rear--; Que[rear++] =i; } cout<< Dp[n] <<Endl; } return 0;}
Hdoj 1300 Pearls Slope optimization DP