HDU 3474 Necklace(单调队列,好题)

Necklace

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


 

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

题意:给你一个圆环手链,任给你某个割点切成一条链后的珠子情况(C表示水晶珠,J表示玉珠),问你有多少个切割点满足从这个切割点往左或者往右依次收集所有的珠子,收集过程中手中的水晶珠数量不少于玉珠。

受HDU 4193的启发,把水晶珠看成1,玉珠看成-1,就是有多少个切点满足从这个切点左右方向中有一个方向n个珠子的和大于等于0。于是可以利用前缀和做两遍单调队列,向HDU4193那样就行。

以为这样就完了???

错!!!

本题有很多坑点:

1、数组开多了会MLE,事实证明开四个2e6数组已经快到极限了。(刚好不超,所以有的数组要重复利用)

2、本题是求合法的切割点数量,所以做两遍单调队列时要标记合法的切点,最后再统一答案。(此处WA了无数发

3、标记的时候,一定要注意两遍的不同,这样才能统一切割点的下标。我用fg[i]表示在第i-1个珠子和第i个珠子之间切割开。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=2e6+50;
int n,m,k;
int tail,head;
int stk[maxn];
int ans;
int a[maxn];
int c[maxn];
char s[maxn];
bool jd[maxn];
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    getchar();
    while(T--)
    {
        memset(jd,0,sizeof(jd));
        scanf("%s",s+1);
        n=strlen(s+1);
        for(int i=1;i<=n;i++)
        {
            a[i]=(s[i]=='C')?1:-1;
            a[n+i]=a[i];
        }
        c[0]=0;
        for(int i=1;i<=2*n;i++)
        {
            c[i]=c[i-1]+a[i];
        }
        ans=0;
        head=-1;tail=0;
        for(int i=1;i<2*n;i++)
        {
            while(head>=tail&&c[i]<c[stk[head]]) head--;
            stk[++head]=i;
            while(head>tail&&i-stk[tail]>=n) tail++;
            if(i>=n&&c[stk[tail]]-c[i-n]>=0) jd[i-n+1]=1;
        }
        c[0]=0;
        for(int i=1;i<=2*n;i++)
        {
            int k=n+n-i+1;
            c[i]=c[i-1]+a[k];
        }
        head=-1;tail=0;
        for(int i=1;i<=2*n;i++)// i<=2*n
        {
            while(head>=tail&&c[i]<c[stk[head]]) head--;
            stk[++head]=i;
            while(head>tail&&i-stk[tail]>=n) tail++;
            if(i>n&&c[stk[tail]]-c[i-n]>=0) jd[n-(i-n)+1]=1;//i>n 注意正反切割地点的下标要一致
        }
        for(int i=1;i<=n;i++)
        if(jd[i]) ans++;
        printf("Case %d: %d\n",cas++,ans);
    }
    return 0;
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值