您的位置:首页 > 其它

Codeforces 496D Tennis Game【连续二分+枚举】

2017-04-18 13:32 423 查看
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.

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


题目大意:

已知N轮比赛的比赛结果,1表示A胜利,2表示B胜利。

一个赛季一共有S场比赛,每场比赛赛点为t轮。表示一场比赛如果有一个人先获胜了t轮那么这一场比赛就结束了,并且先达到t轮胜利的那个人为这场比赛的获胜者。

已知,如果一个人先获得了S场比赛的胜利,那么就结束了所有轮次的比赛,也就是说在N轮前结束的比赛是不合法的。

现在让你找到合法的s,t,并且按照s递增输出。

思路:

1、如果暴力去搞这个问题的话,就是O(n)枚举t.然后O(n)判断是否满足题意,使得一个队胜利了S场,且在第N轮结束的时候获得的最终胜利,那么对应此时的t,s是维护出来的。

那么总时间复杂度O(n^2);显然超时的一个时间复杂度。

其实对于一场比赛来讲,已知这场比赛的起点轮次的情况下,我们可以二分终点的轮次。

对于每一场游戏进行维护,那么我们需要连续二分,将时间复杂度降低至O(n*logn*longn);

2、在维护的过程中,需要保证正好游戏进行了N轮,并且需要保证最后一轮结束的同时,获胜的那只队伍必须是最终的获胜者。注意平局也是不可行的方案。

Ac代码:

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
}e[150000];
int a[150000];
int sum[105000];
int sum2[105000];
int n;
int cmp(node a,node b)
{
return a.x<b.x;
}
int Slove(int tt)
{
int aa=0;
int bb=0;
int now=0;
while(now<=n)
{
if(now==n)break;
int ans=-1;
int l=tt+now;
int r=tt*2-1+now;
r=min(r,n);
while(r-l>=0)
{
int mid=(l+r)/2;
if(sum[mid]-sum[now]>=tt||sum2[mid]-sum2[now]>=tt)
{
if(sum[mid]-sum[now]==tt)ans=mid;
if(sum2[mid]-sum2[now]==tt)ans=mid;
r=mid-1;
}
else l=mid+1;
}
if(ans==-1)return -1;
else
{
if(sum[ans]-sum[now]==tt)
{
aa++;
if(ans==n)
{
if(aa<=bb)return -1;
}
}
if(sum2[ans]-sum2[now]==tt)
{
bb++;
if(ans==n)
{
if(bb<=aa)return -1;
}
}
now=ans;
}
}
int tmp=max(aa,bb);
if(aa==bb)return -1;
return tmp;
}
int main()
{
while(~scanf("%d",&n))
{
memset(sum,0,sizeof(sum));
memset(sum2,0,sizeof(sum2));
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
int tmp=0;
if(a[i]==1)tmp++;
sum[i]=sum[i-1]+tmp;
tmp=0;
if(a[i]==2)tmp++;
sum2[i]=sum2[i-1]+tmp;
}
int tot=0;
for(int t=1;t<=n;t++)
{
int ans=Slove(t);
if(ans!=-1)
{
e[tot].x=ans;
e[tot++].y=t;
}
}
sort(e,e+tot,cmp);
printf("%d\n",tot);
for(int i=0;i<tot;i++)
{
printf("%d %d\n",e[i].x,e[i].y);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Codeforces 496D