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 ...
随机推荐
- China cuts bank reserves by $100m to cushion US tariffs
China cuts bank reserves by $100m to cushion US tariffs中国央行定向降准释放千亿美元资金China is cutting the amount o ...
- [转]被玩坏的innerHTML、innerText、textContent和value属性
一.前言 由于innerText并非W3C标准属性,因此我们无法在FireFox中使用它,一般情况下我们可以使用textContent来代替,但它两者是否就能完全等同呢?在坑爹的表单元素(如input ...
- 吴裕雄 实战python编程(1)
import sqlite3 conn = sqlite3.connect('E:\\test.sqlite') # 建立数据库联接cursor = conn.cursor() # 建立 cursor ...
- django中怎么使用mysql数据库的事务
Mysql数据库事务: 在进行后端业务开始操作修改数据库时,可能会涉及到多张表的数据修改,对这些数据的修改应该是一个整体事务,即要么一起成功,要么一起失败. Django中对于数据库的事务,默认每执行 ...
- HTTP Response Code 中文详解
引自:https://blog.csdn.net/lplj717/article/details/70053560 1xx - 信息提示这些状态代码表示临时的响应.客户端在收到常规响应之前,应 ...
- hdoj1087 (DP--LIS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087 思路:这题很简单了,纯LIS的解法,没有一点变形,由于数据小,使用O(n^2)LIS解法就足够了 ...
- zoj1109-Language of FatMouse 【字典树】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 Language of FatMouse Time Limit: 10 S ...
- Treasures and Vikings(两次搜索)
Treasures and Vikings https://www.luogu.org/problemnew/show/P4668 题意翻译 你有一张藏宝图,藏宝图可视为 N×MN×M 的网格.每个格 ...
- Python反转
1切片 s="svdfbffdbdf" a=s[::-1] 2入栈出栈 入栈之后再出栈正好就是了 3reverse 这个函数是列表的....你要先把str转成list list-& ...
- 最近公共祖先 · Lowest Common Ancestor
[抄题]: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. “Th ...