zoukankan      html  css  js  c++  java
  • codeforces 939F 单调队列优化dp

    F. Cutlet
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    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.

    Input

    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

    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.

    Examples
    input
    Copy
    10 2
    3 5
    11 13
    output
    Full
    2
    input
    Copy
    10 3
    3 5
    9 10
    11 13
    output
    Full
    1
    input
    Copy
    20 1
    3 19
    output
    Hungry
    Note

    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

    直接推荐一篇题解(我不会,抄他的,逃)

     1 /*
     2 Welcome Hacking
     3 Wish You High Rating
     4 */
     5 #include<iostream>
     6 #include<cstdio>
     7 #include<cstring>
     8 #include<ctime>
     9 #include<cstdlib>
    10 #include<algorithm>
    11 #include<cmath>
    12 #include<string>
    13 using namespace std;
    14 int read(){
    15     int xx=0,ff=1;char ch=getchar();
    16     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    17     while(ch>='0'&&ch<='9'){xx=xx*10+ch-'0';ch=getchar();}
    18     return xx*ff;
    19 }
    20 int N,K,f[110][100010];
    21 struct Segment{
    22     int L,R;
    23 }S[110];
    24 int q[100010],head,tail;
    25 int main(){
    26     //freopen("in","r",stdin);
    27     N=read(),K=read();
    28     for(int i=1;i<=K;i++)
    29         S[i].L=read(),S[i].R=read();
    30     for(int j=0;j<=N;j++)
    31         f[0][j]=(1<<29);
    32     f[0][0]=0;
    33     for(int i=1;i<=K;i++){
    34         for(int j=0;j<=N;j++)
    35             f[i][j]=f[i-1][j];
    36         head=1,tail=0;
    37         for(int j=0;j<=S[i].R;j++){
    38             if(j<=N){
    39                 while(head<=tail&&f[i-1][j]<=f[i-1][q[tail]])
    40                     tail--;
    41                 q[++tail]=j;
    42             }
    43             while(head<=tail&&q[head]<=j-(S[i].R-S[i].L+1))
    44                 head++;
    45             if(head<=tail)
    46                 f[i][j]=min(f[i][j],f[i-1][q[head]]+2);
    47         }
    48         head=1,tail=0;
    49         for(int j=S[i].R;j>=0;j--){
    50             if(S[i].R-j<=N){
    51                 while(head<=tail&&f[i-1][S[i].R-j]<=f[i-1][q[tail]])
    52                     tail--;
    53                 q[++tail]=S[i].R-j;
    54             }
    55             while(head<=tail&&q[head]<=S[i].L-(j+1))
    56                 head++;
    57             if(head<=tail)
    58                 f[i][j]=min(f[i][j],f[i][q[head]]+1);
    59         }
    60     }
    61     if(f[K][N]>=(1<<29))
    62         puts("Hungry");
    63     else{
    64         puts("Full");
    65         printf("%d
    ",f[K][N]);
    66     }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    (转)音频降噪算法 附完整C代码
    (转)移动直播技术秒开优化经验(含PPT)
    React实现简单的SearchBox搜索框组件
    为ARM安卓设备交叉编译C/C++语言程序
    Ubuntu无法用快捷键或图标打开终端
    Java基础系列-SPI你认识吗
    Java基础系列-时间日期API
    Java基础系列-RandomAccess
    Java基础系列-Optional
    那些字段适不适合建索引?
  • 原文地址:https://www.cnblogs.com/lzhAFO/p/8551601.html
Copyright © 2011-2022 走看看