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
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; char a[];
bool flag[];
int s1[];
typedef struct
{
int num;
int sum;
} Queue;
Queue q[],tmp;
int tom; int main()
{
int T,t;
int i,k,n,len,tail,head;
while(scanf("%d",&T)>)
{
getchar();
for(t=; t<=T; t++)
{
scanf("%s",a+);
n=strlen(a+);
len=n+n;
for(i=n+; i<=len; i++)
a[i]=a[i-n];
for( s1[]=,i=; i<=len; i++)
{
if(a[i]=='C') s1[i]=s1[i-]+;
else s1[i]=s1[i-]-;
}
memset(flag,false, sizeof(flag));
head=;
tail=-;
for(i=; i<=len-; i++)
{
tmp.sum=s1[i];
tmp.num=i;
while( head<=tail && q[tail].sum>tmp.sum ) tail--;
q[++tail]=tmp;
if( i>=n )
{
while( head<=tail && q[head].num+n<=i ) head++;
if( q[head].sum-s1[i-n]>= )
{
flag[ i-n+ ]=true;
// printf("%d ",i-n+1);
}
}
}
// printf("\n");
s1[]=;
for(i=; i<=len; i++)
{
k=len-i+;
if(a[k]=='C') s1[i]=s1[i-]+;
else s1[i]=s1[i-]-;
}
head=;tail=-;
for(i=; i<=len; i++)
{
tmp.sum=s1[i];
tmp.num=i;
while( head<=tail && q[tail].sum>tmp.sum ) tail--;
q[++tail]=tmp;
if( i>n )
{
while( head<=tail && q[head].num+n<=i ) head++;
if( q[head].sum-s1[i-n]>= )
{
flag[ n-(i-n)+ ]=true;
// printf("%d ",n-(i-n)+1);
}
}
}
// printf("\n");
for(tom=,i=; i<=n; i++)
if(flag[i]==true) tom++;
printf("Case %d: %d\n",t,tom);
}
}
return ;
}

hdu 3474 Necklace 单调队列的更多相关文章

  1. HDU 3401 Trade(单调队列优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3401 题意:炒股.第i天买入一股的价钱api,卖出一股的价钱bpi,最多买入asi股,最多卖出bsi股 ...

  2. Hdu 3410 【单调队列】.cpp

    题意: 给出一个数组,问你对于第i个数,从最后一个比它大的数到它之间比它小的数中最大的那个数的下标,以及它右边到第一个比它大的数中比它小的数中最大的那一个数的下标<下标从1开始>. eg: ...

  3. HDU 5749 Colmerauer 单调队列+暴力贡献

    BestCoder Round #84   1003 分析:(先奉上zimpha巨官方题解) 感悟:看到题解单调队列,秒懂如何处理每个点的范围,但是题解的一句算贡献让我纠结半天 已知一个点的up,do ...

  4. HDU 5289 Assignment(单调队列)

    题意:给T足数据,然后每组一个n和k,表示n个数,k表示最大同意的能力差,接下来n个数表示n个人的能力,求能力差在k之内的区间有几个 分析:维护一个区间的最大值和最小值,使得他们的差小于k,于是採用单 ...

  5. HDU - 3530 Subsequence (单调队列)

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. hdu 3530 Subsequence 单调队列

    题目链接 题目给出n个数, 一个下界m, 一个上界k, 让你求出最长的一段序列, 满足这段序列中的最大的数-最小的数<=k&&>=m, 输出这段长度. 可以维护两个队列, ...

  7. HDU - 6444(单调队列+思维)

    链接:HDU - 6444 题意:给出一个包含 n 个数的环,每个数都有一个价值,起点任选,每次跳顺时针跳 k 个数,在哪个数就能获得该价值(包括起点),最多取 m 次,问最少需要补充多少价值,所拿的 ...

  8. HDU 3507 PrintArticle (单调队列优化)

    题意:给出一个数列C,一个数字M,将数列分成若干段,每段的代价为(设这段的数字为k个): dp[i]=min(dp[j]+(sum[i]-sum[j])*(sum[i]-sum[j])+M) 若j1& ...

  9. hdu 6319 逆序建单调队列

    题目传送门//res tp hdu 维护递增单调队列 根据数据范围推测应为O(n)的. 我们需要维护一个区间的信息,区间内信息是"有序"的,同时需要在O(1)的时间进行相邻区间的信 ...

随机推荐

  1. PHP和JS中全局变量和局部变量

    一,PHP中全局变量和局部变量 php与C++中对全局变量和局部变量定义类似,全局变量:函数外定义的变量,在全局通用:局部变量:在函数内定义的变量,只在函数内有效.PHP中变量范围跨越了include ...

  2. 点击文字,把input type="radio"也选中

    本文原文地址:https://my.oschina.net/jack088/blog/469815 1. <label> <input type="radio" ...

  3. Azure 部署K8S(二)

    在"China Azure中部署Kubernetes(K8S)集群"一文中,我们使用的ACS Version及Kubernete Version版本都比较低,ACS Version ...

  4. spring与shiro配置详解

    1.加入shiro相关依赖的jar包 pom.xml部分内容如下: <dependency> <groupId>org.apache.shiro</groupId> ...

  5. [温故]图解java多线程设计模式(一)

    去年看完的<图解java多线程设计模式>,可惜当时没做笔记,导致后来忘了许多东西,打算再温习下这本书,顺便在这里记录一下~  1.顺序执行.并行.并发 顺序执行:多个操作按照顺序依次执行. ...

  6. centos7嵌入式环境搭建

    1. 在网上搜索下载交叉编译器arm-linux-gcc文件,我下载的是:arm-2014.05-29-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2 ...

  7. OpenERP how to set the tree view limit

    return { 'name':u'库存报表', 'view_type':'form', 'view_mode':'tree,form', 'res_model':'rainsoft.account. ...

  8. Mac下开机启动rc.common不生效的问题

    经过测试在10.12.6下/etc/rc.common不生效,原因是已经被launchd守护进程所取代,虽然保留着这个文件,但是基本是不起作用的. 如果要开机启动请直接使用launchd进行操作. 同 ...

  9. sql server 2008 R2 配置管理工具打不开

    使用 sql server 配置管理工具是报如下错误: 解决方法:   1 找出 sqlmgmproviderxpsp2up.mof 这个文件的位置   2 以管理员身份运行 mofcomp &quo ...

  10. (转)DB2和 Oracle的并发控制(锁)比较

    DB2和 Oracle的并发控制(锁)比较 牛 新庄2005 年 12 月 26 日发布 原文:https://www.ibm.com/developerworks/cn/data/library/t ...