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 ...
 
随机推荐
- Python使用Threading模块创建线程
			
使用Threading模块创建线程,直接从threading.Thread继承,然后重写__init__方法和run方法: #!/usr/bin/python # -*- coding: UTF-8 ...
 - springmvc获取资源文件的两种方式(超简单)
			
1 比如我们在sc目录下新建一个db.properties文件内容如下 DriverClass=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306 ...
 - Javaweb连接数据库
			
在JSP中使用JDBC驱动连接mysql数据库. 1: 下载mysql的Java连接程序 2: 解压目录下的mysql-connector-java-5.0.24-bin.jar文件就是连接MySql ...
 - 求数组中的逆序对的数量----剑指offer36题
			
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数: 如数组{7,5,6,4},逆序对总共有5对,{7,5},{7,6},{7, ...
 - CentOS7使用yum详细搭建zabbix3.2过程
			
本文引用于:http://blog.csdn.net/s3275492383/article/details/62417210 一.准备搭建环境: 1.系统:CentOS7 2.默认有使用linux服 ...
 - linux下字符串的比较方式
			
A="$1" B="$2" #判断字符串是否相等 if [ "$A" = "$B" ];then echo &qu ...
 - 部分PR回写的数量带有小数,分别是2023工厂的纸箱104007000389,2021工厂的纸盒404002005930;
			
描述:部分PR回写的数量带有小数,分别是2023工厂的纸箱104007000389,2021工厂的纸盒404002005930: 原因:所有物料规划PR时对舍入值的先后考虑逻辑影响到回写出来的temp ...
 - cloudrea manager 调整datanode数据存储目录
			
由于datanode所需磁盘空间较大,所以工作中可能会涉及到给datanode增加磁盘目录或者更改数据目录 CM停止该datanode节点 CM页面增加目录或者修改目录 如果是修改目录的话 需要将服务 ...
 - android显示和隐藏软键盘
			
显示键盘: EditText editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.request ...
 - maven的配置及仓库的配置
			
1.maven的配置 1.1.注意:电脑上需要安装jdk. 1.2.配置MAVEN_HOME,再在path中配置到bin这一层. (1)配置MAVEN_HOME:我的电脑--->右击---> ...