cf_496 D-tennis Game (two points)

Source: Internet
Author: User
Tags bool integer numbers rounds sort 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 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 to play tennis, win one point, each game has a T, a win after the score will be refreshed, when someone won the s innings after the end of the game; now enter n number, indicating the score of two people in a game, 1 means Petya, 2 means Gena. It is required to output all possible scoring rounds based on this set of scores, in order of increments of S and s equal to T.

The following:

The key to this problem is to understand the good test instructions, the topic requires the output of all possible turn system, we can be 1 to the maximum score enumeration all cases, and then each time to determine whether to meet the rules of the game.

Note the point:

For the processing of the score is the key, if the time to traverse the complexity of O (n*n), must be timed out, so you can enter the score directly to the current everyone's total score storage, so you can use two points in each judgment to find, positioning the first to meet the score of the person, the Victory plus one. When the game is over, the score will be refreshed, so at the end of each inning, the two will be synchronized to the winner's current position, then the next operation.

not satisfied with the situation of test instructions:

1. In the T-system calculation, the end of the end is not filled with a whole board;

2.winner is not the same as the person with the highest score;

3. Two people scored the same;


Code implementation:

<span style= "FONT-SIZE:14PX;" > #include <iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm> #include

<cstring> #include <vector> using namespace std;
    struct option{int s;
    int t; BOOL operator < (const option &next) const {if (THIS-&GT;S==NEXT.S) return This->t < n
        ext.t;
    else return This->s < Next.s;
}};//defines the structure to store a round system that meets the conditions, overloading ' < ' to facilitate post sort sorting vector<int> num1;//store ' 1 ' total points vector<int> num2;//store ' 2 ' gross score
vector<option> olis;//storage int n,sum1,sum2;
int winner;//marks the winner (who get the key point) with the bool Solve (int);
    int main () {scanf ("%d", &n); int A;
    sum1=0;sum2=0;
        for (int i=0;i<n;i++) {scanf ("%d", &a);
        if (a==1) sum1++;
        else sum2++;
        if (i==n-1) winner=a;
        Num1.push_back (SUM1);
    Num2.push_back (SUM2);
   } int sum=sum1>sum2?sum1:sum2; for (int i=1;i<=sum;i++)//from 1 to Maximum score enumeration solve (i);
    Sort (Olis.begin (), Olis.end ());
    printf ("%d\n", Olis.size ());
    for (int i=0;i< (int) olis.size (); i++) printf ("%d%d\n", olis[i].s,olis[i].t);
return 0; 
    } bool Solve (int x) {int tmp1,tmp2;//stores the first position that reaches the specified fraction int pos1,pos2;//storage 1, 2 current actual score int score1,score2;//storage 1, 2 won rounds
    BOOL Flag=true;
    pos1=0;pos2=0;
    score1=0;score2=0; for (int i=0;i<n;i++) {//two points find the earliest meeting score of the Turn Tmp1=lower_bound (Num1.begin (), Num1.end (), pos1+x)-num1.begin (
        );
        Tmp2=lower_bound (Num2.begin (), Num2.end (), pos2+x)-num2.begin (); if (tmp1!=n| |
                TMP2!=N)//must jump out of the loop {if (TMP1&LT;TMP2)//1 first satisfies the condition {score1++, when both people have reached the highest score;
                Sync Score: Position the two people's current score to the same location POS1=NUM1[TMP1];
            POS2=NUM2[TMP1];
                } else {score2++;
                POS1=NUM1[TMP2];
           POS2=NUM2[TMP2]; }} else//two people at the same time to achieve the highest score {tmp1=tmp2=n-1; At this point, the end is still not finished. if (num1[tmp1]!=pos1| |
            NUM2[TMP2]!=POS2) Flag=false;
        Break }} if (flag) {//does not meet test instructions condition if ((winner==1&&score2>score1) | | (Winner==2&&score1>score2) | |
            (Score1==score2))
        return false;
        option tem;
        tem.s=score1>score2?score1:score2;
        Tem.t=x;
        Olis.push_back (TEM);
    return true;
} return false;
 }</span>


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.