D. 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 increasing si, and for equal si — in the order of increasing ti.

Examples
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
【分析】进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时,游戏结束。
现在给出n次对决的记录,问可能的s和t有多少种,并按s递增的方式输出。
可以想到枚举t,然后挨个找每一场结束的位置。但是从头遍历肯定会超时,所以需要预处理一下。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e6+;
const int M = 1e6+;
int n,m,k,cnt[];
int p[][N],a[N],pp[][N];
struct man{
int s,t;
}ans[N];
bool cmp(man d,man f){
if(d.s==f.s)return d.t<f.t;
return d.s<f.s;
}
int main() {
int x,y,tot=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&x);
cnt[x]++;
p[x][cnt[x]]=i;
pp[][i]=cnt[];
pp[][i]=cnt[];
}
bool ok=false;
int s1=,s2=;
for(int i=;i<=n;i++){
x=y=;
int g1=,g2=;
int u,v;
while(){
x+=i;y+=i;
u=p[][x];
v=p[][y];
if(u==v)break;
else if(u==){
g2++;
x=pp[][v];
y=pp[][v];
if(v==n)break;
}
else if(v==){
g1++;
x=pp[][u];
y=pp[][u];
if(u==n)break;
}
else if(u<v){
g1++;
x=pp[][u];
y=pp[][u];
}
else if(v<u){
g2++;
x=pp[][v];
y=pp[][v];
}
}
if(u!=n&&v!=n)continue;
if(g1!=g2){
ok=true;
if(g1>g2&&u==n){
ans[++tot].s=max(g1,g2);
ans[tot].t=i;
}
else if(g2>g1&&v==n){
ans[++tot].s=max(g1,g2);
ans[tot].t=i;
}
}
}
if(!ok)puts("");
else {
sort(ans+,ans+tot+,cmp);
printf("%d\n",tot);
for(int i=;i<=tot;i++){
printf("%d %d\n",ans[i].s,ans[i].t);
}
}
return ;
}

Codeforces Round #283 (Div. 2) D. Tennis Game(模拟)的更多相关文章

  1. Codeforces Round #283 Div.2 D Tennis Game --二分

    题意: 两个人比赛,给出比赛序列,如果为1,说明这场1赢,为2则2赢,假如谁先赢 t 盘谁就胜这一轮,谁先赢 s 轮则赢得整个比赛.求有多少种 t 和 s 的分配方案并输出t,s. 解法: 因为要知道 ...

  2. 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

    题目传送门 /* 题意:删除若干行,使得n行字符串成递增排序 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 */ /*********************** ...

  3. 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

    题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...

  4. Codeforces Round #368 (Div. 2) B. Bakery (模拟)

    Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...

  5. Codeforces Round #284 (Div. 2)A B C 模拟 数学

    A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

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

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

  8. Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划

    C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...

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

随机推荐

  1. Boosting&Bagging

    Boosting&Bagging 集成学习方法不是单独的一个机器学习算法,而是通过构建多个机器学习算法来达到一个强学习器.集成学习可以用来进行分类,回归,特征选取和异常点检测等.随机森林算法就 ...

  2. [Leetcode] Palindrome number 判断回文数

    Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...

  3. 【NOIP模拟赛】 permutation 数学(打表)

    biubiu~~~ 这道题卡读题卡得很死......首先他告诉我们读循环的时候要顺着圈读,然后又说这个圈在数列上要以最大数开始读,而且以这样的循环的首数排序,得到的序列与原序列一样那么他就是可行序列, ...

  4. taotao用户登录springMVC拦截器的实现

    在springMVC中写拦截器,只需要两步: 一.写 java 拦截器类,实现 interceptor 拦截器接口. 二.在 springMVC 的xml配置文件中,配置我们创建的拦截器对象及其拦截目 ...

  5. n元线性方程非负整数解的个数问题

    设方程x1+x2+x3+...+xn = m(m是常数) 这个方程的非负整数解的个数有(m+n-1)!/((n-1)!m!),也就是C(n+m-1,m). 具体解释就是m个1和n-1个0做重集的全排列 ...

  6. 安卓下拉刷新空间SwipeRefreshLayout的基本使用

    1.先写布局文件 <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/refresh" androi ...

  7. 有关eclipse的内存溢出问题

    一:前言 最近在做的项目在启动tomcat时就报“内存溢出的错误”,其实也不是自己第一次遇到,但是每次都是在网上查询后敲进去,所以这次我觉得自己记载下来吧. 二:内容 我自己的配置大小,这里的配置位置 ...

  8. 知问前端——日历UI(二)

    datapicker外观选项 属性 默认值/类型 说明 disabled false/布尔值 禁用日历 numberOfMonths 1/数值 日历中同时显示的月份个数.默认为1,如果设置3就同时显示 ...

  9. 【BZOJ4031】【HEOI2015】小Z的房间 [Matrix-Tree][行列式]

    小Z的房间 Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 你突然有了一个大房子,房子里面有 ...

  10. [BZOJ2190&BZOJ2705]欧拉函数应用两例

    欧拉函数phi[n]是表示1~n中与n互质的数个数. 可以用公式phi[n]=n*(1-1/p1)*(1-1/p2)*(1-1/p3)...*(1-1/pk)来表示.(p为n的质因子) 求phi[p] ...