Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1553    Accepted Submission(s): 453

Problem Description
You are given a necklace consists of N beads linked as a circle. Each bead is either crystal or jade.
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
 
Input
In the first line there is an integer T, indicates the number of test cases. (T<=50)
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.
 
Output
For each case, print “Case x: d” on a single line in which x is the number of case counted from one and d is the number of ways.
 
Sample Input
2
CJCJCJ
CCJJCCJJCCJJCCJJ
 
Sample Output
Case 1: 6
Case 2: 8
 
Author
love8909
 
Source
 
Recommend
zhengfeng   |   We have carefully selected several similar problems for you:  3473 3470 3471 3472 3475
 
 
 
 
解题分析:
  这个题为是快用了单调队列,把C当作1,J当作-1放到arr【】数组里,其中arr【n】=arr【n-m】,这里n>m,再维护一个sum【】数组,其中sum【i】表示arr【0】到arr【i】的和,然后构造单调队列,找出连续m个元素中的最小值,然后减去该连续m个元素的第1个元素的前面的一个元素的值,然后判断这个值的大小,如果小于0则不能在此处剪,否则能在此处剪。然后注意不同的方向用不同的sum数组。最后要注意不同方向比较时要在同一剪切点进行比较。为了节省空间arr【】数组开成m大小的。然后当数组过大时就把它构造成全局变量,这样就不是堆溢出了。最后,一定要细心,粗心大意害死人呐。。。他娘的。。
 
#include<iostream>
#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的更多相关文章

  1. HDU5730 Shell Necklace(DP + CDQ分治 + FFT)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...

  2. 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 ...

  3. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  4. HDU 3874 Necklace (树状数组 | 线段树 的离线处理)

    Necklace Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  5. USACO section1.1 Broken Necklace

    /* ID: vincent63 LANG: C TASK: beads */ #include <stdio.h> #include<stdlib.h> #include&l ...

  6. [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链

    [BZOJ1789][BZOJ1830][Ahoi2008]Necklace Y型项链 试题描述 欢乐岛上众多新奇的游乐项目让小可可他们玩的非常开心.现在他们正在玩比赛串项链的游戏,谁串的最快就能得到 ...

  7. POJ 1286 Necklace of Beads(Polya原理)

    Description Beads of red, blue or green colors are connected together into a circular necklace of n ...

  8. Accepted Necklace

    Accepted Necklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 2660 Accepted Necklace

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2660 Accepted Necklace Description I have N precious ...

  10. UVA 10054 The Necklace(欧拉回路,打印路径)

    题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

随机推荐

  1. ibernate 配置数据库方言

          在开发hibernate的程序时,需要进行SessionFactory的配置,简单地说,也就是建立与数据库之间连接的配置,在hibernate中一般使用xml文件来进行配置,但是在该文件的 ...

  2. git cherry-pick用法

    场景: 如果你的应用已经发布了一个版本2.0, 代码分支叫release-2.0, 现在正在开发3.0, 代码的分支叫dev-3.0. 那么有一天产品说, 要把正在开发的某个特性提前上线, 也就是说要 ...

  3. cmd enabledelayedexpansion

    先来说说变量延迟扩展吧.当然,放狗一搜,就能看到满天飞的关于变量延迟扩展的文章,所以,我这里就简单介绍一下.先来看一段批处理: set str=test if %str%==test ( set st ...

  4. https://developer.mozilla.org/

    Document/querySelector      https://developer.mozilla.org/zh-CN/docs/Web/API/Document/querySelector

  5. 4、注解反射生成SQL语句

    .任务说明         ①有一张用户表,字段包括:用户ID.用户名.昵称.年龄.性别.所在城市.邮箱.手机号:         ②使用java注解来对用户表的每个字段或字段的组合条件进行动态生成S ...

  6. test5

    ## 前言 因为vs2010没有集成mvvmlight 所以想要使用mvvmlight的relaycomman需要引用dll 需要测试某个功能的时候,不能进行快带的集成 ## 引用mvvmlight ...

  7. Windows XP with SP3大客户免激活日文版

    原贴地址:http://www.humin.com.cn/ja_windows_xp_professional_with_service_pack_3_x86_dvd_vl_x14-74058-iso ...

  8. Hook钩子编程

    钩子(Hook),是Windows消息处理机制的一个平台,钩子实际上是一个处理消息的程序段,通过系统调用,把它挂入系统,以监视指定窗口的某种消息.每当特定的消息发出,在没有到达目的窗口前,钩子程序就先 ...

  9. swift 移除所有子控件

    /// 移除所有子控件 func removeAllSubViews(){ if self.view.subviews.count>0{ self.view.subviews.forEach({ ...

  10. JSP标准标签库JSTL

    1.什么是JSTL? JSP标准标签库(JSP Standard Tag Library) 2.JSTL标准标签库中的常用标签 JSTL是JSP页面的标签库,实质上是一段Java代码.我们常用的是它的 ...