CF 496D
D. 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.
大意:
一场乒乓球比赛有 s 局, 每局 t 回合组成. 给出两个人的在一局内的胜负情况..... 先赢 t 回合赢下这一局, 先赢 s 回合赢下这场比赛.
给出两个人的胜负情况(刚好记录到结束回合为止). 请你求出可能的 s,t.
思路:
给一个提示吧.... ∑ n + n/2 + n/3 + n/4 + .... = O(nlogn).
所以我们每局回合数,关键就在于求出下一局开始的位置...记录前缀,二分能在 O(logn)的时间内找到位置.
所以总的复杂度为 O(nlog2n).
1 #include<cstdlib> 2 #include<cstdio> 3 #include<algorithm> 4 #include<vector> 5 #include<iostream> 6 using namespace std; 7 const int maxn = (int)1.5e5; 8 int n; 9 int match[maxn],prefix[maxn][3]; 10 vector<pair<int,int> >v; 11 int gets(int l,int r,int t){ 12 return prefix[r][t] - prefix[l][t]; 13 } 14 int getnext(int &pos,int s){ 15 int l = pos + 1, r = n,cond[3] = {},tp = pos,mid; 16 while(l <= r){ 17 mid = (l + r) >> 1; 18 cond[1] = gets(pos,mid,1); cond[2] = gets(pos,mid,2); 19 if(cond[1] >= s || cond[2] >= s) r = mid - 1; 20 else l = mid + 1; 21 } 22 pos = l; 23 if(l > n) return -1; 24 else return gets(tp,pos,1) > gets(tp,pos,2) ? 1 : 2; 25 } 26 void check(int s){ 27 int pos = 0,cond[3] = {},t; 28 while( t = getnext(pos,s), t != -1){ 29 cond[t]++; 30 if(pos == n) break; 31 } 32 if(t == -1) return; 33 if(cond[1] != cond[2] && cond[t] == max(cond[1],cond[2])) v.push_back(make_pair(max(cond[1],cond[2]),s)); 34 } 35 int main() 36 { 37 freopen("D.in","r",stdin); 38 freopen("D.out","w",stdout); 39 ios::sync_with_stdio(false); 40 cin >> n; 41 for(int i = 1; i <= n; ++i){ 42 cin >> match[i]; 43 prefix[i][match[i]]++; 44 } 45 for(int i = 2; i <= n; ++i){ 46 prefix[i][1] += prefix[i-1][1]; 47 prefix[i][2] += prefix[i-1][2]; 48 } 49 for(int i = n; i >= 1; --i) check(i); 50 sort(v.begin(),v.end()); 51 cout << v.size() << endl; 52 for(int i = 0; i < (int)v.size(); ++i) cout << v[i].first << " " << v[i].second << endl; 53 return 0; 54 }