首页 > 代码库 > CF 496D(Tennis Game-O(t*(n/t)复杂度+vector排序)

CF 496D(Tennis Game-O(t*(n/t)复杂度+vector排序)

D. 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 increasing si, 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

本题考试使就是想不出非暴力算法。

考完后得知暴力的复杂度是O(t*(n/t))=O(n) ..我一直以为是O(n^2)...

我说为什么有人5minAC,,说多了都是泪


解法:

显然t确定s就确定了,暴力枚举t,然后扫一遍,记得先预处理使得能在O(1)查找1局比赛


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<vector>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,n1=0,n2=0;
int f[MAXN],g[MAXN]; // f/g win'game num
int a[MAXN],b[MAXN]; // f/g win f[i] game until ith game end

vector<pair<int,int> > ans;
int main()
{
//	freopen("Tennis.in","r",stdin);
//	freopen(".out","w",stdout);
	MEMI(f) MEMI(g)
	cin>>n;
	For(i,n)
	{
		int t;
		scanf("%d",&t);
		if (t&1) f[++n1]=i;
		else g[++n2]=i;
		a[i]=n1,b[i]=n2;
	}
	For(t,n)
	{
		bool flag=0;
		int s1=0,s2=0,cur1=0,cur2=0;
		for(int cur=0;cur<=n;)
		{
			int x,y;
			if (cur1+t>n1&&cur2+t>n2) {flag=1;break;}
			int now=min(x=f[cur1+t],y=g[cur2+t]);
			
			if (x<y) s1++;else s2++;
			cur1=a[now],cur2=b[now];
			
			if (now==n)
			{
				if (x<y&&s1<s2) flag=1; //x win the last game but totally y win
				if (x>y&&s1>s2) flag=1; //y win the last game but totally x win
				if (s1==s2) flag=1; //impossible for role
				break;
			} 
			
			
			cur=now;
		}
		
		
		
		if (!flag) ans.push_back(make_pair(max(s1,s2),t));
	}
	
	
	sort(ans.begin(),ans.end());
	printf("%d\n",ans.size());
	Rep(i,ans.size()) printf("%d %d\n",ans[i].first,ans[i].second);
	
	
	return 0;
}




CF 496D(Tennis Game-O(t*(n/t)复杂度+vector排序)