Algorithm
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?
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.
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.
5
1 2 1 2 1
2
1 3
3 1
4
1 1 1 1
3
1 4
2 2
4 1
4
1 2 1 2
0
8
2 1 2 1 1 1 1 1
3
1 6
2 3
6 1
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int const N = 1e5 + 1;
int n, a[N];
vector<int> nxt[2];
vector<pair<int, int> > sol;
int check(int p) {
int i = -1, j = -1, wi = 0, wj = 0;
for(; i < int(nxt[0].size()) && j < int(nxt[1].size());) {
if(i == int(nxt[0].size()) - 1 && j == int(nxt[1].size()) - 1)
break;
if(i + p < nxt[0].size() && j + p < nxt[1].size() && nxt[0][i + p] < nxt[1][j + p])
i += p, j = upper_bound(nxt[1].begin(), nxt[1].end(), nxt[0][i]) - nxt[1].begin(), --j, ++wi;
else if(i + p < nxt[0].size() && j + p < nxt[1].size() && nxt[0][i + p] > nxt[1][j + p])
j += p, i = upper_bound(nxt[0].begin(), nxt[0].end(), nxt[1][j]) - nxt[0].begin(), --i, ++wj;
else if(i + p < nxt[0].size() && j + p >= nxt[1].size())
i += p, j = upper_bound(nxt[1].begin(), nxt[1].end(), nxt[0][i]) - nxt[1].begin(), --j, ++wi;
else if(i + p >= nxt[0].size() && j + p < nxt[1].size())
j += p, i = upper_bound(nxt[0].begin(), nxt[0].end(), nxt[1][j]) - nxt[0].begin(), --i, ++wj;
else
return -1;
}
if(wi == wj || (!nxt[0].empty() && !nxt[1].empty() &&
((wi > wj && nxt[0].back() < nxt[1].back()) ||
(wi < wj && nxt[0].back() > nxt[1].back()))))
return -1;
return max(wi, wj);
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", a + i);
nxt[a[i] - 1].push_back(i);
}
int o = 0, t = 0;
for(int i = 0; i < n; ++i) {
o += a[i] == 1;
t += a[i] == 2;
if(o != t) {
int res = check(max(o, t));
if(res > 0)
sol.push_back({res, max(o, t)});
}
}
sort(sol.begin(), sol.end());
sol.resize(unique(sol.begin(), sol.end()) - sol.begin());
printf("%d\n", int(sol.size()));
for(int i = 0; i < sol.size(); ++i)
printf("%d %d\n", sol[i].first, sol[i].second);
return 0;
}
Copy The Code &
Try With Live Editor
Input
1 2 1 2 1
Output
1 3
3 1
Demonstration
Codeforces Solution-Tennis Game-Solution in C, C++, Java, Python