Codeforces 496D Tennis Game (enum)

Source: Internet
Author: User
Tags integer numbers printf time limit

Topic links

Tennis Game time limit per test 2 seconds memory limit per test megabytes input standard input output standard output

Petya and Gena love playing table tennis. A single match was played according to the following Rules:a match consists of multiple sets, each set consists of MULTIPL E serves. Each serve was 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 is being set to 0. As soon as one of the players wins the total of S sets, he wins the match and the match are over. Here s and T is 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 are, for each serve they write down the winner. Serve winners is 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 are over as soon as one of the play ERS wins s sets.

Petya and Gena has found a record of an old match. Unfortunately, the sequence of serves in the record isn ' t divided to sets and numbers s and T for the given match is Al So lost. The players now wonder what values of S and T might be. Can 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 is won by Petya, if AI = 2, then the i-th serve is won by Gena.

It is not guaranteed, at least one, option for numbers s and T corresponds to the given record. Output

In the first, line print a, k-the number of options for numbers s and T.

In each of the following K lines print, integers si and ti-the option for numbers s and T. Print, the options in the O Rder of Increasingsi, and for equal si-in the order of increasing ti. Sample Test (s) 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

Test instructions: Two people play ping-pong, every ball is known who wins, but do not know how many goals a Bureau win (set as T), do not know how many innings (set as s) to win the final victory. Output all possible s and T by dictionary order.

Puzzle: Enumerate t,1<=t<=n. For each of the enumerated T, then O (n) sweep again, to determine whether it is feasible and to find the corresponding S. This complexity is obviously not going to work. But we can preprocess each 1 position and each 2 position. This will allow O (1) to cite each inning, without O (n) sweep it, but up to O (n/t) sweep over again.

The complexity is O (n 1+1/2+1/3+ .... 1/n)) is approximately equal to O (NLGN).

The code is as follows:

#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include < string> #include <queue> #include <stack> #include <map> #include <set> #include <stdlib.h
> #include <vector> #define INFF 0x3fffffff #define NN 110000 #define MOD 1000000007 typedef long Long LL;
Const LL inf64=inff* (LL) Inff;
using namespace Std;
int n;
int YI[NN],ER[NN];
Vector<pair<int,int> >ans;
int A[NN];
int F1[NN],F2[NN];
    int main () {int i,x;
    int i1,i2,j;
        while (scanf ("%d", &n)!=eof) {i1=i2=0;
        Ans.clear ();
            for (i=1;i<=n;i++) {scanf ("%d", &a[i]);
            X=a[i];
            if (x==1) {yi[++i1]=i;
        } else er[++i2]=i;
        } f1[n+1]=i1+1;
        f2[n+1]=i2+1;
        int IX,FC;
        Ix=i1,fc=i2;
   for (i=n;i>=1;i--) {if (a[i]==1) {             f1[i]=ix--;
            F2[I]=F2[I+1];
                } else {f2[i]=fc--;
            F1[I]=F1[I+1];
        }} int fi,se;
        int one,two;
            for (i=1;i<=n;i++) {fi=se=0;
            Ix=fc=1;
            for (j=1;j<=n;)
                {if (ix+i-1>i1) One=inff;
                else one=yi[ix+i-1];
                if (FC+I-1&GT;I2) Two=inff;
                else two=er[fc+i-1];
                if (ONE==INFF&AMP;&AMP;TWO==INFF) break;
                    if (one<two) {j=one+1;
                    fi++;
                    Ix+=i;
                FC=F2[J];
                    } else {j=two+1;
                    se++;
                    Fc+=i;
     IX=F1[J];           }} if (j==n+1) {if (one<two&&fi>se)
                {Ans.push_back (Make_pair (fi,i));
                } if (Two<one&&se>fi) {ans.push_back (Make_pair (se,i));
        }}} int la=ans.size ();
        Sort (Ans.begin (), Ans.end ());
        printf ("%d\n", LA);
        for (i=0;i<la;i++) {printf ("%d%d\n", ans[i].first,ans[i].second);
}} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.