Codeforces 496D Tennis Game continuous Two + enumeration

Source: Internet
Author: User
Tags integer numbers time limit

D. Tennis Game time limit per test 2 seconds memory limit per test megabytes input standard input output standard OUTP Ut

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 increasing SI, and for equal si-in the order of increasing ti. Examples 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

Main topic:

The result of the game known as the N-round tournament, 1 represents a victory, and 2 represents B victory.

A season has a total of S games, each match point is the T wheel. Show a game if one person wins first the T-wheel then this game is over, and the man who wins the T-wheel first is the winner of the game.

It is known that if a person wins the S game first, it ends all rounds of the race, that is, the game that ends before the N-round is illegal.

Now let you find the legal s,t, and follow the s increment output.


Ideas:


1, if the violence to engage in this problem, is O (n) enumeration T. then O (n) determines whether the test instructions is satisfied, so that a team wins the S-field, and at the end of the nth round the final victory, then corresponding to the t,s at this time is maintained.

So the total time complexity O (n^2); Obviously time-out is a complexity.

In fact, for a game, we know the beginning of this game, we can have a two-point round.

For each game to be maintained, then we need two consecutive points, reduce the complexity of time to O (N*LOGN*LONGN);


2, in the maintenance process, it is necessary to ensure that the game has been the N-wheel, and to ensure that the final round of the end of the same time, the winning team must be the ultimate winner. Pay attention to the draw is also not feasible scheme.


AC Code:


#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> using namespace
Std struct node {int x, y;}
E[150000];
int a[150000];
int sum[105000];
int sum2[105000];
int n;
    int CMP (node A,node b) {return a.x<b.x;} int slove (int tt) {int aa=0;
    int bb=0;
    int now=0;
        while (now<=n) {if (now==n) break;
        int ans=-1;
        int L=tt+now;
        int R=tt*2-1+now;
        R=min (R,n);
            while (r-l>=0) {int mid= (L+R)/2; if (sum[mid]-sum[now]>=tt| |
                SUM2[MID]-SUM2[NOW]&GT;=TT) {if (SUM[MID]-SUM[NOW]==TT) Ans=mid;
                if (SUM2[MID]-SUM2[NOW]==TT) Ans=mid;
            R=mid-1;
        } else l=mid+1;
        } if (ans==-1) return-1;
                else {if (SUM[ANS]-SUM[NOW]==TT) {aa++; if (ans==n) {if (AA&LT;=BB) return-1;
                }} if (Sum2[ans]-sum2[now]==tt) {bb++;
                if (ans==n) {if (BB&LT;=AA) return-1;
        }} Now=ans;
    }} int Tmp=max (AA,BB);
    if (AA==BB) return-1;
return TMP;
        } int main () {while (~SCANF ("%d", &n)) {memset (sum,0,sizeof (sum));
        memset (sum2,0,sizeof (sum2));
        for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
            for (int i=1;i<=n;i++) {int tmp=0;
            if (a[i]==1) tmp++;
            sum[i]=sum[i-1]+tmp;
            Tmp=0;
            if (a[i]==2) tmp++;
        sum2[i]=sum2[i-1]+tmp;
        } int tot=0;
            for (int t=1;t<=n;t++) {int ans=slove (t);
                if (ans!=-1) {E[tot].x=ans;
            e[tot++].y=t;
        }} sort (e,e+tot,cmp); printf ("%d\n ", tot);
        for (int i=0;i<tot;i++) {printf ("%d%d\n", e[i].x,e[i].y);
 }
    }
}











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.