zoukankan      html  css  js  c++  java
  • ZOJ 3632 K

    K - Watermelon Full of Water

    Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

    Appoint description:
     

    Description

    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
    题意:有n天,每天都可以买西瓜,西瓜有价格和可以吃的时间,同时只能拥有一个西瓜,然后问你最少花费,让自己每天都能吃西瓜
    比较普通的DP题,转移方程是当if(last[k]+k-1>i) dp[i]=min(dp[i],dp[k-1]+val[k])
    但是普普通通的做会T掉,所以得优先队列优化一下

    int p[N],last[N];
    long long dp[N];
    struct node
    {
        long val;
        int last;
        bool operator<(const node& a)const
        {
            return val>a.val;
        }
    };
    int main()
    {
        int n;
        int i;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=1;i<=n;i++)
                scanf("%d",&p[i]);
            for(i=1;i<=n;i++)
                scanf("%d",&last[i]);
            priority_queue<node> q;
                node temp;
            dp[1]=p[1];
            temp.val=p[1]; temp.last=last[1];
            q.push(temp);
            for(i=2;i<=n;i++)
            {
                temp.val=dp[i-1]+p[i];
                temp.last=last[i]+i-1;
                q.push(temp);
                while(q.top().last<i) q.pop();
                dp[i]=q.top().val;
            }
            printf("%lld
    ",dp[n]);
        }
        return 0;
    }

  • 相关阅读:
    窗口显示于parent控件上
    DELPHI SOCKET 通信编程要点小结
    dxBarManagerToDxNavBar方法
    DLL直接返回对象
    海量数据库的查询优化及分页算法方案
    excel怎么只打印某页?excel怎么只打印某几页
    HTTP请求错误400、401、402、403、404、405、406、407、412、414、500、501、502解析
    excel中如何设置只打印第一页
    Navicat Premium 常用功能讲解
    laravel查询构造器DB还是ORM,这两者有什么区别,各该用在什么场景中
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4317885.html
Copyright © 2011-2022 走看看