zoukankan      html  css  js  c++  java
  • hdu 3474 Necklace 单调队列

    Necklace

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1566    Accepted Submission(s): 455


    Problem Description
    You are given a necklace consists of N beads linked as a circle. Each bead is either crystal or jade.
    Now, your task is:
    1.  Choose an arbitrary position to cut it into a chain.
    2.  Choose either direction to collect it.
    3.  Collect all the beads in the chosen direction under the constraint that the number of crystal beads in your hand is not less than the jade at any time.
    Calculate the number of ways to cut meeting the constraint
     
    Input
    In the first line there is an integer T, indicates the number of test cases. (T<=50)
    Then T lines follow, each line describes a necklace. ‘C’ stands for a crystal bead and ‘J’ stands for a jade bead. The length of necklace is between 2 and 10^6.
     
    Output
    For each case, print “Case x: d” on a single line in which x is the number of case counted from one and d is the number of ways.
     
    Sample Input
    2
    CJCJCJ
    CCJJCCJJCCJJCCJJ
     
    Sample Output
    Case 1: 6
    Case 2: 8
     
    Author
    love8909
     
    Source
     
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 using namespace std;
     6 
     7 char a[2000005];
     8 bool flag[2000002];
     9 int s1[2000004];
    10 typedef struct
    11 {
    12     int num;
    13     int sum;
    14 } Queue;
    15 Queue q[2000004],tmp;
    16 int tom;
    17 
    18 int main()
    19 {
    20     int T,t;
    21     int i,k,n,len,tail,head;
    22     while(scanf("%d",&T)>0)
    23     {
    24         getchar();
    25         for(t=1; t<=T; t++)
    26         {
    27             scanf("%s",a+1);
    28             n=strlen(a+1);
    29             len=n+n;
    30             for(i=n+1; i<=len; i++)
    31                 a[i]=a[i-n];
    32             for( s1[0]=0,i=1; i<=len; i++)
    33             {
    34                 if(a[i]=='C') s1[i]=s1[i-1]+1;
    35                 else s1[i]=s1[i-1]-1;
    36             }
    37             memset(flag,false, sizeof(flag));
    38             head=0;
    39             tail=-1;
    40             for(i=1; i<=len-1; i++)
    41             {
    42                 tmp.sum=s1[i];
    43                 tmp.num=i;
    44                 while( head<=tail && q[tail].sum>tmp.sum ) tail--;
    45                 q[++tail]=tmp;
    46                 if( i>=n )
    47                 {
    48                     while( head<=tail && q[head].num+n<=i ) head++;
    49                     if( q[head].sum-s1[i-n]>=0 )
    50                     {
    51                         flag[ i-n+1 ]=true;
    52                     //    printf("%d ",i-n+1);
    53                     }
    54                 }
    55             }
    56         //    printf("
    ");
    57             s1[0]=0;
    58             for(i=1; i<=len; i++)
    59             {
    60                 k=len-i+1;
    61                 if(a[k]=='C') s1[i]=s1[i-1]+1;
    62                 else s1[i]=s1[i-1]-1;
    63             }
    64             head=0;tail=-1;
    65             for(i=1; i<=len; i++)
    66             {
    67                 tmp.sum=s1[i];
    68                 tmp.num=i;
    69                 while( head<=tail && q[tail].sum>tmp.sum ) tail--;
    70                 q[++tail]=tmp;
    71                 if( i>n )
    72                 {
    73                     while( head<=tail && q[head].num+n<=i ) head++;
    74                     if( q[head].sum-s1[i-n]>=0 )
    75                     {
    76                         flag[ n-(i-n)+1 ]=true;
    77                     //    printf("%d ",n-(i-n)+1);
    78                     }
    79                 }
    80             }
    81         //    printf("
    ");
    82             for(tom=0,i=1; i<=n; i++)
    83                 if(flag[i]==true) tom++;
    84             printf("Case %d: %d
    ",t,tom);
    85         }
    86     }
    87     return 0;
    88 }
  • 相关阅读:
    [CF1355] Codeforces Round #643 (Div. 2)
    [ABC189] AtCoder Beginner Contest 189
    P3702 [SDOI2017]序列计数 (三模数NTT)
    P3321 [SDOI2015]序列统计 (NTT快速幂)
    洛谷P4157 [SCOI2006]整数划分
    洛谷P2553 [AHOI2001]多项式乘法
    洛谷P1919 (模板)A*B Problem升级版(FFT快速傅里叶)
    MySQL学习总结-详细版(包括下载安装)
    查看oracle数据库中表是否被锁
    SQL优化(面试题)
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3560661.html
Copyright © 2011-2022 走看看