Codeforces Round #283 (Div. 2) D. Tennis Game

185 篇文章 0 订阅
65 篇文章 0 订阅

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.

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


题意:给你一个比赛成绩的序列,问你可能的s 和 t 有几组。


分析:最后一场比赛的胜者一定是胜场为s的那个,一开始想到这点后就打算倒着推了,然后发现后边的情况不好确定。。。。正解是枚举t,然后顺着可以o(n/t)判断是否有符

合条件的s。


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int n,num,a[200001],f[3][200001],F[3][200002],tot[3];
struct thing
{
	int s,t;
	friend bool operator < (thing a,thing b)
	{
		if(a.s == b.s) return a.t < b.t;
		return a.s < b.s;
	}
} ans[100001];
int main()
{
	scanf("%d",&n);
	for(int i = 1;i <= n;i++)
	{
		scanf("%d",&a[i]);
		f[a[i]][i] = ++tot[a[i]];
		f[3-a[i]][i] = f[3-a[i]][i-1];
		F[a[i]][tot[a[i]]] = i;
	}
	for(int i = 1;i <= n;i++)
	{
		int now[3]={0,0,0},pos = 1;
		bool flag = false;
		while(true)
		{
			if(pos == n+1)
			{
				flag = true;
				break;
			} 
			if(F[a[pos]][f[a[pos]][pos] + i - 1] == 0 && F[3-a[pos]][f[3-a[pos]][pos] + i] == 0) break;
			if(F[a[pos]][f[a[pos]][pos] + i - 1] == 0 || F[a[pos]][f[a[pos]][pos] + i - 1] - pos + 1>= 2*i) pos = F[3-a[pos]][f[3-a[pos]][pos] + i] + 1;
			else pos = F[a[pos]][f[a[pos]][pos] + i - 1] + 1;
			now[a[pos-1]]++;
		}
		if(flag && now[a[n]] > now[3-a[n]])
		{
			ans[++num].s = now[a[n]];
			ans[num].t = i;
		}
	}
	sort(ans+1,ans+1+num);
	cout<<num<<endl;
	for(int i = 1;i <= num;i++)
	 cout<<ans[i].s<<" "<<ans[i].t<<endl;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值