Codeforces Round #283 (Div. 2) D. Tennis Game
来源:互联网 发布:海南大学网络教学中心 编辑:程序博客网 时间:2024/04/29 08:25
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.
枚举t,在O(lgn)的复杂度判断这个t是否可行,如果可行的话计算出s。a[i]代表第一个人赢的第i把在整个序列中的位置,b[i]代表第二个人赢的第i把在整个序列中的位置,同时用win1[i]表示前i次第一个人赢了几次,win2[i]表示前i次第二个人赢了几次。
然后进行模拟,pos1表示第一个人在a数组中当前的位置,pos2表示第二个人在数组b中当前的位置,cur表示当前已经玩了几次,根据a[pos1+t]和b[pos2+t]的大小来判断下轮谁赢,然后根据pos和win更新下一轮结束后cur和pos1,pos2的位置。最后如果cur能刚好在N的位置并且赢的次数多的那个人是整个序列最后出现的那个人,说明t是满足的。
#include<iostream>#include<cstdio>#include<algorithm>#include<vector>using namespace std;typedef pair<int,int> pii;const int MAXN=100010;int N,a[MAXN],b[MAXN],win1[MAXN],win2[MAXN];vector<pii> V;int main(){ while(scanf("%d",&N)!=EOF){ int t,cnt1=0,cnt2=0,ans=0,last; V.clear(); for(int i=1;i<=N;i++){ scanf("%d",&t); last=t; if(t==1) a[++cnt1]=i; else b[++cnt2]=i; win1[i]=cnt1; win2[i]=cnt2; } for(t=1;t<=N;t++){ int cur=0,pos1=0,pos2=0,ans1=0,ans2=0,flag=1; while(cur<N){ if(pos1+t>cnt1&&pos2+t>cnt2){ flag=0; break; } if(pos1+t<=cnt1&&pos2+t<=cnt2){ if(a[pos1+t]<b[pos2+t]){ pos1+=t; cur=win1[a[pos1]]+win2[a[pos1]]; pos2=win2[a[pos1]]; ans1++; } else{ pos2+=t; cur=win1[b[pos2]]+win2[b[pos2]]; pos1=win1[b[pos2]]; ans2++; } } else if(pos1+t<=cnt1){ int n=(cnt1-pos1)/t; pos1+=n*t; cur=win1[a[pos1]]+win2[a[pos1]]; ans1+=n; } else if(pos2+t<=cnt2){ int n=(cnt2-pos2)/t; pos2+=n*t; cur=win1[b[pos2]]+win2[b[pos2]]; ans2+=n; } } if(flag&&(last==1&&ans1>ans2||last==2&&ans1<ans2)){ ans++; V.push_back(make_pair(max(ans1,ans2),t)); } } int len=V.size(); sort(V.begin(),V.end()); printf("%d\n",ans); for(int i=0;i<len;i++) printf("%d %d\n",V[i].first,V[i].second); } return 0;}
- Codeforces Round #283 (Div. 2) D. Tennis Game
- Codeforces Round #283 (Div. 2) D. Tennis Game
- Codeforces Round #283 (Div. 2) D. Tennis Game
- Codeforces Round #283 (Div. 2) D Tennis Game ——枚举,二分
- Codeforces Round #397(Div. 1 + Div. 2 combined)C. Table Tennis Game 2【贪心】
- Codeforces Round #443 (Div. 2)BTable Tennis
- Codeforces Round #382 (Div. 2)C. Tennis Championship(贪心) D.巴赫猜想
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game
- Codeforces Round #304 (Div. 2) D Soldier and Number Game
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game
- Codeforces Round #304 (Div. 2) D - Soldier and Number Game
- Codeforces Round #320 (Div. 2)D. "Or" Game
- Codeforces Round #320 (Div. 2) D. "Or" Game
- Codeforces Round #360 (Div. 2) D. Remainders Game(数学)
- Codeforces Round #360 (Div. 2) D. Remainders Game
- Codeforces Round #360 (Div. 2) D. Remainders Game
- Codeforces Round #304 (Div. 2)D. Soldier and Number Game
- Codeforces Round #320 (Div. 2) - D. "Or" Game
- AS3][Matrix][利用颜色矩阵进行颜色变换]
- Spring学习8-用MyEclipse搭建SSH框架 Struts Spring Hibernate
- Android安全机制讲解
- ColorMatrixFilter--颜色矩阵滤镜 此博文包含图
- 线程间同步机制----信号
- Codeforces Round #283 (Div. 2) D. Tennis Game
- Lua简明教程
- Spring学习9-MyEclipse中Spring工程使用@Resource注释的问题
- 黑马程序员 多态
- 如何在本地Windows环境中用Eclipse中调试MapReduce程序
- Spring学习10-SpringMVC入门
- UVa 10474 - Where is the Marble?
- Spring学习10-SpringMVC原理及核心组件1
- html+css仿百度新闻首页(下)