Necklace
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1553 Accepted Submission(s): 453
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
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.
CJCJCJ
CCJJCCJJCCJJCCJJ
Case 2: 8
#define N 2000010
using namespace std;
int arr[1000010];
int sum[N];
int que[N];
int ok1[N],ok2[N];
void main()
{
int n,c=0;
cin>>n;
getchar();
while(n--)
{
c++;
int all=0;
char temp;
int m=0;
while(temp=getchar())
{
if(temp=='C')
arr[m]=1;
else if(temp=='J')
arr[m]=-1;
else break;
m++;
}
sum[0]=0;
for(int i=1;i<=m;i++)
sum[i]=sum[i-1]+arr[i-1];
for(i=m+1;i<=2*m;i++)
sum[i]=sum[m]+sum[i-m];
int start=0 , tail=0;
for(i=m*2;i>0;i--)
{
while(start<tail && sum[que[tail-1]]>sum[i]) tail--;
que[tail++]=i;
while(start<tail && que[start]-i>m-1) start++;
ok1[i]=sum[que[start]]-sum[i-1];
}
sum[2*m]=0;
for(i=2*m;i>m;i--)
sum[i-1]=sum[i]+arr[i-m-1];
for(i=m-1;i>=0;i--)
sum[i]=sum[m]+sum[m+i];
start=tail=0;
for(i=0;i<m*2;i++)
{
while(start<tail && sum[que[tail-1]]>sum[i]) tail--;
que[tail++]=i;
while(start<tail && i-que[start]>m-1) start++;
ok2[i]=sum[que[start]]-sum[i+1];
}
for(i=m;i<m*2-1;i++)
if(ok2[i]>=0 || ok1[i-(m-2)]>=0) all++;
if(ok1[1]>=0 || ok2[2*m-1]>=0) all++;
cout<<"Case "<<c<<": "<<all<<endl;
}
}
Necklace的更多相关文章
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 2016 Multi-University Training Contest 1 H.Shell Necklace
Shell Necklace Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
- HDU 3874 Necklace (树状数组 | 线段树 的离线处理)
Necklace Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- USACO section1.1 Broken Necklace
/* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...
- [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链
[BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...
- POJ 1286 Necklace of Beads(Polya原理)
Description Beads of red, blue or green colors are connected together into a circular necklace of n ...
- Accepted Necklace
Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 2660 Accepted Necklace
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...
- UVA 10054 The Necklace(欧拉回路,打印路径)
题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- Android中app卡顿原因分析示例
在知乎回答了一个“为什么微博的app在iPhone比Android上流畅”的问题.后面部分是一个典型的动画卡顿的性能分析过程,因此帖在这里.有编程问题可以在这里交流.知乎链接. =========== ...
- Numpy随机数
Numpy随机数 np.random随机数子库 1: 基本函数 .rand(d0,d1,..dn):创建d0-dn维度的随机数数组,浮点数,范围从0-1,均匀分布 .randn(d0,d1,..dn) ...
- python生成器(转)
生成器是一种特殊的迭代器,内部支持了生成器协议,不需要明确定义__iter__()和next()方法.生成器通过生成器函数产生,生成器函数可以通过常规的def语句来定义,但是不用return返回,而是 ...
- javascritp伪协议
[javascritp伪协议] 将javascript代码添加到客户端的方法是把它放置在伪协议说明符javascript:后的URL中.这个特殊的协议类型声明了URL的主体是任意的javascript ...
- django1.8模板位置的设置setting.py
大多数django教程比较老,给出的template的设置方案为: 更改工程下的setting.py文件, TEMPLATE_DIRS = ( os.path.join( APP_DIR, ' ...
- tag-SMASS-1
SMASS 是在vasp的DFTMD中决定着系综的类型,在手册中给出的该参数具体信息如下: SMASS = -3 | -2 | -1 | [real] ≥ 0 Default: SMASS = -3 ...
- Array Product(模拟)
Array Product http://codeforces.com/problemset/problem/1042/C You are given an array aa consisting o ...
- Loitor_产品(一)
源码:https://github.com/loitor-vis/vi_sensor_sdk 注意:以下要一直在管理员权限 1.C++ 示例程序的编译步骤 先确认你的系统已经成功安装了OpenCV. ...
- [z]kafka相关资料
http://my.oschina.net/ielts0909/blog/93190 http://www.iteye.com/magazines/107 http://blog.csdn.net/h ...
- How to add libraries to “External Libraries” in WebStorm/PhpStorm/Intellij
Stack Overflow Questions Developer Jobs Tags Users Log In Sign Up Join Stack Overflow to learn, sh ...