hdu 3474 Necklace 单调队列 DP

本文深入探讨了信息技术领域的核心内容,包括前端开发、后端开发、移动开发、游戏开发等细分技术领域的最新趋势和实践案例。同时,文章还涉及大数据开发、开发工具、嵌入式硬件与开发环境、音视频基础、AI音视频处理、测试、基础运维、DevOps、操作系统、云计算厂商、自然语言处理、区块链、隐私计算等多个关键领域,为读者提供了一站式的技术指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


//


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
/*首先计算从开始到i(0~2*len)点的C与J的数量之差sum[i]根据当
sum[i-len]<=Min(sum[j]|i-len<j<i)时符合题意要求*/
const int maxn=1000010;
char str[maxn*2];
int sum[maxn*2];//从1开始
int cnt;//answer
int len;
bool legal[2][maxn];
int Q[maxn*2];//单调队列
int I[maxn*2];//I[i]表示队列中的Q[i]在数组中的下标
void solve(int index)
{
    int n=strlen(str+1);
    sum[0]=0;
    for(int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]+(str[i]=='C'?1:-1);
    }
    int head=1,tail=0;
    for(int i=0;i<len;i++) //a[i]入队
    {
        while(head<=tail&&Q[tail]>=sum[i]) tail--;
        tail++;
        Q[tail]=sum[i],I[tail]=i;
    }
    for(int i=len;i<n;i++)
    {
        while(head<=tail&&Q[tail]>=sum[i]) tail--;
        tail++;
        Q[tail]=sum[i],I[tail]=i;
        while(I[head]<=i-len) head++;
        int rmin=Q[head];
        legal[index][i-len]=sum[i-len]<=rmin;
        //if(sum[i-len]<=rmin) cout<<index<<"..."<<i-len<<endl;
    }
    /*
    int head=1,tail=0;
    while(head<=tail&&Q[tail]>=a[i]) tail--;//a[i]入队
    tail++;
    Q[tail]=a[i],I[tail]=i;
    while(I[head]<=i-k) head++;//a[i-k]出队
    _min[i]=Q[head];//保存
    */
}
void _strev(char* str)
{
    for(int i=0,j=strlen(str)-1;i<j;i++,j--)
    {
        char t=str[i];
        str[i]=str[j];
        str[j]=t;
    }
}
int main()
{
    int ci,pl=1;scanf("%d",&ci);
    while(ci--)
    {
        scanf("%s",str+1);
        len=strlen(str+1);
        for(int i=len+1;i<=len+len;i++) str[i]=str[i-len];
        str[len+len+1]='\0';
        memset(legal,0,sizeof(legal));
        solve(0);
        _strev(str+1);
        solve(1);
        cnt=0;
        for(int i=0;i<len;i++)
        {
            if(legal[0][i]||legal[1][(len-i)%len])
            {
                cnt++;
            }
        }
        printf("Case %d: %d\n",pl++,cnt);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值