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("\n");
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("\n");
82             for(tom=0,i=1; i<=n; i++)
83                 if(flag[i]==true) tom++;
84             printf("Case %d: %d\n",t,tom);
85         }
86     }
87     return 0;
88 }

 

转载于:https://www.cnblogs.com/tom987690183/p/3560661.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值