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(优先队列优化) 模板 /******************************** ...
随机推荐
- MySQL 5.7.17 Group Relication(组复制)搭建手册【转】
本博文介绍了Group Replication的两种工作模式的架构.并详细介绍了Single-Master Mode的部署过程,以及如何切换到Multi-Master Mode.当然,文末给出了Gro ...
- 一步一步搭建oracle 11gR2 rac+dg之database安装(五)【转】
一步一步在RHEL6.5+VMware Workstation 10上搭建 oracle 11gR2 rac + dg 之database安装 (五) 转自 一步一步搭建oracle 11gR2 ...
- Dapper实用教程
Dapper是什么? Dpper是一款.Net平台简单(Simple)的对象映射库,并且Dapper拥有着“微型ORM之王”的称号.就速度而言与手写ADO.NET SqlDateReader相同.OR ...
- Django-模板语言和过滤器
Django模板语言 首先模板只是一个文本文件,它可以生成任何基于文本的格式(HTML.XML.CSS等),模板中包含变量,在模板被渲染的时候替换为最终的值,以及控制模板逻辑的标签. 变量使用{{ 变 ...
- mysql -> 事务&事务锁_09
事务的特性 redo undo 锁的隔离级别
- 栈应用之 背包问题(Python 版)
栈应用之 背包问题 背包问题描述:一个背包里可以放入重量为weight的物品,现有n件物品的集合s,其中物品的重量为别为w0,w1,...,wn-1.问题是能否从中选出若干件物品,其重量之和正好等于w ...
- Isolate randomforest检测异常点的非监督方法
由于异常数据的两个特征(少且不同: few and different) 异常数据只占很少量; 异常数据特征值和正常数据差别很大. iTree的构成过程如下: l 随机选择一个属性Attr: l ...
- 【CF767C】Garland
传送门啦 分析: 这个题我是看着翻译做的,感觉不是很难,很普通的一个树形dp 题目大意: 在一棵树上分离出三个子树,使这三个子树的点权和相等. 明确题目意思这个题就简单多了吧. 我们会发现每一棵子树的 ...
- Java标记接口
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------这篇博客主要来谈谈" ...
- 三 oracle 用户管理一
一.创建用户概述:在oracle中要创建一个新的用户使用create user语句,一般是具有dba(数据库管理员)的权限才能使用.create user 用户名 identified by 密码; ...