zoukankan      html  css  js  c++  java
  • ZOJ Problem Set 3632 Watermelon Full of Water

    ZOJ Problem Set - 3632
    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 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
    

    Author: HUANG, Qiao
    Contest: ZOJ Monthly, July 2012
    //动规加线段树优化

    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #define N 50005 #define Max 1000000000000000 #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 using namespace std; long long Min[N<<2]; int p[N],last[N]; long long dp[N]; void build(int l,int r,int k) { Min[k]=Max; if(l==r) return; int m=(l+r)>>1; build(lson); build(rson); } void up(int &k) { Min[k]=min(Min[k<<1],Min[k<<1|1]); } long long val; void update(int &index,int l,int r,int k) { if(l==r)//更新每天买的西瓜最远可以持续到的天数 { Min[k]=min(val,Min[k]); return ; } int m=(l+r)>>1; if(index<=m) update(index,lson); else update(index,rson); up(k); } long long query(int &L,int &R,int l,int r,int k) { if(L<=l&&R>=r) { return Min[k]; } int m=(l+r)>>1; long long t1=Max,t2=Max; if(L<=m) t1=query(L,R,lson); if(R>m) t2=query(L,R,rson); return min(t1,t2); } int main() { int n; int i,k; 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]); build(1,n,1); dp[0]=0; for(i=1;i<=n;i++) { k=last[i]+i-1; k=k>n?n:k;val=dp[i-1]+p[i]; update(k,1,n,1); dp[i]=query(i,n,1,n,1);//从当前到结束找到最小的 } printf("%lld\n",dp[n]); } return 0; }
    //还有一种是用优先队列优化,经测试,线段树优化要快点
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #define N 50005
    using namespace std;
    long long Min[N<<2];
    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\n",dp[n]);
        }
        return 0;
    }


  • 相关阅读:
    洛谷 P6622
    洛谷 P6619
    LOJ 3188
    CF::Gym 102174G
    eJOI2017~2019
    洛谷 P6313
    洛谷 P6305
    JSOI2020 酱油记
    洛谷 P6234
    CodeForces 1334F
  • 原文地址:https://www.cnblogs.com/372465774y/p/2623656.html
Copyright © 2011-2022 走看看