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. python 将列表嵌套字典的unicode字符串转换为str格式的字符串的方法

    今天在进行django开发的过程中遇到了一个非常棘手的问题, 因为需求原因, 需要将一份数据存为json格式到数据库中, 如下面这种格式: list_1 = [{"name":&q ...

  2. linux将指令加入开机启动或加入环境变量

    以mongodb运行指令为例,/usr/local/webserver/mongodb/bin/mongo 1,linux将指令加入环境变量PATH 简单说PATH就是一组路径的字符串变量,当你输入的 ...

  3. SpringCloud学习笔记(一)——基础

    什么是微服务架构 简单地说,微服务是系统架构上的一种设计风格,它的主旨是将一个原本独立的系统拆分成多个小型服务,这些小型服务都在各自独立的进程中运行,服务之间通过基于HTTP的RESTful API进 ...

  4. java_对象序列化

    对象序列化(serializable) 序列化读:ObjectInputStream  ois=new ObjectInputStream(new FileInputStream("./gg ...

  5. 使用TortoiseSVN客户端管理IntelliJ IDEA项目的方法

    使用TortoiseSVN客户端管理IntelliJ IDEA项目的方法 今天在打开一个使用SVN管理的项目时,IntelliJ IDEA提示: Can't use Subversion comman ...

  6. 功能一: 数据库访问DAO层方法定义

    功能1: 今天到现在为止 实战课程的访问量 yyyyMMdd courseID 使用数据库来进行存储我们的统计结果 Spark Streaming把统计结果写入到数据库里面 可视化前端根据: yyyy ...

  7. Scrum Meeting 汇总

    Alpha [Alpha]Scrum Meeting 0&1 [Alpha]Scrum Meeting 2 [Alpha]Scrum Meeting 3 [Alpha]Scrum Meetin ...

  8. E - Guess the Root 拉格朗日差值法+交互

    题目传送门 题意:告诉你存在一个未知项系数最高为10的$f(x)$,你最多可以有50次询问,每次询问给出一个$x'$,系统会返回你$f(x')$的值,你需要猜一个$x''$,使得$f(x'')=0$, ...

  9. hiho#1449 重复旋律6 求长度为k的串最大次数 后缀自动机

    题目传送门 题目大意:求长度为k的串的最大次数,把k从1到length的所有答案全部输出. 思路: 这道题放在$SAM$里就是求长度$k$对应的所有$right$集中最大的大小. 我们以$aabab$ ...

  10. SpringMVC 的 切面

    官网路径:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans 一:术语介绍 通知 ...