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
  • 相关阅读:
    【数据库领域】mysql中in与or对比
    数据库优化
    数据库-索引
    数据库-事务
    数据库-视图
    数据库设计
    数据库编程提高
    数据库高级操作
    数据库基本操作
    with-上下文管理器
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3451706.html
Copyright © 2011-2022 走看看