ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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=;i<=n;i++)
scanf("%d",&p[i]);
for(i=;i<=n;i++)
scanf("%d",&last[i]);
priority_queue<node> q;
node temp;
dp[]=p[];
temp.val=p[]; temp.last=last[];
q.push(temp);
for(i=;i<=n;i++)
{
temp.val=dp[i-]+p[i];
temp.last=last[i]+i-;
q.push(temp);
while(q.top().last<i) q.pop();
dp[i]=q.top().val;
}
printf("%lld\n",dp[n]);
}
return ;
}
ZOJ 3632 K - Watermelon Full of Water 优先队列优化DP的更多相关文章
- XJOI3602 邓哲也的矩阵(优先队列优化DP)
题目描述: 有一个 n×m的矩阵,现在准备对矩阵进行k次操作,每次操作可以二选一 1: 选择一行,给这一行的每一个数减去p,这种操作会得到的快乐值等于操作之前这一行的和 2: 选择一列,给这一列的每一 ...
- Atcoder 2566 3N Numbers(优先队列优化DP)
問題文N を 1 以上の整数とします. 長さ 3N の数列 a=(a1,a2,…,a3N) があります. すぬけ君は.a からちょうど N 個の要素を取り除き.残った 2N 個の要素を元の順序で並べ. ...
- 最短路算法模板合集(Dijkstar,Dijkstar(优先队列优化), 多源最短路Floyd)
再开始前我们先普及一下简单的图论知识 图的保存: 1.邻接矩阵. G[maxn][maxn]; 2.邻接表 邻接表我们有两种方式 (1)vector< Node > G[maxn]; 这个 ...
- poj 1511 优先队列优化dijkstra *
题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...
- 【bzo1579】拆点+dijkstra优先队列优化+其他优化
题意: n个点,m条边,问从1走到n的最短路,其中有K次机会可以让一条路的权值变成0.1≤N≤10000;1≤M≤500000;1≤K≤20 题解: 拆点,一个点拆成K个,分别表示到了这个点时还有多少 ...
- hdu 1874(最短路 Dilkstra +优先队列优化+spfa)
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- ZOJ 3874 Permutation Graph (分治NTT优化DP)
题面:vjudge传送门 ZOJ传送门 题目大意:给你一个排列,如果两个数构成了逆序对,就在他们之间连一条无向边,这样很多数会构成一个联通块.现在给出联通块内点的编号,求所有可能的排列数 推来推去容易 ...
- 晴天小猪历险记之Hill(Dijkstra优先队列优化)
描述 这一天,他来到了一座深山的山脚下,因为只有这座深山中的一位隐者才知道这种药草的所在.但是上山的路错综复杂,由于小小猪的病情,晴天小猪想找一条需时最少的路到达山顶,但现在它一头雾水,所以向你求助. ...
- 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...
随机推荐
- xshell5 优化方案
有道云笔记链接-> grep: 过滤 过滤的速度是最快的(相对于另外两个) -v -n -o 显示grep匹配到了什么 grep . -o -i --ignore-case -E == ...
- js中字符串的常用方法
一.普通方法 1.字符方法 动态方法:1.str.charAt(index); 返回子字符串,index为字符串下标,index取值范围[0,str.length-1] 动态方法:2.str.cha ...
- fsevents npm install是报错
npm install 安装插件的时候,fsevents报错,这是node 8.x版本的问题,解决办法,把node 版本切换到6.x
- TypeScript 2 : 获取当前日期及前后范围日期【Array】
前言 今天有个接口字段需求,要写一个今天及前几天的日期传过去: 在网上找了下都木有什么比较好的方案:就自己写了一个. 因为技术栈就是NG2+TS2+WEBPACK,这里的代码需要一定的TS2及ES6的 ...
- MySQL学习笔记:删除存储过程和函数
删除存储过程.存储函数主要使用drop语句: drop procedure —— 删除存储过程 drop function —— 删除存储函数 语法: DROP {PROCEDURE|FUNCTI ...
- 20155225 2016-2017-2《Java程序设计》课程总结
20155225 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:新的开始 预备作业2:C语言学习回顾 预备作业3:Linux基础入门和虚拟机的安装 第一 ...
- wordpress 常用函数-wpdb类
与数据库建立接口 WordPress为用户提供了一系列用于数据库操作的函数类——wpdb.Wpdb类建立在Justin Vincent编写并维护的ezSQL类的基础上. 使用须知 不可直接调用wpdb ...
- 安装部署Apache Hadoop (本地模式和伪分布式)
本节内容: Hadoop版本 安装部署Hadoop 一.Hadoop版本 1. Hadoop版本种类 目前Hadoop发行版非常多,有华为发行版.Intel发行版.Cloudera发行版(CDH)等, ...
- sicily 1051. Biker's Trip Odomete
DescriptionMost bicycle speedometers work by using a Hall Effect sensor fastened to the front fork o ...
- USACO 6.5 All Latin Squares
All Latin Squares A square arrangement of numbers 1 2 3 4 5 2 1 4 5 3 3 4 5 1 2 4 5 2 3 1 5 3 1 2 4 ...