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. linux centos 7 下安装ElasticSearch5.4

    一. 把elasticsearch-5.4.0.rpm和kibana-5.4.0-x86_64.rpm上传到centos下/root目录中,如下图:二.进入centos目录/root,并用命令rpm ...

  2. byte转文件流 下载到本地

    此方法将byte类型文件转为文件流保存到本地 byte 经过BASE64Decoder 进行编码之后的类型 所以需要解码 防止出现乱码及文件损毁 /** * byte 转文件 下载到本地 * @par ...

  3. java学习笔记_接口

    接口:interface(关键字) public interface USB {} 1. 接口中都是抽象方法,方法前面的可见度(public.private)和抽象关键字(abstract)可以不写. ...

  4. Vue 不睡觉教程1-从最土开始

    目标最近在学习vue的过程中发现网上的vue教程总有些不同的问题,有的教程上来只说语法,有的教程上来就用vue-cli来建项目,但是vue-cli是整合了webpack等多个插件的工具,不利于我们学习 ...

  5. JavaIO流总结

    字节流 InputStream FileInputStream FilterInputStream BufferedInputStream DataInputStream PushbackInputS ...

  6. matplotlib画图无法显示图例 报错No handles with labels found to put in legend.

    很久没有matplotlib了,今天画图的时候发现了一个很小的问题....明明加了legend(),图表会出来,却无法正常显示图例.最后发现只要在plt.plot()加label图例就可以正常显示了.

  7. java回调方法之理解

    以前经常看见"回调方法(或回调函数)"一词,但是没有了解过是什么意思,更不知道用法.现在从网络上搜集了一些很好的资料,自己又整理一下,作为自己的笔记,也作为学习过程中的一个小脚印. ...

  8. GitLab 项目创建后地址由Localhost改为实际IP的方法

    进入终端修改以下文件即可. vim /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml ## GitLab settings git ...

  9. ruby gems列表

    https://github.com/shageman/cobradeps

  10. subset II

    Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...