CF 496D(Tennis Game-O(t*(n/t)复杂度+vector排序)

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.

Sample test(s)

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

本题考试使就是想不出非暴力算法。

考完后得知暴力的复杂度是O(t*(n/t))=O(n) ..我一直以为是O(n^2)...

我说为什么有人5minAC,,说多了都是泪

解法:

显然t确定s就确定了,暴力枚举t,然后扫一遍,记得先预处理使得能在O(1)查找1局比赛

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<vector>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (100000+10)
long long mul(long long a,long long b){return (a*b)%F;}
long long add(long long a,long long b){return (a+b)%F;}
long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
typedef long long ll;
int n,n1=0,n2=0;
int f[MAXN],g[MAXN]; // f/g win'game num
int a[MAXN],b[MAXN]; // f/g win f[i] game until ith game end

vector<pair<int,int> > ans;
int main()
{
//	freopen("Tennis.in","r",stdin);
//	freopen(".out","w",stdout);
	MEMI(f) MEMI(g)
	cin>>n;
	For(i,n)
	{
		int t;
		scanf("%d",&t);
		if (t&1) f[++n1]=i;
		else g[++n2]=i;
		a[i]=n1,b[i]=n2;
	}
	For(t,n)
	{
		bool flag=0;
		int s1=0,s2=0,cur1=0,cur2=0;
		for(int cur=0;cur<=n;)
		{
			int x,y;
			if (cur1+t>n1&&cur2+t>n2) {flag=1;break;}
			int now=min(x=f[cur1+t],y=g[cur2+t]);

			if (x<y) s1++;else s2++;
			cur1=a[now],cur2=b[now];

			if (now==n)
			{
				if (x<y&&s1<s2) flag=1; //x win the last game but totally y win
				if (x>y&&s1>s2) flag=1; //y win the last game but totally x win
				if (s1==s2) flag=1; //impossible for role
				break;
			} 

			cur=now;
		}

		if (!flag) ans.push_back(make_pair(max(s1,s2),t));
	}

	sort(ans.begin(),ans.end());
	printf("%d\n",ans.size());
	Rep(i,ans.size()) printf("%d %d\n",ans[i].first,ans[i].second);

	return 0;
}
时间: 2024-08-06 08:23:43

CF 496D(Tennis Game-O(t*(n/t)复杂度+vector排序)的相关文章

CF 628A --- Tennis Tournament --- 水题

CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m <=n)个人进行比赛,剩余的n-m个人直接晋级, 直至只剩一人为止,问总共需要的水的数量和毛巾的数量 解题思路:毛巾数很简单: n*p即可 水的数量:1,2,4,8,16,32,64,128,256,512,提前打成一个表, 根据当前剩余的人数n在表中二分查找最大的小于等于n的数,结果即为本次进行比赛

CF 496D

D. 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 o

Codeforces 496D Tennis Game 枚举+二分

题目链接:点击打开链接 题意: 给定n场比赛. 下面n个数字:表示该场是1获胜还是2获胜. 1.胜利者获得一分. 2.若已经决出整个赛季的胜负则比赛不会继续. 3.设要赢得这个赛季需要赢有s局,每局先获得t分的选手胜利. 问: 找出所有的(s,t)组合使得给定的n场比赛记录合法. 输出要排序. 枚举t. a数组存第一个人赢的哪些场次. b数组存第二个人赢的哪些场次. 设赢t分为一句.则判断 第一个人再赢t分是第几场,第二个人再赢t分是第几场. 显然先赢得t分的人赢了这场. 这样同时跑2个人的场数

CodeForces 496D Tennis Game (暴力枚举)

题意:进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时, 游戏结束.现在给出n次对决的记录,问可能的s和t有多少种,并按s递增的方式输出. 析:如果枚举s 和 t,那么一定会超时的,所以我们考虑是不是可以不用全枚举.我们只要枚举 t ,然后每次都去计算 s. 首先我们先预处理两个人的获得第 i 分时是第几场比赛.然后每次枚举每个 t,每次我们都是加上t,所以总的时间复杂度为 n*logn. 完全可以接受,注意有几个坑,首先

【机器学习算法-python实现】协同过滤(cf)的三种方法实现

(转载请注明出处:http://blog.csdn.net/buptgshengod) 1.背景 协同过滤(collaborative filtering)是推荐系统常用的一种方法.cf的主要思想就是找出物品相似度高的归为一类进行推荐.cf又分为icf和ucf.icf指的是item collaborative filtering,是将商品进行分析推荐.同理ucf的u指的是user,他是找出知趣相似的人,进行推荐.通常来讲icf的准确率可能会高一些,通过这次参加天猫大数据比赛,我觉得只有在数据量非

CF dp 一句话解题

wyq说刚入门oi 或是遇到瓶颈的时候就刷DP吧,虽然觉得这么刷CF题有点浪费,但是还是挺爽的,按照solved排序做的,前面的题都挺水的(忘记记录了混蛋),就不写了,从5C开始写解题 CF5 C. Longest Regular Bracket Sequence:题目大意,给一个括号序列,让你求最长的合法扩号子串,以及最长子串出现的次数 思路:开个栈乱搞,当遇到右括号的时候这右括号开始最长的长度应该是到左括号的长度加上左括号左边一个字符的最长长度

URAL 2089 Experienced coach Twosat

Description Misha trains several ACM teams at the university. He is an experienced coach, and he does not underestimate the meaning of friendly and collaborative atmosphere during training sessions. It used to be that way, but one of the teams happen

《人工智能教程(张仰森)》(二)

三.确定性推理方法 依照推理过程所用知识的确定性,推理可分为确定性推理和不确定性推理.自然演绎推理和归结推理是经典的确定性推理.         1. 推理概述 首先谈了推理的基本概念.还是涉及到推理机.综合数据库和知识库的流程. 然后是推理的方法及其分类. 1)依照推理的逻辑基础分为演绎推理(由一般到个别,最经常使用的是三段论).归纳推理(由个别到一般,最经常使用的是数学归纳法)和默认推理(在知识不完备的情况下如果某些条件已经具备).演绎推理不能增殖新的知识,而归纳推理即归结推理能产生新的知识

Wedding (poj 3648 2-SAT 输出随意一组解)

Language: Default Wedding Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9004   Accepted: 2722   Special Judge Description Up to thirty couples will attend a wedding feast, at which they will be seated on either side of a long table. Th