Codeforces 497B Tennis Game( 枚举+ 二分)
2 seconds
256 megabytes
standard input
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?
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.
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.
5
1 2 1 2 1
2
1 3
3 1
4
1 1 1 1
3
1 4
2 2
4 1
4
1 2 1 2
0
8
2 1 2 1 1 1 1 1
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( 枚举+ 二分)的更多相关文章
- codeforces 613B B. Skills(枚举+二分+贪心)
题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces 497b// Tennis Game// Codeforces Round #283(Div. 1)
题意:网球有一方赢t球算一场,先赢s场的获胜.数列arr(长度为n)记录了每场的胜利者,问可能的t和s. 首先,合法的场景必须: 1两方赢的场数不一样多. 2赢多的一方最后一场必须赢. 3最后一场必须 ...
- Codeforces C. Maximum Value(枚举二分)
题目描述: Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard ...
- HDU4430 Yukari's Birthday(枚举+二分)
Yukari's Birthday HDU4430 就是枚举+二分: 注意处理怎样判断溢出...(因为题目只要10^12) 先前还以为要用到快速幂和等比数列的快速求和(但肯定会超__int64) 而 ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- 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 ...
- 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 ...
- Codeforces gym101612 L.Little Difference(枚举+二分)
传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...
- Codeforces 734C Anton and Making Potions(枚举+二分)
题目链接:http://codeforces.com/problemset/problem/734/C 题目大意:要制作n个药,初始制作一个药的时间为x,魔力值为s,有两类咒语,第一类周瑜有m种,每种 ...
随机推荐
- shell --08 例子
目录 1.按照时间生成文件2018-05-22.log将每天的磁盘使用状态写入到对应日期的文件 [root@web01 ceshi]# cat 1.sh #!/bin/bash Date=`date ...
- BZOJ3227 [sdoi2008]红黑树
贪心什么的太神仙了( 老老实实dp于是就是沙茶题了 f[i][d][0/1]表示i个节点bh为d当前节点颜色白/黑[好好读题是真.. 转移一下然后就可以打表了( 由于我们发现这玩意很好卡有很好的性质( ...
- java 通过反射调用属性,方法,构造器
package reflection2; import static org.junit.Assert.assertArrayEquals; import java.lang.reflect.Cons ...
- MyBank后感
MyBank系统主要的构成部分有:银行账户的初始化,开户,存款,取款,转账,查询余额,修改密码这些操作. =============================================== ...
- delphi 控件背景透明代码
procedure DrawParentBackground(Control: TControl; DC: HDC; R: PRect = nil; bDrawErasebkgnd: Boolean ...
- windows系统的安装时间怎么查看
方法一:利用命令符窗口查询 直接按下Windows+R组合键 出现运行对话框(或 点击开始—运行),输入cmd,进入命令符窗口 然后,在该界面下输入”systeminfo”,然后回车,等待系统自动运 ...
- 【原】webpack--文件监听的原理
轮询判断文件的最后编辑时间是否发生变化,一开始有个文件的修改时间,先存储起来这个修改时间,下次再有修改就会和上次修改时间比对,发现不一致的时候不会立即告诉监听者,而是把文件修改缓存起来,等待一段时间, ...
- Oracle 11g 体系结构 --SGA PGA 前后台进程
Oracle服务器主要由实例.数据库.程序全局区.前台进程 实例:用来提供管理数据库的功能 数据库:由Oracle数据库文件组成,用来存储系统数据 ;一般有:数据文件.控制文件.重做日志文件 而实例可 ...
- leetcode-解题记录 884. 两句话中的不常见单词
题目 给定两个句子 A 和 B . (句子是一串由空格分隔的单词.每个单词仅由小写字母组成.) 如果一个单词在其中一个句子中只出现一次,在另一个句子中却没有出现,那么这个单词就是不常见的. 返回所有不 ...
- 项目搭建(一):windows UIAutomation API 框架
[环境] 操作系统:Windows7 集成环境:Visual Studio2015 编程语言:C# 目标框架:.net framework4.6 1.新建项目 Visual Studio 2015 [ ...