【HDU】3474Necklace(单调队列,正反两个)

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


 

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

2010 ACM-ICPC Multi-University Training Contest(4)——Host by UESTC

 

 

Recommend

zhengfeng

 

 

题目大意:给个成环的字符串,现在要从一个地方断开这个环,然后可以向左或向右走,在走的过程中C的数量要始终保持大于J的数量,问共有多少个这样的端点

思路:我们以C为1J为-1,用cnt【i】表示前缀和,然后我们要找到的是前缀和不小于0的那一部分,这道题单调队列写两遍就行了,一次是全都向左走,另一次是全都向右走

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1000000+5;
struct queuey
{
    int pos,sum;
}q[maxn*2];
int head,tail;
void insert(int pos,int sum)
{
    while(head<tail&&sum<q[tail].sum)
        tail--;
    q[++tail].pos=pos;
    q[tail].sum=sum;
}
char s[2*maxn],ss[2*maxn];
bool vis[2*maxn];
int cnt[2*maxn];
int main()
{
    int kase=1,t;
    scanf("%d",&t);
    while(t--)
    {
        int count=0;
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;i<len;i++)
            ss[i]=s[i];
        for(int i=0;i<len;i++)
            ss[len+i]=s[i];
        cnt[0]=ss[0]=='C'?1:-1;
        head=tail=0;
        for(int i=1;i<2*len;i++)
            cnt[i]=cnt[i-1]+(ss[i]=='C'?1:-1);
        memset(vis,false,sizeof(vis));
        for(int i=0;i<2*len;i++)
        {
            insert(i,cnt[i]);
            while(q[head+1].pos<=i-len&&head<tail)
                head++;
            if(i-len>=0)
            {
                if(q[head+1].sum>=cnt[i-len])
                    if(vis[i]==0)
                    {
                        count++;
                        vis[i]=1;
                    }
            }
        }
        head=tail=0;
        cnt[2*len]=0;
        cnt[2*len-1]=ss[2*len-1]=='C'?1:-1;
        for(int i=2*len-2;i>=0;i--)
            cnt[i]=cnt[i+1]+(ss[i]=='C'?1:-1);

        for(int i=2*len;i>0;i--)
        {
            insert(i,cnt[i]);
             while(q[head+1].pos>=i+len&&head<tail)
                head++;
            if(i<=len)
            {
                if(q[head+1].sum>=cnt[i+len])
                    if(vis[i+len-1]==0)
                    {
                        count++;
                        vis[i+len-1]=1;
                    }
            }
        }
        printf("Case %d: %d\n",kase++,count);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值