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(优先队列优化) 模板 /******************************** ...
随机推荐
- JAVA_OPTS讲解【转】
JAVA_OPTS ,顾名思义,是用来设置JVM相关运行参数的变量. JAVA_OPTS="-server -Xms256m -Xmx512m -XX:PermSize=64M -XX:Ma ...
- C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定
思路来源:http://bbs.csdn.NET/topics/390819824,引用该页面某网友提供的方法. 题目:我现在有100个任务,需要多线程去完成,但是要限定同时并发数量不能超过5个. 原 ...
- 数据库--mysql介绍
一:什么是数据库 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件 ...
- urllib2使用初探
在入门urllib2之前,我想应该先调研一下urllib与urllib2的区别[1].首先我们要明白的是,这两个模块不可以相互替代.两者都是接受URL请求的模块,但是提供了不同的功能,两个显著的区别是 ...
- centos7联网
一般centos安装(在虚拟机上安装)完成后需要自己配置服务,下面我就讲下如何配置 配置联网步骤 首先,打开虚拟机的两个服务,右击我的电脑-->管理--->找到服务-->右击启动 两 ...
- HDU 4632 Palindrome subsequence(区间DP求回文子序列数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...
- U盘删除文件时提示“文件或目录损坏且无法读取”的解决方法
U盘删除文件时提示“文件或目录损坏且无法读取”的解决方法 出现原因:在写入或读取文件时,进行复制操作,此时复制到的文件是不完整的!或者移动硬盘/U盘中途被拔出,导致文件损坏 异常现象:被删文件(夹)属 ...
- ubuntu14.04 使用传统的netcat
Ubuntu上默认安装的是netcat-openbsd,而不是经典的netcat-traditional. 网上例子很多都是以netcat-traditional为例. sudo apt-get -y ...
- Visual Studio 2013百度云下载地址
Visual Studio 2013百度云下载地址: 链接: https://pan.baidu.com/s/1JkVYLnFo2TWSu4dkDdZP3Q 提取码: 关注公众号[获取 winf ...
- React.js学习之环境搭建
1 工欲善其事必先利其器:前端开发工具 1.1 WebStorm和Sublime Text Sublime Text:作为代码编辑器,Sublime Text的优点如下: 主流前端开发编辑器 体积较小 ...