您的位置:首页 > 其它

Codeforces Round #283 (Div. 2) D Tennis Game ——枚举,二分

2014-12-26 11:33 483 查看
原题链接:

http://codeforces.com/contest/496/problem/D

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


题意:比赛,数字1和2分别代表a,b获胜,规则是:①优先获得t次胜利的人,总分+1,当前小局得分清零②优先总分达到s的人获得总胜利,游戏结束。

思路:

①枚举t的值,然后依次判断谁优先获得t分,计算出a,b总分。

②总分大的人,与将游戏结束的人若相同,则t可行。

③枚举t的时候,用二分方法计算。

小优化:

①t枚举的范围,上限可优化为最终胜利人的总胜利局数。

②枚举t计算总分时,若计算到某t局胜利结束后,后面a和b胜利的局数都小于t了,则停止计算。

注意:

注意输出的顺序。需要重新排序一下。

#include "iostream"
#include "stdio.h"
#include "string.h"
#include "algorithm"
using namespace std;

int a[100010],b[100010],h=0,nans=-1,n,winer,sa=0,sb=0;

struct f
{
int s;
int t;
}ans[100010];

bool cmp(struct f a1,struct f a2)
{
if(a1.s!=a2.s)
return (a1.s<a2.s);
else
return (a1.t<a2.t);
}

void get_s(int i)
{
h=0;
sa=0;
sb=0;
while(a
-a[h]>=i||b
-b[h]>=i)
{
int l=h+1,r=n,m;
int tha=n+1,thb=n+1;
while(l<=r)
{
m=(l+r)/2;
if(a[m]-a[h]>=i)
{
tha=m;
r=m-1;
}
else
l=m+1;
}
l=h+1;
r=n;
while(l<=r)
{
m=(l+r)/2;
if(b[m]-b[h]>=i)
{
thb=m;
r=m-1;
}
else
l=m+1;
}
if(thb>tha)
{
h=tha;
sa++;
}
else
{
h=thb;
sb++;
}
}
if(h!=n)
sb=sa;
}

int main()
{
int num,nwin,w;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&num);
if(num==1)
{
a[i]=a[i-1]+1;
b[i]=b[i-1];
}
else
{
a[i]=a[i-1];
b[i]=b[i-1]+1;
}
}
if(a
!=a[n-1])
{
winer=1;
nwin=a
;
}
else
{
winer=2;
nwin=b
;
}
for(int i=1;i<=nwin;i++)
{
get_s(i);
if(winer==1&&sa>sb)
{
ans[++nans].s=sa;
ans[nans].t=i;
}
else if(winer==2&&sb>sa)
{
ans[++nans].s=sb;
ans[nans].t=i;
}
}
printf("%d\n",nans+1);
sort(ans,ans+nans+1,cmp);
if(nans>=0)
{
for(int i=0;i<=nans;i++)
printf("%d %d\n",ans[i].s,ans[i].t);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: