codeforces 排位赛1

博客内容涉及codeforces的编程竞赛,包括多个问题的题面、分析及代码解决方案,讨论了如何计算一致性的牛群对、在FizBuzz游戏中找到特定位置的数字、判断朋友在参观农场时是否满意、按特定条件排列奶牛以及确定所需查看的邮箱颜色序列长度等算法问题。
摘要由CSDN通过智能技术生成

传送门

题面

A. Cow Gymnastics
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
In order to improve their physical fitness, the cows have taken up gymnastics! Farmer John designates his favorite cow Bessie to coach the N other cows and to assess their progress as they learn various gymnastic skills. In each of K practice sessions (1≤K≤10), Bessie ranks the N cows according to their performance (1≤N≤20). Afterward, she is curious about the consistency in these rankings. A pair of two distinct cows is consistent if one cow did better than the other one in every practice session.

Help Bessie compute the total number of consistent pairs.

Input
The first line of the input file contains two positive integers K and N. The next K lines will each contain the integers 1…N in some order, indicating the rankings of the cows (cows are identified by the numbers 1…N). If A appears before B in one of these lines, that means cow A did better than cow B.

Output
Output, on a single line, the number of consistent pairs.

Example
inputCopy
3 4
4 1 2 3
4 1 3 2
4 2 1 3
outputCopy
4
Note
The consistent pairs of cows are (1,4), (2,4), (3,4), and (1,3).

分析

n很小 暴力就ok了

代码

#include<iostream>
using namespace std;
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

const int INF=1e9;
const int MAX_N=25; 
int a[MAX_N][MAX_N];
int f[MAX_N]; 

int main()
{
	int k,n; scanf("%d%d",&k,&n);
	for(int i=1;i<=k;i++) for(int j=1;j<=n;j++) scanf("%d",&a[i][j]);//第i次  排名第j的牛 
	
	int ans=0;//
	
	for(int i=1;i<=n;i++){//枚举n头牛
		//memset(f,0,sizeof(f));
		for(int i=1;i<=n;i++) f[i]=1;
		 
		for(int j=1;j<=k;j++){//k次 
			for(int x=1; x<=n ;x++){ 牛i之前都 X 
				f[ a[j][x] ]=0;//第x头牛 pass	
				
				if(a[j][x]==i) break;
			}
		}
	 	//for(int j=1;j<=n;j++) if(f[j]==1) { ans++; cout<<j<<" ";}  cout<<endl; 
	 	for(int j=1;j<=n;j++) if(f[j]==1)  ans++;
	}
	printf("%d\n",ans); 
	return 0;
} 


题面

B. MooBuzz
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John’s cows have recently become fans of playing a simple number game called “FizzBuzz”. The rules of the game are simple: standing in a circle, the cows sequentially count upward from one, each cow saying a single number when it is her turn. If a cow ever reaches a multiple of 3, however, she should say “Fizz” instead of that number. If a cow reaches a multiple of 5, she should say “Buzz” instead of that number. If a cow reaches a multiple of 15, she should say “FizzBuzz” instead of that number. A transcript of the first part of a game is therefore: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16

Having a slightly more limited vocabulary, the version of FizzBuzz played by the cows involves saying “Moo” instead of Fizz, Buzz, and FizzBuzz. The beginning of the cow version of the game is therefore

1, 2, Moo, 4, Moo, Moo, 7, 8, Moo, Moo, 11, Moo, 13, 14, Moo, 16

Given N (1≤N≤109), please determine the Nth number spoken in this game.

Test cases 2-5 satisfy N≤106.

Input
The input consists of a single integer, N.

Output
Please print out the Nth number spoken during the game.

Example
inputCopy
4
outputCopy
7
Note
The 4th number spoken is 7. The first 4 numbers spoken are 1, 2, 4, 7, since we skip over any time a cow says “Moo”.

分析

大概就是 把自然数中所有3和5的倍数除去,求第n个数
有两种方法
1 反过来想,假设这个数是k,1~k中把3和5的倍数除去后剩下的数的个数就是n
所以可以用二分,其中除去3和5的倍数可以用容斥

2 设原自然数集合为A,除去3和5的倍数后集合为B
每15个数除去3和5的倍数后,剩下8个数
也就是说,15是集合A的一个循环节,如{123456789 10 11 12 13 14 15}
而对应地,8是集合B的一个循环节 ,如 {1,2,4,7,8,11,13,14}
所以,算出集合B中 n在第几个循环节,n在最后一个循环节的第几个数
对应地,集合A中 k也在第几个循环节,k也在最后一个循环节中第几个数

代码

方法一

#include<iostream>
using namespace std;
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

const long long INF=3e9;
int n;

long long f(long long x){//x是 第几个数 
	return x-x/3-x/5+x/15;
}

