K - Watermelon Full of Water

Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Appoint description:
 

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的更多相关文章

  1. XJOI3602 邓哲也的矩阵(优先队列优化DP)

    题目描述: 有一个 n×m的矩阵,现在准备对矩阵进行k次操作,每次操作可以二选一 1: 选择一行,给这一行的每一个数减去p,这种操作会得到的快乐值等于操作之前这一行的和 2: 选择一列,给这一列的每一 ...

  2. Atcoder 2566 3N Numbers(优先队列优化DP)

    問題文N を 1 以上の整数とします. 長さ 3N の数列 a=(a1,a2,…,a3N) があります. すぬけ君は.a からちょうど N 個の要素を取り除き.残った 2N 個の要素を元の順序で並べ. ...

  3. 最短路算法模板合集(Dijkstar,Dijkstar(优先队列优化), 多源最短路Floyd)

    再开始前我们先普及一下简单的图论知识 图的保存: 1.邻接矩阵. G[maxn][maxn]; 2.邻接表 邻接表我们有两种方式 (1)vector< Node > G[maxn]; 这个 ...

  4. poj 1511 优先队列优化dijkstra *

    题意:两遍最短路 链接:点我 注意结果用long long #include<cstdio> #include<iostream> #include<algorithm& ...

  5. 【bzo1579】拆点+dijkstra优先队列优化+其他优化

    题意: n个点,m条边,问从1走到n的最短路,其中有K次机会可以让一条路的权值变成0.1≤N≤10000;1≤M≤500000;1≤K≤20 题解: 拆点,一个点拆成K个,分别表示到了这个点时还有多少 ...

  6. hdu 1874(最短路 Dilkstra +优先队列优化+spfa)

    畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. ZOJ 3874 Permutation Graph (分治NTT优化DP)

    题面:vjudge传送门 ZOJ传送门 题目大意:给你一个排列,如果两个数构成了逆序对,就在他们之间连一条无向边,这样很多数会构成一个联通块.现在给出联通块内点的编号,求所有可能的排列数 推来推去容易 ...

  8. 晴天小猪历险记之Hill(Dijkstra优先队列优化)

    描述 这一天,他来到了一座深山的山脚下,因为只有这座深山中的一位隐者才知道这种药草的所在.但是上山的路错综复杂,由于小小猪的病情,晴天小猪想找一条需时最少的路到达山顶,但现在它一头雾水,所以向你求助. ...

  9. 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛

    传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...

随机推荐

  1. python版本共存

    要玩多版本最好使用虚拟环境,避免根python切换及包误安装的麻烦 1.直接安装实现 1.1 windows下 到官网(https://www.python.org/downloads/)下载,如py ...

  2. 用django-cors-headers做跨域

    什么是CORS? CORS(跨域资源共享,Cross-Origin Resource Sharing)是一种跨域访问的机制,可以让Ajax实现跨域访问. 其实,在服务器的response header ...

  3. 我常用的 Python 调试工具 - 博客 - 伯乐在线

    .ckrating_highly_rated {background-color:#FFFFCC !important;} .ckrating_poorly_rated {opacity:0.6;fi ...

  4. CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析

       Adobe Acrobat和Reader都是美国Adobe公司开发的非常流行的PDF文件阅读器. 基于Window和Mac OS X的Adobe Reader和Acrobat 9.4之前的9.x ...

  5. HP 打印机监控

    http://www.ttlsa.com/zabbix/zabbix-monitor-hp-printer/ https://www.cnblogs.com/losbyday/articles/583 ...

  6. 【58沈剑架构系列】互联网公司为啥不使用mysql分区表?

    缘起:有个朋友问我分区表在58的应用,我回答不出来,在我印象中,百度.58都没有听说有分区表相关的应用,业内进行一些技术交流的时候也更多的是自己分库分表,而不是使用分区表.于是去网上查了一下,并询问了 ...

  7. 1975: [Sdoi2010]魔法猪学院

    k短路,只会写A*的飘过..priority_queue超空间差评!

  8. C语言:奇偶归一猜想

    1.奇偶归一猜想——求多少步归一.(10分) 题目内容: 奇偶归一猜想——对于每一个正整数,如果它是奇数,则对它乘3再加1,如果它是偶数,则对它除以2,如此循环,最终都能够得到1. 如n = 11,得 ...

  9. centos6编译安装zabbix3.0和中文支持整理文档

    编者按: 最近公司部分业务迁移机房,为了更方便的监控管理主机资源,决定上线zabbix监控平台.运维人员使用2.4版本的进行部署,个人在业余时间尝鲜,使用zabbix3.0进行部署,整理文档如下,仅供 ...

  10. hdoj2602 Bone Collector(DP,01背包)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2602 题意 有n块骨头,每块骨头体积为volume,价值为value,还有一个容量为v的背包,现在将骨 ...