练习赛补题-------K - Watermelon Full of Water DP

Watermelon is very popular in the hot summer. Students in ZJU-ICPC Team also love watermelon very much and they hope that they can have watermelon to eat every day during the summer vacation. Suppose there are n days and every day they can buy only one watermelon. The price of watermelon may be different in each day. Besides, sometimes the watermelon they choose to buy may be very big, which means if they buy this watermelon, they will need several days to eat it up. The students want to spend the minimum money to buy enough watermelon so that they can eat watermelon every day. Can you help them?

Notice: When they buy a new watermelon, if they still have an old watermelon, they will 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 will throw the old one, and they have to buy a new watermelon on the fourth day since they don’t have any watermelon to eat on that day.

Input
The input contains multiple test cases ( no more than 200 test cases ).
In each test case, first there is an integer, n ( 1 <= n <=50000 ) , which is the number of summer days.
Then there is 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 are no more than 100000 and integers are seperated by a space.

Output
For each case, output one line with an integer which is the minimum money they must spend so that they can have watermelon to eat every day.

Sample Input
4
10 20 1 40
3 2 3 1
Sample Output
11

动态规划:
dp[i]代表i天内每天都有西瓜吃的最小消费;
那么对于每天,假设选上当天的西瓜,那么dp[i - 1] + price[i]这个值在[i,i + day[i] - 1]这段时间区间内,这个值是可以用的(可以成为dp值,选最小的即可),所以这就需要维护一个区间最小值了,可以用线段树,也可以用优先队列,我用的是优先队列优化的。
线段树的话,区间更新即可,dp[i]就是线段树i点的值
注意:该题会爆long long,所以结构体内需要用LL

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 50005;

int price[N];
int day[N];
LL dp[N];
int n;
typedef struct Node{
    LL x,y;
    friend bool operator < (const Node &p,const Node &q){
        if(p.x == q.x){
            return p.y > q.y;
        }else{
            return p.x > q.x;
        }
    }
}Node;
priority_queue<Node>que;

int main()
{
    while(~scanf("%d",&n)){
        memset(dp,0,sizeof(dp));
        for(int i = 1;i <= n;++i){
            scanf("%d",&price[i]);
        }
        for(int i = 1;i <= n;++i){
            scanf("%d",&day[i]);
        }
        while(!que.empty()) que.pop();
        Node node;
        for(int i = 1;i <= n;++i){
            //在第i天买了第i个瓜,在[i,i + day[i] - 1]范围内都可以用这个值
            node.x = dp[i - 1] + price[i];
            node.y = i + day[i] - 1;
            while(!que.empty() && que.top().y < i) que.pop();
            que.push(node);
            dp[i] = que.top().x;
        }
        printf("%lld\n",dp[n]);
    }
    return 0;
}

发布了269 篇原创文章 · 获赞 33 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/88865857