zoukankan      html  css  js  c++  java
  • cf496D Tennis Game

    Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.

    To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.

    Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?

    Input

    The first line contains a single integer n — the length of the sequence of games (1 ≤ n ≤ 105).

    The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena.

    It is not guaranteed that at least one option for numbers s and t corresponds to the given record.

    Output

    In the first line print a single number k — the number of options for numbers s and t.

    In each of the following k lines print two integers si and ti — the option for numbers s and t. Print the options in the order of increasing si, and for equal si — in the order of increasing ti.

    Example

    Input
    5
    1 2 1 2 1
    Output
    2
    1 3
    3 1
    Input
    4
    1 1 1 1
    Output
    3
    1 4
    2 2
    4 1
    Input
    4
    1 2 1 2
    Output
    0
    Input
    8
    2 1 2 1 1 1 1 1
    Output
    3
    1 6
    2 3
    6 1

    倍增预处理下每个数字往后2^k是哪,然后就可以logn的知道往后走n步是哪。

    枚举每一个可能的“小分”,然后直接模拟下往后走。可以logn的时间知道1和2那个先到。

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<algorithm>
      6 #include<cmath>
      7 #include<queue>
      8 #include<deque>
      9 #include<set>
     10 #include<map>
     11 #include<ctime>
     12 #define LL long long
     13 #define inf 0x7ffffff
     14 #define pa pair<int,int>
     15 #define mkp(a,b) make_pair(a,b)
     16 #define pi 3.1415926535897932384626433832795028841971
     17 using namespace std;
     18 inline LL read()
     19 {
     20     LL x=0,f=1;char ch=getchar();
     21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
     22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
     23     return x*f;
     24 }
     25 int n;
     26 int a[100010];
     27 int s1[100010],s2[100010];
     28 int go1[100010][20],go2[100010][20];
     29 int bin[1048577];
     30 int lst1,lst2,anst;
     31 struct aaa{int x,y;}ans[100010];
     32 bool operator <(aaa a,aaa b){return a.x<b.x;}
     33 inline int lowbit(int x){return x&(-x);}
     34 inline int calc(int s,int k,int op)
     35 {
     36     if (s==-1)return s;
     37     while (k)
     38     {
     39         if (op==1)s=go1[s][bin[lowbit(k)]];
     40         else s=go2[s][bin[lowbit(k)]];
     41         if (!s)break;
     42         k-=lowbit(k);
     43     }
     44     return s==0?-1:s;
     45 }
     46 int main()
     47 {
     48     for (int i=0;i<20;i++)bin[1<<i]=i;
     49     n=read();
     50     for (int i=1;i<=n;i++)a[i]=read();
     51     if (a[n]==2)for (int i=1;i<=n;i++)a[i]=3-a[i];
     52     for (int i=1;i<=n;i++)
     53     {
     54         s1[i]=s1[i-1]+(a[i]==1);
     55         s2[i]=s2[i-1]+(a[i]==2);
     56     }
     57     for (int i=n;i>=1;i--)
     58     {
     59         go1[i][0]=lst1;
     60         go2[i][0]=lst2;
     61         if (a[i]==1)lst1=i;
     62         else lst2=i;
     63     }
     64     for (int i=1;i<=20;i++)
     65     {
     66         if(i>n)break;
     67         for (int j=1;j<=n;j++)
     68         {
     69             if (go1[j][i-1])go1[j][i]=go1[go1[j][i-1]][i-1];
     70             if (go2[j][i-1])go2[j][i]=go2[go2[j][i-1]][i-1];
     71         }
     72     }
     73     go1[0][0]=lst1;
     74     go2[0][0]=lst2;
     75     for (int i=1;i<=20;i++)
     76     {
     77         if (i>n)break;
     78         if (go1[0][i-1]!=0)go1[0][i]=go1[go1[0][i-1]][i-1];
     79         if (go2[0][i-1]!=0)go2[0][i]=go2[go2[0][i-1]][i-1];
     80     }
     81     for (int i=1;i<=n;i++)
     82     {
     83         int cnt1=0,cnt2=0,now=0,nx1,nx2,mrk=0;
     84         while (now!=-1&&now<n)
     85         {
     86             nx1=calc(now,i,1);
     87             nx2=calc(now,i,2);
     88             if (nx1==-1&&nx2==-1){mrk=1;break;}
     89             if (nx1==-1)cnt2++,now=nx2;
     90             else if (nx2==-1)cnt1++,now=nx1;
     91             else
     92             {
     93                 if (nx1<nx2)
     94                 {
     95                     cnt1++;now=nx1;
     96                     if (now==n)break;
     97                 }else
     98                 {
     99                     cnt2++;now=nx2;
    100                 }
    101             }
    102         }
    103         if (mrk)continue;
    104         if (!cnt1&&!cnt2)break;
    105         if (cnt1>cnt2)ans[++anst].y=i,ans[anst].x=cnt1;
    106     }
    107     sort(ans+1,ans+anst+1);
    108     printf("%d
    ",anst);
    109     for (int i=1;i<=anst;i++)printf("%d %d
    ",ans[i].x,ans[i].y);
    110 }
    cf496D
  • 相关阅读:
    《Centos服务器版安装教程》
    从CentOS官网下载系统镜像详细教程
    一键LNMP文件
    Centos 7 ip地址
    cmd常用命令
    bat命令
    JAVA学习资源整理
    DevOps 高效 shell 命令
    编程范式:命令式编程(Imperative)、声明式编程(Declarative)和函数式编程(Functional)
    Java 中的函数式编程(Functional Programming):Lambda 初识
  • 原文地址:https://www.cnblogs.com/zhber/p/7284611.html
Copyright © 2011-2022 走看看