long long bisearch(long long l,long long r){
	
	while(l<r){
		long long mid=(l+r)/2;
		if(f(mid)<n) l=mid+1;
		else r=mid;
	}
	return l;
}

int main()
{
	scanf("%d",&n);
	
	printf("%lld\n", bisearch(1,INF) );
	
	return 0;
} 

方法二

#include<iostream>
using namespace std;
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>

const long long INF=3e9;
int a[15]={1,2,4,7,8,11,13,14};
int n;

int main()
{
	cin>>n;
	long long ans=(n-1)/8*15;
	ans+=a[ (n-1)%8 ];
	cout<<ans<<endl;
	
	return 0;
}

题面

E. Milk Visits
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John is planning to build N (1≤N≤105) farms that will be connected by N−1 roads, forming a tree (i.e., all farms are reachable from each-other, and there are no cycles). Each farm contains a cow, whose breed is either Guernsey or Holstein.

Farmer John’s M friends (1≤M≤105) often come to visit him. During a visit with friend i, Farmer John will walk with his friend along the unique path of roads from farm Ai to farm Bi (it may be the case that Ai=Bi). Additionally, they can try some milk from any cow along the path they walk. Since most of Farmer John’s friends are also farmers, they have very strong preferences regarding milk. Some of his friends will only drink Guernsey milk, while the remainder will only drink Holstein milk. Any of Farmer John’s friends will only be happy if they can drink their preferred type of milk during their visit.

Please determine whether each friend will be happy after visiting.

Input
The first line contains the two integers N and M.

The second line contains a string of length N. The ith character of the string is ‘G’ if the cow in the ith farm is a Guernsey, or ‘H’ if the cow in the ith farm is a Holstein.

The next N−1 lines each contain two distinct integers X and Y (1≤X,Y≤N), indicating that there is a road between farms X and Y.

The next M lines contain integers Ai, Bi, and a character Ci. Ai and Bi represent the endpoints of the path walked during friend i’s visit, while Ci is either G or H if the ith friend prefers Guernsey milk or Holstein milk.

Output
Print a binary string of length M. The ith character of the string should be ‘1’ if the ith friend will be happy, or ‘0’ otherwise.

Example
inputCopy
5 5
HHGHG
1 2
2 3
2 4
1 5
1 4 H
1 4 G
1 3 G
1 3 H
5 5 H
outputCopy
10110
Note
Here, the path from farm 1 and farm 4 involves farms 1, 2, and 4. All of these contain Holsteins, so the first friend will be satisfied while the second one will not.

分析

先dfs给每个连通块标号
要满足题意,有两种情况
1 起点或终点的类型符合ch
2 若起点和终点类型都不符合ch,但这两点所在的连通块序号不同,说明这两点不在同一个连通块,两点之间也就必然会经过另一种类型的连通块,而那另一种类型就是符合ch的类型

代码

#include<iostream>
using namespace std;
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<vector>
typedef long long ll;

const int INF=1e9;
const int MAX_N=1e5+5;
int Y[MAX_N];
char str[MAX_N];
int ans[MAX_N];
vector<int> g[MAX_N]; 

void dfs(int x,int num,char ch){//dfs求连通块 并标号 
	if(str[x]!=ch) return ;// 
	
	Y[x]=num;
	for(int i=0;i<g[x].size();i++){
		int j=g[x][i];
		if(!Y[j] && str[j]==ch) dfs(j,num,ch);
	}
}


int main()
{
	int n,m; scanf("%d%d",&n,&m);
	scanf("%s",str+1);
	for(int i=1;i<=n-1;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		g[x].push_back(y);
		g[y].push_back(x);
	}
	
	int num=0;
	for(int i=1;i<=n;i++){
		if(!Y[i]) { 
			num++;
			dfs(i,num,str[i]);
		}
	}

	for(int i=1;i<=m;i++){
		int x,y; char ch;
		scanf("%d%d",&x,&y);
		getchar(); scanf("%c",&ch);				
		if( str[x]==ch || str[y]==ch || Y[x]!=Y[y]) ans[i]=1;//若起点终点类型不符ch 但两点所在连通块序号也不同  说明中间有其他类型的连通块 
		
	}
	for(int i=1;i<=m;i++) printf("%d",ans[i]);
	
	
	return 0;
} 

题面

G. Livestock Lineup
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Every day, Farmer John milks his 8 dairy cows, named Bessie, Buttercup, Belinda, Beatrice, Bella, Blue, Betsy, and Sue. The cows are rather picky, unfortunately, and require that Farmer John milks them in an order that respects N constraints (1≤N≤7). Each constraint is of the form “X must be milked beside Y”, stipulating that cow X must appear in the milking order either directly after cow Y or directly before cow Y.

