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种,每种 ...
随机推荐
- MapReduce-WordCountDemo
/** * @Author: dreamer Q * @Date: 2019/11/4 22:26 * @Version 1.0 * @Discription 使用MapReduce 开发 WordC ...
- Git相关命令整理
git config --global user.name //配置姓名git config --global user.email //配置邮箱git config --list //查看配置 ...
- html的q标签、blockquote标签
九层之台,起于垒土 一.<q> 定义和用法 <q> 标签定义短的引用.浏览器经常在引用的内容周围添加引号. <html> <body> <p> ...
- 关于微信小程序的一些总结
mpvue? {{}} 在vue和小程序中的区别? 01 小程序中{{}}和vue中的{{}}用法基本一致,可以显示data中的数据,可以写表达式 不一样的地方? 01 小程序的{{}}可以写在属性中 ...
- Shell06--数组应用
目录 Shell06---数组应用 1. 数组基本概述 2. 数组基本使用 3. 数组遍历与循环 Shell06---数组应用 1. 数组基本概述 01. 什么是数组? 数组其实也算是变量,传统的变量 ...
- java 局部变量与成员成员变量的区别
package java04; /* 局部变量和成员变量的不同: 1.定义的位置不一样 局部变量:定义在方法内部 成员变量:在方法外部,直接写在类中 2.作用范围不一样 局部变量:只有方法中能使用,除 ...
- 一个历时五天的 Bug
一个程序员在没有成长成为架构师之前,几乎都要跟 Bug为伴,程序员有很多时间都是花在了查找各种 Bug上. 我印象深刻的一个Bug, 是一个服务器网络框架无锁队列的 Bug .那个 Bug 连续查找了 ...
- HTTP: 状态码200~505说明
HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释: 1xx(临时响应) 表示 ...
- 【leetcode】823. Binary Trees With Factors
题目如下: Given an array of unique integers, each integer is strictly greater than 1. We make a binary t ...
- css linear-gradient;心跳animation
css线性背景 background:linear-gradient(20deg,#ccffff,#ffcccc); transform transform:scale(1.5); transform ...