cf496D Tennis Game
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.
Example
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
倍增预处理下每个数字往后2^k是哪,然后就可以logn的知道往后走n步是哪。
枚举每一个可能的“小分”,然后直接模拟下往后走。可以logn的时间知道1和2那个先到。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int a[];
int s1[],s2[];
int go1[][],go2[][];
int bin[];
int lst1,lst2,anst;
struct aaa{int x,y;}ans[];
bool operator <(aaa a,aaa b){return a.x<b.x;}
inline int lowbit(int x){return x&(-x);}
inline int calc(int s,int k,int op)
{
if (s==-)return s;
while (k)
{
if (op==)s=go1[s][bin[lowbit(k)]];
else s=go2[s][bin[lowbit(k)]];
if (!s)break;
k-=lowbit(k);
}
return s==?-:s;
}
int main()
{
for (int i=;i<;i++)bin[<<i]=i;
n=read();
for (int i=;i<=n;i++)a[i]=read();
if (a[n]==)for (int i=;i<=n;i++)a[i]=-a[i];
for (int i=;i<=n;i++)
{
s1[i]=s1[i-]+(a[i]==);
s2[i]=s2[i-]+(a[i]==);
}
for (int i=n;i>=;i--)
{
go1[i][]=lst1;
go2[i][]=lst2;
if (a[i]==)lst1=i;
else lst2=i;
}
for (int i=;i<=;i++)
{
if(i>n)break;
for (int j=;j<=n;j++)
{
if (go1[j][i-])go1[j][i]=go1[go1[j][i-]][i-];
if (go2[j][i-])go2[j][i]=go2[go2[j][i-]][i-];
}
}
go1[][]=lst1;
go2[][]=lst2;
for (int i=;i<=;i++)
{
if (i>n)break;
if (go1[][i-]!=)go1[][i]=go1[go1[][i-]][i-];
if (go2[][i-]!=)go2[][i]=go2[go2[][i-]][i-];
}
for (int i=;i<=n;i++)
{
int cnt1=,cnt2=,now=,nx1,nx2,mrk=;
while (now!=-&&now<n)
{
nx1=calc(now,i,);
nx2=calc(now,i,);
if (nx1==-&&nx2==-){mrk=;break;}
if (nx1==-)cnt2++,now=nx2;
else if (nx2==-)cnt1++,now=nx1;
else
{
if (nx1<nx2)
{
cnt1++;now=nx1;
if (now==n)break;
}else
{
cnt2++;now=nx2;
}
}
}
if (mrk)continue;
if (!cnt1&&!cnt2)break;
if (cnt1>cnt2)ans[++anst].y=i,ans[anst].x=cnt1;
}
sort(ans+,ans+anst+);
printf("%d\n",anst);
for (int i=;i<=anst;i++)printf("%d %d\n",ans[i].x,ans[i].y);
}
cf496D
cf496D Tennis Game的更多相关文章
- Codeforces CF#628 Education 8 A. Tennis Tournament
A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 628A --- Tennis Tournament --- 水题
CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m & ...
- Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划
C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...
- Codeforces Round #382 (Div. 2) C. Tennis Championship 斐波那契
C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- PAT 1026. Table Tennis
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For ...
- Tennis Championship
Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 1026. Table Tennis (30)
题目如下: A table tennis club has N tables available to the public. The tables are numbered from 1 to N. ...
- codeforces 735C Tennis Championship(贪心+递推)
Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C ——每天在线,欢迎留言谈论. 题目大意: 给你一个 n ...
- Tennis Game CodeForces - 496D(唯一分解定理,费马大定理)
Tennis Game CodeForces - 496D 通过排列组合解决问题. 首先两组不同素数的乘积,是互不相同的.这应该算是唯一分解定理的逆运用了. 然后是,输入中的素数,任意组合,就是n的因 ...
随机推荐
- CMDB资产采集方案
CMDB资产采集方案 CMDB 资产采集的方案总共有四种 Agent SSH类 Saltstack Puttet 方案设计,从性能上考虑 下面前三种是用Python开发的,目标是兼容三种采集方式的软件 ...
- apropos linux
Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ...
- OO第三次电梯作业优化
目录 第三次电梯作业个人优化 前言 优化思路 一.调度器 二.电梯 第三次电梯作业个人优化 前言 由于个人能力有限,第二次电梯作业只能完成正确性设计,没能进行优化,也因此损失了强测分数,于是第三次电梯 ...
- Dojo的define接口
http://blog.csdn.net/lovecarpenter/article/details/53979717 第三种用法用的最多. 此接口用于定义模块: define([],function ...
- ReactiveCocoa概念解释篇
1.ReactiveCocoa简介 ReactiveCocoa(简称为RAC),是由Github开源的一个应用于iOS和OS开发的新框架,Cocoa是苹果整套框架的简称,因此很多苹果框架喜欢以Coco ...
- python爬虫: 豆瓣电影top250数据分析
转载博客 https://segmentfault.com/a/1190000005920679 根据自己的环境修改并配置mysql数据库 系统:Mac OS X 10.11 python 2.7 m ...
- 日志切割logrotate和定时任务crontab详解
1.关于日志切割 日志文件包含了关于系统中发生的事件的有用信息,在排障过程中或者系统性能分析时经常被用到.对于忙碌的服务器,日志文件大小会增长极快,服务器会很快消耗磁盘空间,这成了个问题.除此之外,处 ...
- urllib、requests库整理
- Applied Nonparametric Statistics-lec3
Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/4 使用非参数方法的优势: 1. 对总体分布做的假设 ...
- __vet_atags
参考:atags--__vet_atags标签 arch/arm/include/asm/setup.h /* * linux/include/asm/setup.h * * Copyright ...