Please help Farmer John determine an ordering of his cows that satisfies all of these required constraints. It is guaranteed that an ordering is always possible. If several orderings work, then please output the one that is alphabetically first. That is, the first cow should have the alphabetically lowest name of all possible cows that could appear first in any valid ordering. Among all orderings starting with this same alphabetically-first cow, the second cow should be alphabetically lowest among all possible valid orderings, and so on.

Input
The first line of input contains N. The next N lines each contain a sentence describing a constraint in the form “X must be milked beside Y”, where X and Y are names of some of Farmer John’s cows (the eight possible names are listed above).

Output
Please output, using 8 lines, an ordering of cows, one cow per line, satisfying all constraints. If multiple orderings work, output the one that is alphabetically earliest.

Example
inputCopy
3
Buttercup must be milked beside Bella
Blue must be milked beside Bella
Sue must be milked beside Beatrice
outputCopy
Beatrice
Sue
Belinda
Bessie
Betsy
Blue
Bella
Buttercup

分析

用map建立string到int的映射
而string数组相当于int到string的映射
根据题目信息建立邻接表,把必定相邻的两个点连边
再用next_permutation字典序从小到大枚举所有排列,暴力判断是否满足上述相邻条件
输出第一个满足题意的就是字典序最小的答案

代码

#include<iostream>
using namespace std;
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<vector>
typedef long long ll;

const int INF=1e9;
//int f[10],order[10],A=0;
int a[10]={0,1,2,3,4,5,6,7,8};
string s[10]={
" ",
"Beatrice",
"Belinda",
"Bella",
"Bessie",
"Betsy",
"Blue",
"Buttercup",
"Sue"
};
map<string,int> mp;
vector<int> g[10];

bool check(int x,int y){
	for(int i=1;i<=8;i++){
		if(a[i]==x){
			if(i!=1&&a[i-1]==y) return 1;
			if(a[i+1]==y) return 1;
		}
	}
	return 0;
}

int main()
{
	int n; cin>>n;
	
	for(int i=1;i<=8;i++) mp[s[i]]=i;
	string aa,b,x;
	for(int i=1;i<=n;i++){
		cin>>aa;
		for(int j=1;j<=4;j++) cin>>x;
		cin>>b;
		
		int s=mp[aa],t=mp[b];
		g[s].push_back(t);//邻接表 
	} 
	
	//int ok=0;
	do{
		int ok=1;
		for(int i=1;i<=8;i++){
			for(int j=0;j<g[i].size();j++){	
					
				if( !check(i,g[i][j]) ) {ok=0; break;}
			}
			if(!ok)break;
		}
		
		if(ok) break;
		
	}while(next_permutation(a+1,a+1+8));
	
	//for(int i=1;i<=8;i++) for(int j=0;j<g[i].size();j++) cout<<i<<" "<<g[i][j]<<endl;
	for(int i=1;i<=8;i++) cout<<s[a[i]]<<endl;
	
	
	return 0;
} 



题面

I. Where Am I?
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Farmer John has gone out for a walk down the road and thinks he may now be lost! Along the road there are N farms (1≤N≤100) in a row. Farms unfortunately do not have house numbers, making it hard for Farmer John to figure out his location along the road. However, each farm does have a colorful mailbox along the side of the road, so Farmer John hopes that if he looks at the colors of the mailboxes nearest to him, he can uniquely determine where he is.

Each mailbox color is specified by a letter in the range A…Z, so the sequence of N mailboxes down the road can be represented by a string of length N containing letters in the range A…Z. Some mailboxes may have the same colors as other mailboxes. Farmer John wants to know what is the smallest value of K such that if he looks at any sequence of K consecutive mailboxes, he can uniquely determine the location of that sequence along the road.

For example, suppose the sequence of mailboxes along the road is ‘ABCDABC’. Farmer John cannot set K=3, since if he sees ‘ABC’, there are two possible locations along the road where this consecutive set of colors might be. The smallest value of K that works is K=4, since if he looks at any consecutive set of 4 mailboxes, this sequence of colors uniquely determines his position along the road.

Input
The first line of input contains N, and the second line contains a string of N characters, each in the range A…Z.

Output
Print a line containing a single integer, specifying the smallest value of K that solves Farmer John’s problem.

Example
inputCopy
7
ABCDABC
outputCopy
4

分析

n很小,暴力截取子串进行判断就ok了

代码

#include<iostream>
using namespace std;
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
typedef long long ll;

const int INF=1e9;
const int MAX_N=105;

int main()
{
	int n,k=0; scanf("%d",&n);
	string s;
	cin>>s;
	int ok=0;
	
	for(int len=n-1;len>=1;len--){
		for(int i=0;i+len-1<=n-1;i++){
			
			for(int j=i+1;j+len-1<=n-1;j++){
				if(s.substr(i,len)==s.substr(j,len)) { k=len; ok=1; break; }
			}
			if(ok) break;
		}
		if(ok) break;
	}
	printf("%d\n",k+1);
	
	
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值