codeforces 939F 单调队列优化dp
4 seconds
256 megabytes
standard input
standard output
Arkady wants to have a dinner. He has just returned from a shop where he has bought a semifinished cutlet. He only needs to fry it. The cutlet should be fried for 2n seconds, in particular, it should be fried for n seconds on one side and n seconds on the other side. Arkady has already got a frying pan and turn on fire, but understood that maybe he won't be able to flip the cutlet exactly after n seconds after the beginning of cooking.
Arkady is too busy with sorting sticker packs in his favorite messenger and can flip the cutlet only in some periods of time. Namely, there are k periods of time in which he can do it, the i-th of them is an interval of time from li seconds after he starts cooking till ri seconds, inclusive. Arkady decided that it's not required to flip the cutlet exactly in the middle of cooking, instead, he will flip it several times in such a way that the cutlet will be fried exactly n seconds on one side and n seconds on the other side in total.
Help Arkady and find out if it's possible for him to cook the cutlet, if he is able to flip the cutlet only in given periods of time; and if yes, find the minimum number of flips he needs to cook the cutlet.
The first line contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 100) — the number of seconds the cutlet should be cooked on each side and number of periods of time in which Arkady can flip it.
The next k lines contain descriptions of these intervals. Each line contains two integers li and ri(0 ≤ li ≤ ri ≤ 2·n), meaning that Arkady can flip the cutlet in any moment starting from li seconds after the beginning of cooking and finishing at ri seconds after beginning of cooking. In particular, if li = ri then Arkady can flip the cutlet only in the moment li = ri. It's guaranteed that li > ri - 1 for all 2 ≤ i ≤ k.
Output "Hungry" if Arkady won't be able to fry the cutlet for exactly n seconds on one side and exactly nseconds on the other side.
Otherwise, output "Full" in the first line, and the minimum number of times he should flip the cutlet in the second line.
10 2
3 5
11 13
Full
2
10 3
3 5
9 10
11 13
Full
1
20 1
3 19
Hungry
In the first example Arkady should flip the cutlet in time moment 3 seconds after he starts cooking and in time moment 13 seconds after he starts cooking.
In the second example, Arkady can flip the cutlet at 10 seconds after he starts cooking.
题意:
有一个烤肉,需要正面烤N秒,反面烤N秒。
给出K个时间区间,在这些区间内可以翻转烤肉
N<=100000 K<=100
http://blog.csdn.net/Charlie_jilei/article/details/79342492
直接推荐一篇题解(我不会,抄他的,逃)
/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
int xx=,ff=;char ch=getchar();
while(ch>''||ch<''){if(ch=='-')ff=-;ch=getchar();}
while(ch>=''&&ch<=''){xx=xx*+ch-'';ch=getchar();}
return xx*ff;
}
int N,K,f[][];
struct Segment{
int L,R;
}S[];
int q[],head,tail;
int main(){
//freopen("in","r",stdin);
N=read(),K=read();
for(int i=;i<=K;i++)
S[i].L=read(),S[i].R=read();
for(int j=;j<=N;j++)
f[][j]=(<<);
f[][]=;
for(int i=;i<=K;i++){
for(int j=;j<=N;j++)
f[i][j]=f[i-][j];
head=,tail=;
for(int j=;j<=S[i].R;j++){
if(j<=N){
while(head<=tail&&f[i-][j]<=f[i-][q[tail]])
tail--;
q[++tail]=j;
}
while(head<=tail&&q[head]<=j-(S[i].R-S[i].L+))
head++;
if(head<=tail)
f[i][j]=min(f[i][j],f[i-][q[head]]+);
}
head=,tail=;
for(int j=S[i].R;j>=;j--){
if(S[i].R-j<=N){
while(head<=tail&&f[i-][S[i].R-j]<=f[i-][q[tail]])
tail--;
q[++tail]=S[i].R-j;
}
while(head<=tail&&q[head]<=S[i].L-(j+))
head++;
if(head<=tail)
f[i][j]=min(f[i][j],f[i][q[head]]+);
}
}
if(f[K][N]>=(<<))
puts("Hungry");
else{
puts("Full");
printf("%d\n",f[K][N]);
}
return ;
}
codeforces 939F 单调队列优化dp的更多相关文章
- 【简洁易懂】CF372C Watching Fireworks is Fun dp + 单调队列优化 dp优化 ACM codeforces
题目大意 一条街道有$n$个区域. 从左到右编号为$1$到$n$. 相邻区域之间的距离为$1$. 在节日期间,有$m$次烟花要燃放. 第$i$次烟花燃放区域为$a_i$ ,幸福属性为$b_i$,时间为 ...
- 单调队列优化DP,多重背包
单调队列优化DP:http://www.cnblogs.com/ka200812/archive/2012/07/11/2585950.html 单调队列优化多重背包:http://blog.csdn ...
- bzoj1855: [Scoi2010]股票交易--单调队列优化DP
单调队列优化DP的模板题 不难列出DP方程: 对于买入的情况 由于dp[i][j]=max{dp[i-w-1][k]+k*Ap[i]-j*Ap[i]} AP[i]*j是固定的,在队列中维护dp[i-w ...
- hdu3401:单调队列优化dp
第一个单调队列优化dp 写了半天,最后初始化搞错了还一直wa.. 题目大意: 炒股,总共 t 天,每天可以买入na[i]股,卖出nb[i]股,价钱分别为pa[i]和pb[i],最大同时拥有p股 且一次 ...
- Parade(单调队列优化dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2490 Parade Time Limit: 4000/2000 MS (Java/Others) ...
- BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP
BZOJ_3831_[Poi2014]Little Bird_单调队列优化DP Description 有一排n棵树,第i棵树的高度是Di. MHY要从第一棵树到第n棵树去找他的妹子玩. 如果MHY在 ...
- 【单调队列优化dp】 分组
[单调队列优化dp] 分组 >>>>题目 [题目] 给定一行n个非负整数,现在你可以选择其中若干个数,但不能有连续k个数被选择.你的任务是使得选出的数字的和最大 [输入格式] ...
- [小明打联盟][斜率/单调队列 优化dp][背包]
链接:https://ac.nowcoder.com/acm/problem/14553来源:牛客网 题目描述 小明很喜欢打游戏,现在已知一个新英雄即将推出,他同样拥有四个技能,其中三个小技能的释放时 ...
- 单调队列以及单调队列优化DP
单调队列定义: 其实单调队列就是一种队列内的元素有单调性的队列,因为其单调性所以经常会被用来维护区间最值或者降低DP的维数已达到降维来减少空间及时间的目的. 单调队列的一般应用: 1.维护区间最值 2 ...
随机推荐
- Functional language 函数
一.什么是函数式语言? 函数式语言一类程序设计语言,是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型.这种语言具有较强的组织数据结构的能力,可以把某一数据 ...
- 【原】Python学习
1.常用模块介绍 #python -m SimpleHTTPServer 执行上面的命令就会在服务器当前目录下启动一个文件下载服务器,默认打开8000端口.这个时候,你只需要将IP和端口告诉客户端,即 ...
- 考试总结(CE???)
直接开写题解: (由于T1为暴力模拟,不进行整理) T2: 扶苏给了你一棵树,这棵树上长满了幼嫩的新叶,我们约定这棵树的根是 1,每个节 点都代表树上的一个叶子. 如果你不知道什么叫树,你可以认为树是 ...
- .Net Core2.2 + EF Core + DI,三层框架项目搭建教程
笔记: 近两年.Net Core发展的很快,目前最新版为3.0预览版,之前在网上买了一本1.1版书籍都还没来得及看呢,估计现在拿出来看也毫无意义了.已多年.net工作经验,看书不如直接实际上手来得快, ...
- Number 数据类型
//Number 数据类型//包含 整数 小数 NaN(not a number)var a = 1233;var b = 12.34;//1/'a'//把其他数据类型转化成数字,他在转化时,只要字符 ...
- 洛谷——P2047 [NOI2007]社交网络
P2047 [NOI2007]社交网络 $Floyd$,一眼看到就是他(博主是不小心瞄到了这个题的标签吧qwq) 这个题目只要预处理出$S$到$T$的最短路的条数即可,类似$Spfa$的更新方法 如果 ...
- UVA - 1620 Lazy Susan(逆序数)
题目: 把1~n(n≤500)放到一个圆盘里,每个数恰好出现一次.每次可以选4个连续的数字翻转顺序.问能不能变成1.2.3....n的顺序. 思路: 这样的题的规律真的是一点都不好推,看了网上的博客知 ...
- python正则表达式的好文章(转)
http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html https://blog.csdn.net/shw800/article/det ...
- Python函数基础---参数、变量
函数:指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可. def sayhi( ): # 函数名 print('hello world') sayhi( ) # ...
- HDU——1133 Buy the Ticket
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...