B. 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.

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

只要暴力枚举每盘游戏的结束分数 t, 然后判断游戏的盘数 s 。

怎么判断s呢,当然就是找s的倍数 lar 。

然后再进行二分出两个玩家拿到结束当局游戏的分数 , 位置比较前的就是赢家 。

这题是我打那么多场DIV1最可惜的一题 , 卡在了边缘 。没有判断第n 个位置一定是要赢家得分的 。

而且注意 , 最后得分的人赢的游戏盘数一定比另外一个人多才符合要求 。

复杂度就是 n * log n * log n ( 枚举分数 t , 枚举 t 的倍数 , 二分 分数的位置 ) 。

#include <bits/stdc++.h>
using namespace std;
const int N = ;
const int INF = 1e9+;
typedef pair<int,int> pii;
#define X first
#define Y second
int n , a[N], cnt1[N] , cnt2[N] ;
vector<pii>ANS; int Find( int sc , int *A ) { int l = , r = n , pos = INF ;
if( A[n] < sc ) return pos;
while( l <= r ){
int mid = (l+r)>>;
if( A[mid] < sc )
l = mid + ;
else
pos = mid , r = mid - ;
}
return pos ;
} int check( int t ) { int ans = , sc1 = , sc2 = ;
int c1 = , c2 = ;
while( ){
int pos1 = Find( sc1 + t , cnt1 );
int pos2 = Find( sc2 + t , cnt2 );
if( pos1 == INF && pos2 == INF ) return - ;
if( pos1 == INF ) {
if( ( cnt2[n] - sc2 ) %t || a[n] == ) return - ;
else {
c2 += ( cnt2[n] - sc2 ) / t ;
if( c1 >= c2 ) return - ;
return c2 ;
}
}
if( pos2 == INF ) {
if( ( cnt1[n] - sc1 ) %t || a[n] == ) return - ;
else {
c1 += ( cnt1[n] - sc1 ) / t ;
if( c2 >= c1 ) return - ;
return c1 ;
}
}
if( pos1 < pos2 ) c1++ , sc1 = cnt1[pos1] , sc2 = cnt2[pos1];
else c2++ , sc1 = cnt1[pos2] , sc2 = cnt2[pos2] ;
}
} int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
while( cin >> n ){
cnt1[] = cnt2[] = ;
for( int i = ; i <= n ; ++i ) {
cin >> a[i] ;
if( a[i] == ) cnt1[i] = cnt1[i-]+ , cnt2[i] = cnt2[i-];
else cnt1[i] = cnt1[i-] , cnt2[i] = cnt2[i-]+;
}
int tot = max( cnt1[n] , cnt2[n] ) , ans = ;
ANS.clear();
for( int i = ; i <= tot ; ++i ) {
int res = check(i);
if( res != - ) {
ans++; ANS.push_back( pii( res , i ) );
}
}
sort( ANS.begin() , ANS.end() ) ;
cout << ans << endl ;
for( int i = ; i < ans ; ++i ) {
cout << ANS[i].X <<' ' << ANS[i].Y << endl ;
}
}
}

Codeforces 497B Tennis Game( 枚举+ 二分)的更多相关文章

  1. codeforces 613B B. Skills(枚举+二分+贪心)

    题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. codeforces 497b// Tennis Game// Codeforces Round #283(Div. 1)

    题意:网球有一方赢t球算一场,先赢s场的获胜.数列arr(长度为n)记录了每场的胜利者,问可能的t和s. 首先,合法的场景必须: 1两方赢的场数不一样多. 2赢多的一方最后一场必须赢. 3最后一场必须 ...

  3. Codeforces C. Maximum Value(枚举二分)

    题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. HDU4430 Yukari's Birthday(枚举+二分)

    Yukari's Birthday  HDU4430 就是枚举+二分: 注意处理怎样判断溢出...(因为题目只要10^12) 先前还以为要用到快速幂和等比数列的快速求和(但肯定会超__int64) 而 ...

  5. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  6. 4 Values whose Sum is 0(枚举+二分)

    The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, compute ...

  7. Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分

    C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...

  8. Codeforces gym101612 L.Little Difference(枚举+二分)

    传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...

  9. Codeforces 734C Anton and Making Potions(枚举+二分)

    题目链接:http://codeforces.com/problemset/problem/734/C 题目大意:要制作n个药,初始制作一个药的时间为x,魔力值为s,有两类咒语,第一类周瑜有m种,每种 ...

随机推荐

  1. 前端 ----- 初探ES6 Promise

    前段时间做项目,在调用接口的时候,遇到了异步问题.开始是使用定时器,发现效果并不理想,于是又用了回调,效果还好但是,很明显的影响了代码的整洁性. 于是我想起了在面试的那段时间,背过的面试题里,出现过一 ...

  2. jsp页面必填项如何加红星号*

    1.加*号 并且设置*号大小 <span style="color:red; font-size: 20px">*</span>

  3. django框架常用的数据库迁移命令

    python manage.py makemigrations 默认所有修改过的model层转为迁移文件 python manage.py migrate   默认将所有的迁移文件都执行,更新数据库 ...

  4. ASP.NET Core中的依赖注入: 构造函数的选择与服务生命周期管理

    ServiceProvider最终提供的服务实例都是根据对应的ServiceDescriptor创建的,对于一个具体的ServiceDescriptor对象来说,如果它的ImplementationI ...

  5. token和session的区别及其发展史

    其实token与session的问题是一种时间与空间的博弈问题, session是空间换时间,而token是时间换空间. 一.发展史 很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为 ...

  6. python-字符串的处理

    s1 = '###12314##231###' print(s1.split('#')) #split,从左往右遇见# 就拆分一次['', '', '', '12314', '', '231', '' ...

  7. 【python实例】统计字符串里大写字母,小写字母的个数和非字母的个数

    """ 给定一个以下字符串:统计大写字母的个数,小写字母的个数,非字母的个数. str1 = "ajdkkKDKEK1343KFKiriromfkfKKRIOW ...

  8. win7系统下IIS7.5配置MVC5环境注意事项与CentOS的MVC5设置

    注意事项: 1.IIS程序应用池更换为4.0net集成 2.网站目录加入IIS帐号的权限(基本设置里是administrators组帐号)注:服务器拒绝时是这里的问题 3.在“ISAPI和CGI限制” ...

  9. 人生苦短_我用Python_dict(字典)_003

    # coding=utf-8 # 字典 {"key":"values"} 无序的 # dict 可以包含任何类型的数据 values可以是任何数据类型 key必 ...

  10. spring-boot整合mongodb多数据源的案例

    1.运行环境 开发工具:intellij idea JDK版本:1.8 项目管理工具:Maven 4.0.0 2.GITHUB地址 https://github.com/nbfujx/springBo ...