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种,每种 ...
随机推荐
- AI-IBM-cognitive class --Liner Regression
Liner Regression import matplotlib.pyplot as plt import pandas as pd import pylab as pl import numpy ...
- rabbitmq windows安装 及 centos安装
windows安装如下: 安装方法如下网址: https://baijiahao.baidu.com/s?id=1605656085633071281&wfr=spider&for= ...
- Linux就该这么学11学习笔记
参考链接:https://i.cnblogs.com/EditPosts.aspx?opt=1 文件传输协议 一般来讲,人们将计算机联网的首要目的就是获取资料,而文件传输是一种非常重要的获取资料的方式 ...
- Dev控件
在DevExpress程序中使用PopupContainerEdit和PopupContainer实现数据展示 使用PopupContainerEdit和PopupContainerControl制作 ...
- python数字图像处理(四) 频率域滤波
import matplotlib.pyplot as plt import numpy as np import cv2 %matplotlib inline 首先读入这次需要使用的图像 img = ...
- Object.assign()遇到的问题分析
概念 Object.assign() 方法可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象.语法如下: Object.assign(target, ...sources) Obj ...
- 【LeetCode】从contest-21开始。(一般是10个contest写一篇文章)
[LeetCode Weekly Contest 29][2017/04/23] 第17周 Binary Tree Tilt (3) Array Partition I (6) Longest Lin ...
- Move Over and Click Link
Move Over and Click Link [Documentation] 等待悬浮菜单中的元素出现并单击元素 [Arguments] ${hover_locator} ${opt_locato ...
- 记录每个action执行时间
import org.apache.commons.lang.time.StopWatch; import org.aspectj.lang.JoinPoint; import org.aspectj ...
- [BZOJ1018][SHOI2008]堵塞的交通traffic 时间分治线段树
题面 介绍一种比较慢的但是好想的做法. 网上漫天的线段树维护联通性,然后想起来费很大周折也很麻烦.我的做法也是要用线段树的,不过用法完全不同. 这个东西叫做时间分治线段树. 首先我们建一个\(1..m ...