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 ...
随机推荐
- ibernate 配置数据库方言
在开发hibernate的程序时,需要进行SessionFactory的配置,简单地说,也就是建立与数据库之间连接的配置,在hibernate中一般使用xml文件来进行配置,但是在该文件的 ...
- git cherry-pick用法
场景: 如果你的应用已经发布了一个版本2.0, 代码分支叫release-2.0, 现在正在开发3.0, 代码的分支叫dev-3.0. 那么有一天产品说, 要把正在开发的某个特性提前上线, 也就是说要 ...
- cmd enabledelayedexpansion
先来说说变量延迟扩展吧.当然,放狗一搜,就能看到满天飞的关于变量延迟扩展的文章,所以,我这里就简单介绍一下.先来看一段批处理: set str=test if %str%==test ( set st ...
- https://developer.mozilla.org/
Document/querySelector https://developer.mozilla.org/zh-CN/docs/Web/API/Document/querySelector
- 4、注解反射生成SQL语句
.任务说明 ①有一张用户表,字段包括:用户ID.用户名.昵称.年龄.性别.所在城市.邮箱.手机号: ②使用java注解来对用户表的每个字段或字段的组合条件进行动态生成S ...
- test5
## 前言 因为vs2010没有集成mvvmlight 所以想要使用mvvmlight的relaycomman需要引用dll 需要测试某个功能的时候,不能进行快带的集成 ## 引用mvvmlight ...
- Windows XP with SP3大客户免激活日文版
原贴地址:http://www.humin.com.cn/ja_windows_xp_professional_with_service_pack_3_x86_dvd_vl_x14-74058-iso ...
- Hook钩子编程
钩子(Hook),是Windows消息处理机制的一个平台,钩子实际上是一个处理消息的程序段,通过系统调用,把它挂入系统,以监视指定窗口的某种消息.每当特定的消息发出,在没有到达目的窗口前,钩子程序就先 ...
- swift 移除所有子控件
/// 移除所有子控件 func removeAllSubViews(){ if self.view.subviews.count>0{ self.view.subviews.forEach({ ...
- JSP标准标签库JSTL
1.什么是JSTL? JSP标准标签库(JSP Standard Tag Library) 2.JSTL标准标签库中的常用标签 JSTL是JSP页面的标签库,实质上是一段Java代码.我们常用的是它的 ...