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 ...
随机推荐
- 协同过滤 spark scala
1 http://www.cnblogs.com/charlesblc/p/6165201.html [转载]协同过滤 & Spark机器学习实战 2 基于Spark构建推荐引擎之一:基于物品 ...
- etcd ui
https://github.com/henszey/etcd-browser docker build --build-arg http_proxy=http://109.105.4.17:3128 ...
- Ansible Playbook Conditionals
通常,play的结果可能取决于变量的值,facts(有关远程系统的知识)或先前的任务结果. 在某些情况下,变量的值可能取决于其他变量. 此外,可以创建其他组,以根据主机是否与其他条件匹配来管理主机. ...
- IntelliJ IDEA SVN
第一步:下载svn的客户端,通俗一点来说就是小乌龟啦!去电脑管理的软件管理里面可以直接下载,方便迅速 下载之后直接安装就好了,但是要注意这里的这个文件也要安装上,默认是不安装的,如果不安装,svn中的 ...
- pidgin的未认证解决办法
安全验证打开还是无法登陆,并且手机无法验证. 解决:开启手机二次验证,给pidgin设置专门的登陆账户密码,即可解决.
- Python repr() 函数
Python repr() 函数 Python 内置函数 描述 repr() 函数将对象转化为供解释器读取的形式. 语法 以下是 repr() 方法的语法: repr(object) 参数 obje ...
- 85. Maximal Rectangle (Graph; Stack, DP)
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and ...
- python之socket运用1
先看下服务端的代码 import socket ip_bind = ("127.0.0.1",3000) sk = socket.socket() sk.bind(ip_bind) ...
- python之面向对象之反射运用
先看下hasattr和getattr在反射中的用法 import sys class apache(object): def __init__(self,tcp): self.tcp = tcp de ...
- springmvc使用list集合实现商品列表的批量修改
1将表单的数据绑定到List 1.1 需求 实现商品数据的批量修改. 1.2 需求分析 要想实现商品数据的批量修改,需要在商品列表中可以对商品信息进行修改,饼干且可以批量提交修改后的商品数据. 1.3 ...