Watermelon Full of Water - ZOJ 3632 dp+线段树

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

题意:每天可以买一个西瓜,每个西瓜有相应的价钱,和可以吃多少天,要求每天都吃西瓜,问你最少的花费是多少。

思路:首先逆推,第k天的最小花费是num1[k]+dp[k+  (1...num2[k])  ],然后因为数据量比较大,需要用线段树来得到后面部分的最小值是多少。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
long long dp[50010],num1[50010],num2[50010];
int t=0,f[50010];
struct node
{ long long l,r,minn;
  int pos;
}tree[200010];
void build(int num,int x,int y)
{ tree[num].l=x;
  tree[num].r=y;
  if(x==y)
  { tree[num].minn=100000*100000;
    tree[num].pos=++t;
    f[x]=num;
    return;
  }
  int mid=(tree[num].l+tree[num].r)/2;
  int left=2*num;
  int right=left+1;
  build(left,x,mid);
  build(right,mid+1,y);
  if(tree[left].minn<tree[right].minn)
  { tree[num].minn=tree[left].minn;
    tree[num].pos=tree[left].pos;
  }
  else
  { tree[num].minn=tree[right].minn;
    tree[num].pos=tree[right].pos;
  }
}
void update(int num,int x)
{ tree[num].minn=x;
  num/=2;
  int left,right;
  while(num>0)
  { left=2*num;right=left+1;
    if(tree[left].minn<tree[right].minn)
    { tree[num].minn=tree[left].minn;
      tree[num].pos=tree[left].pos;
    }
    else
    { tree[num].minn=tree[right].minn;
      tree[num].pos=tree[right].pos;
    }
    num/=2;
  }
}
int query(int num,int x,int y)
{ if(tree[num].l==x && tree[num].r==y)
   return tree[num].pos;
  int mid=(tree[num].l+tree[num].r)/2;
  int left=2*num;
  int right=left+1;
  if(y<=mid)
   return query(left,x,y);
  else if(x>mid)
   return query(right,x,y);
  else
  { int pos1,pos2;
    pos1=query(left,x,mid);
    pos2=query(right,mid+1,y);
    if(dp[pos1]<dp[pos2])
     return pos1;
    else
     return pos2;
  }
}
int main()
{ int n,i,j,k;
  while(~scanf("%d",&n))
  { t=0;
    for(i=1;i<=n;i++)
     scanf("%d",&num1[i]);
    for(i=1;i<=n;i++)
     scanf("%d",&num2[i]);
    dp[n]=num1[n];
    build(1,1,n+1);
    for(i=n-1;i>=1;i--)
    { if(i+num2[i]>=n+1)
       dp[i]=num1[i];
      else
       dp[i]=min(dp[i+1]+num1[i],dp[query(1,i+1,i+num2[i])]+num1[i]);
      update(f[i],dp[i]);
    }
    printf("%lld\n",dp[1]);
  }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值