Necklace 单调队列

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
***************************************************************************************************************************
单调队列
**************************************************************************************************************************
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 using namespace std;
 7 char str[1500001];
 8 int num[1200001];
 9 int que[1200001];
10 bool f1[2100001],f2[2100001];
11 int sum[2100001],n,result;
12 void  solve(bool t)//单调队列,求最优值
13 {
14     int head=1,tail=0;
15     int it,jt;
16     for(it=1;it<n;it++)
17     {
18         while(head<=tail&&sum[que[tail]]<=sum[it])tail--;
19         tail++;
20         que[tail]=it;
21     }
22     for(it=n;it<=2*n;it++)
23     {
24         while(head<=tail&&sum[que[tail]]<=sum[it])tail--;
25         tail++;
26         que[tail]=it;
27         while(head<=tail&&que[head]<it-n)head++;
28         if(sum[it]-sum[que[head]]>=0)
29         {
30             if(t)
31               f1[it-n]=true;
32             else
33                f2[it-n]=true;
34         }
35 
36     }
37 }
38 int main()
39 {
40     int cas,T=0,i,j;
41     scanf("%d",&cas);
42     while(cas--)
43     {
44         T++;
45         scanf("%s",str+1);
46         n=strlen(str+1);
47         memset(f1,false,sizeof(f1));
48         memset(f2,false,sizeof(f2));
49         sum[0]=0;
50         for(i=1;i<=n;i++)
51         {
52             if(str[i]=='C')
53                 num[i]=1;
54             else
55                 num[i]=-1;
56         }
57         for(i=1;i<=n;i++)
58          num[i+n]=num[i];
59         for(i=1;i<=2*n;i++)
60          sum[i]=sum[i-1]+num[i];
61         solve(true);
62         for(i=1;i<=n;i++)
63          sum[i]=sum[i-1]+num[n+1-i];
64         for(i=n+1;i<=2*n;i++)
65           sum[i]=sum[i-1]+num[2*n+1-i];
66         solve(false);
67         result=0;
68         for(i=0;i<n;i++)
69          if(f1[i]||f2[n-i])
70           result++;
71         printf("Case %d: %d\n",T,result);
72     }
73     return 0;
74 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3451706.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值