zoukankan      html  css  js  c++  java
  • Necklace 单调队列

    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
    ***************************************************************************************************************************
    单调队列
    **************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cmath>
     6 using namespace std;
     7 char str[1500001];
     8 int num[1200001];
     9 int que[1200001];
    10 bool f1[2100001],f2[2100001];
    11 int sum[2100001],n,result;
    12 void  solve(bool t)//单调队列,求最优值
    13 {
    14     int head=1,tail=0;
    15     int it,jt;
    16     for(it=1;it<n;it++)
    17     {
    18         while(head<=tail&&sum[que[tail]]<=sum[it])tail--;
    19         tail++;
    20         que[tail]=it;
    21     }
    22     for(it=n;it<=2*n;it++)
    23     {
    24         while(head<=tail&&sum[que[tail]]<=sum[it])tail--;
    25         tail++;
    26         que[tail]=it;
    27         while(head<=tail&&que[head]<it-n)head++;
    28         if(sum[it]-sum[que[head]]>=0)
    29         {
    30             if(t)
    31               f1[it-n]=true;
    32             else
    33                f2[it-n]=true;
    34         }
    35 
    36     }
    37 }
    38 int main()
    39 {
    40     int cas,T=0,i,j;
    41     scanf("%d",&cas);
    42     while(cas--)
    43     {
    44         T++;
    45         scanf("%s",str+1);
    46         n=strlen(str+1);
    47         memset(f1,false,sizeof(f1));
    48         memset(f2,false,sizeof(f2));
    49         sum[0]=0;
    50         for(i=1;i<=n;i++)
    51         {
    52             if(str[i]=='C')
    53                 num[i]=1;
    54             else
    55                 num[i]=-1;
    56         }
    57         for(i=1;i<=n;i++)
    58          num[i+n]=num[i];
    59         for(i=1;i<=2*n;i++)
    60          sum[i]=sum[i-1]+num[i];
    61         solve(true);
    62         for(i=1;i<=n;i++)
    63          sum[i]=sum[i-1]+num[n+1-i];
    64         for(i=n+1;i<=2*n;i++)
    65           sum[i]=sum[i-1]+num[2*n+1-i];
    66         solve(false);
    67         result=0;
    68         for(i=0;i<n;i++)
    69          if(f1[i]||f2[n-i])
    70           result++;
    71         printf("Case %d: %d
    ",T,result);
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    JavaBean对象与Map对象互相转化
    PowerDesigner V16.5 安装文件 及 破解文件
    eclipse get set 自动添加注释
    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
    java poi excel操作 把第一列放到最后去
    java poi excel操作 下拉菜单 及数据有效性
    maven 项目编译失败
    关于TbSchedule任务调度管理框架的整合部署
    mat 使用 分析 oom 使用 Eclipse Memory Analyzer 进行堆转储文件分析
    tomcat启动问题,卡在 preparing launch delegate 100% 的解决方法
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3451706.html
Copyright © 2011-2022 走看看