Codeforces Round #283 (Div. 2)D(good)
来源:互联网 发布:c语言else与下一个if 编辑:程序博客网 时间:2024/04/28 02:29
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 ofs sets, he wins the match and the match is over. Heres and t are some positive integer numbers.
To spice it up, Petya and Gena choose new numbers s andt 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 winss 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 numberss and t for the given match are also lost. The players now wonder what values ofs 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 integersai. Ifai = 1, then thei-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 numberss and t corresponds to the given record.
In the first line print a single number k — the number of options for numberss and t.
In each of the following k lines print two integerssi andti — the option for numberss and t. Print the options in the order of increasingsi, and for equalsi — in the order of increasingti.
51 2 1 2 1
21 33 1
41 1 1 1
31 42 24 1
41 2 1 2
0
82 1 2 1 1 1 1 1
31 62 36 1
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100010;const int INF=1000000010;int sum1[maxn],sum2[maxn];vector<pair<int,int> > ansp;int N;int binary(int l,int r,int len,int *sum){ int st=l; int ans=INF; while(l<=r) { int mid=(l+r)>>1; if(sum[mid]-sum[st-1]==len)ans=mid; if(sum[mid]-sum[st-1]>=len)r=mid-1; else l=mid+1; } return ans;}int judge(int t){ int cnt1=0,cnt2=0; int l=1; while(l<=N) { int pos1=binary(l,N,t,sum1); int pos2=binary(l,N,t,sum2); l=min(pos1,pos2)+1; if(l>N+1)return 0;//没有达到t分,无法结束 if(pos1<pos2)cnt1++; else cnt2++; if(l-1==N) { if(cnt1>cnt2&&pos1>pos2)return 0;//如果第一个人赢得局数已经大于第二个人, //而最后一局第一个人输了,说明这个s选的不合适 if(cnt1<cnt2&&pos1<pos2)return 0; } } if(cnt1==cnt2)return 0; return cnt1>cnt2?cnt1:cnt2;}int main(){ while(scanf("%d",&N)!=EOF) { memset(sum1,0,sizeof(sum1)); memset(sum2,0,sizeof(sum2)); for(int i=1;i<=N;i++) { int x; scanf("%d",&x); if(x==1)sum1[i]=1; else sum2[i]=1; } for(int i=1;i<=N;i++) sum1[i]+=sum1[i-1],sum2[i]+=sum2[i-1]; ansp.clear(); for(int i=1;i<=N;i++) { int tmp=judge(i); if(tmp<=0)continue; ansp.push_back(make_pair(tmp,i)); } sort(ansp.begin(),ansp.end()); int len=ansp.size(); printf("%d\n",len); for(int i=0;i<len;i++) printf("%d %d\n",ansp[i].first,ansp[i].second); } return 0;}
- Codeforces Round #283 (Div. 2)D(good)
- 线段树(good)Codeforces Round #275 (Div. 2)D
- Codeforces Round #266 (Div. 2)(good)
- Codeforces Round #279 (Div. 2)(good)
- Codeforces Round #166 (Div. 2) D - Good Substrings
- Codeforces Round #162 (Div. 2), problem: (D) Good Sequences
- Codeforces Round #166 (Div. 2)---D. Good Substrings(字符串)
- Codeforces Round #258 (Div. 2) D. Count Good Substrings
- Codeforces Round #258 (Div. 2)-D. Count Good Substrings
- Codeforces Round #357 (Div. 2) A Good Contest(水题)
- Codeforces Round #280 (Div. 2)E good
- Codeforces Round #283 (Div. 2) D
- Codeforces Round #283 (Div. 2) D,E
- Codeforces Round #124 (Div. 1) D. The Next Good String
- codeforces Round # 187(Div.2) D
- Codeforces Round #277 (Div. 2)(D)
- Codeforces Round #291 (Div. 2)(D)
- Codeforces Round #166 (Div. 2) D. Good Substrings——后缀自动机
- Spring的事务 之 9.1 数据库事务概述 ——跟我学spring3
- 第十七周项目6——学生成绩统计(2)
- 算法练习--二分搜索哈希表-JS 实现
- hdu 1247 Hat’s Words
- 数据库事务概述
- Codeforces Round #283 (Div. 2)D(good)
- 黑马程序员-Java学习08-IO类1
- spring是如何管理 事务的
- centos 6.5配置lamp
- qt的ListView中设置setIndexWidget
- makefile export变量
- 消息驱动bean(MDB)实例
- Spring多数据源解决方案
- Match函数的典型用法