天天看點

Codeforces 496D 想法

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

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 increasingsi, 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

input

8
2 1 2 1 1 1 1 1
      

output

3
1 6
2 3
6 1
      

題意:進行若幹場比賽,每次比賽兩人對決,赢的人得到1分,輸的人不得分,先得到t分的人獲勝,開始下場比賽,某個人率先赢下s場比賽時,遊戲結束。

現在給出n次對決的記錄,問可能的s和t有多少種,并按s遞增的方式輸出。

題解:n方T隻能說明頭硬

但是 n/1+n/2+...+n/n=nlogn

我們可以用這個公式進行優雅的暴力

記錄第i個1和2出現的位置

然後直接暴力比對過去 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int ans,a[100005],v[200005],p[200005];
struct node{
	int d,t;
	bool operator <(const node& a)const{
		return d<a.d;
	}
}e[100005];
vector<int>sp;
int main(){
	int n,i,j;
	scanf("%d",&n);
	for(i=1;i<=200004;i++)v[i]=p[i]=100004;
	for(i=1;i<=n;i++){
		scanf("%d",&a[i]);
		if(a[i]==1)v[++v[0]]=i;
		else p[++p[0]]=i;
	}
	for(i=1;i<=n;i++){
		int num1=0,num2=0,ch1=0,ch2=0,flag=1;
		int now=1;
		while(now<=n){
			if(v[num1+i]==100004&&p[num2+i]==100004){
				flag=0;
				break;
			}
			if(v[num1+i]<p[num2+i]){
				ch1++;
				num1+=i;
				num2=v[num1]-num1;
				now=v[num1]+1;
			}
			else{
				ch2++;
				num2+=i;
				num1=p[num2]-num2;
				now=p[num2]+1;
			}
		}
		if(ch1==ch2)flag=0;//平局
		if(ch1>ch2&&a[n]!=1)flag=0;//如果a赢但是最後一回合不是a赢
		if(ch1<ch2&&a[n]!=2)flag=0;//同上
		if(flag){
			e[++ans].d=max(ch1,ch2);
			e[ans].t=i;
		}
	}
	sort(e+1,e+1+ans);
	printf("%d\n",ans);
	for(i=1;i<=ans;i++)printf("%d %d\n",e[i].d,e[i].t);
	return 0;
}
           

繼續閱讀