Watermelon full of water Time limit: 3 Seconds Memory Limit: 65536 KB
watermelon is very popular in the hot Summer. Students in Zju-icpc Team also love watermelon very much and they hope that they can has watermelon to eat every day Duri ng the summer vacation. Suppose there is n days and every day they can buy only one watermelon. The price of watermelon is different in each day. Besides, sometimes the watermelon they choose to buy May is very big, which means if they buy this watermelon, they would n Eed several days to eat it up. The students want to spend, the minimum money, buy enough watermelon so, they can eat watermelon every day. Can help them?
Notice:when they buy a new watermelon, if they still has an old watermelon, they would throw the old one into dustbin. For example, suppose they buy a watermelon on the fisrt day, and it needs 4 days to eat up the watermelon. But if they buy a new watermelon on the second day and it needs 2 days to eat up the new watermelon, then they'll throw The old one, and they has to buy a new watermelon on the fourth day since they don ' t has any watermelon to eat on that D Ay.
Input
The input contains multiple test cases (no more than-test cases).
In each test case, first there was an integer, n (1 <= n <=50000), which was the number of summer days.
Then there was a line containing n positive integers with the ith integer indicating the price of the watermelon on the ITH Day.
Finally there is line containing n positive integers with the ith integer indicating the number of days students need To eat up the watermelon bought on the ith day.
All these integers is no more than 100000 and integers is seperated by a space.
Output
For each case, output one line with a integer which is the minimum money they must spend so that they can has watermelon To eat every day.
Sample Input
410 20 1 403 2 3 1
Sample Output
11
Test instructions: There are n days, every day can buy watermelon, each watermelon price is AI, each watermelon can eat bi days. What is the minimum cost of watermelon to eat every day in these n days? If you buy a watermelon on the first day, the watermelon you bought before will be all thrown away before you start eating new watermelon.
Dp[i] Indicates the minimum cost of watermelon to eat every day before I day, then dp[i] = min (dp[i), dp[j-k +1] + val[j]);
Monotonic Queue optimization: Queue priority is the cost of the first, large in the rear. If the first team can bring to the current position I, then dp[i]=que.val;
Segment Tree Optimization: Assuming that the current I buy watermelon, then "i,i+day[i]-1" This interval can be spent dp[i-1]+val[i], segment tree maintenance interval minimum value, dp[i]=i point value, Segment tree query single point value.
#include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include < Cstdio> #include <queue> #define LL long long#define N 50050#define INF 999999999using namespace Std;int n;int val[ N],day[n];ll dp[n];struct Que {ll val,day; BOOL operator < (const Que &b) Const {if (Val==b.val) return day>b.day; Return val>b.val; }};p Riority_queue<que>que;int main () {//freopen ("test.in", "R", stdin); Debug (); while (Cin>>n) {for (int i=1; i<=n; i++) {scanf ("%d", &val[i]); } for (int i=1; i<=n; i++) {scanf ("%d", &day[i]); } while (Que.size ()) Que.pop (); Que it; for (int i=1; i<=n; i++) {it.val=dp[i-1]+val[i],it.day=day[i]+i-1; while (Que.size () &&que.top (). Day<i) Que.pop (); Que.push (IT); Dp[i]=que.top (). Val; } printf ("%lld\n", Dp[n]); } return 0;}
#include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include < cstdio> #include <queue> #define Lson l,mid,idx<<1#define Rson mid+1,r,idx<<1|1#define LC idx<& Lt;1#define RC Idx<<1|1#define ll long long#define N 50050#define INF 99999999999using namespace Std;int n;int val[ N],day[n];ll dp[n],tree[n<<2];void push_up (int idx) {tree[idx]=min (TREE[LC],TREE[RC]);} void build (int l,int r,int idx) {tree[idx]=inf; if (l==r) {return; } int mid= (L+R) >>1; Build (Lson); Build (Rson);} void Push_down (int idx) {if (Tree[idx]!=inf) {tree[lc]=min (tree[lc],tree[idx]); Tree[rc]=min (Tree[rc],tree[idx]); Tree[idx]=inf; }}void Update (int l,int r,int idx,int x,int y,ll k) {if (l>=x&&r<=y) {tree[idx]=min (k,tree[idx]); Return } push_down (IDX); int mid= (L+R) >>1; if (x<=mid) update (LSON,X,Y,K); if (Y>mid) Update (RSON,X,Y,K);} ll query (int l,int r,int idx,int x) {if (l==r) {return TREE[IDX]; } push_down (IDX); int mid= (L+R) >>1; if (x<=mid) return query (LSON,X); else return query (RSON,X);} int main () {//freopen ("test.in", "R", stdin); while (Cin>>n) {for (int i=1; i<=n; i++) {scanf ("%d", &val[i]); } for (int i=1; i<=n; i++) {scanf ("%d", &day[i]); } build (1,n,1); dp[0]=0; for (int i=1; i<=n; i++) {int r=i+day[i]-1; if (r>n) r=n; ll k=dp[i-1]+ (LL) val[i]; Update (1,N,1,I,R,K); Dp[i]=query (1,n,1,i); } printf ("%lld\n", Dp[n]); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
ZOJ 3632 Watermelon full of water (dp+ segment tree or monotonic queue optimization)