关闭

Codeforces Round #438 by Sberbank and Barcelona Bootcamp (Div. 1 + Div. 2 combined) A-C题解

标签: acmcodeforces
672人阅读 评论(0) 收藏 举报
分类:
A. Bark to Unlock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.

Mu-mu's enemy Kashtanka wants to unlock Mu-mu's phone to steal some sensible information, but it can only bark n distinct words, each of which can be represented as a string of two lowercase English letters. Kashtanka wants to bark several words (not necessarily distinct) one after another to pronounce a string containing the password as a substring. Tell if it's possible to unlock the phone in this way, or not.

Input

The first line contains two lowercase English letters — the password on the phone.

The second line contains single integer n (1 ≤ n ≤ 100) — the number of words Kashtanka knows.

The next n lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct.

Output

Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise.

You can print each letter in arbitrary case (upper or lower).

Examples
input
ya
4
ah
oy
to
ha
output
YES
input
hp
2
ht
tp
output
NO
input
ah
1
ha
output
YES
Note

In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES".

In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" producing "http", but it doesn't contain the password "hp" as a substring.

In the third example the string "hahahaha" contains "ah" as a substring.

给定字符串,给定n个字符串,问是否可以组成子串同原串一样,每个字符串的长度都是2.

模拟。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=100005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}

int main()
{
	int i,j,k,n;
	char a,b,c,d;
	string map[105];
	while(cin>>a>>b)
	{
		cin>>n;
		int flag=0,flag2=0;
		for(i=0;i<n;i++)
		{
			cin>>map[i];
		}
		
		for(i=0;i<n&&!flag;i++)
		{
			if(map[i][0]==a&&map[i][1]==b)
			{
				flag=1;
				break;
			}
			for(j=0;j<n&&!flag;j++)
			{
				if(map[i][1]==a&&map[j][0]==b)
				{
					flag=1;
					break;
				}
			}
		}
		if(flag)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}

B. Race Against Time
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other.

The entire universe turned into an enormous clock face with three hands — hour, minute, and second. Time froze, and clocks now show the time h hours, m minutes, s seconds.

Last time Misha talked with the coordinator at t1 o'clock, so now he stands on the number t1 on the clock face. The contest should be ready by t2 o'clock. In the terms of paradox it means that Misha has to go to number t2 somehow. Note that he doesn't have to move forward only: in these circumstances time has no direction.

Clock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way).

Given the hands' positions, t1, and t2, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from t1 to t2 by the clock face.

Input

Five integers hmst1t2 (1 ≤ h ≤ 120 ≤ m, s ≤ 591 ≤ t1, t2 ≤ 12t1 ≠ t2).

Misha's position and the target time do not coincide with the position of any hand.

Output

Print "YES" (quotes for clarity), if Misha can prepare the contest on time, and "NO" otherwise.

You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").

Examples
input
12 30 45 3 11
output
NO
input
12 0 1 12 1
output
YES
input
3 47 0 4 9
output
YES
Note

The three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same.


给定一个时间,这个时间的指针在钟表上是固定的,给定两个整数点,可以顺时针,也可以逆时针的跑,问途中是否会遇到钟表指针。

转化为角度,模拟。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=100005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}

int solve(double a,double b,double c)
{
	if(c>=a&&c<=b)
	return 1;
	else
	return 0;
}

int main()
{
	int h,t1,t2,i,j,k;
	int m,s;
	while(cin>>h>>m>>s>>t1>>t2)
	{
		if(t1>t2)
		swap(t1,t2);
		
		h=h%12;
		double a=t1*30.0;
		double b=t2*30.0;
		double c=(h*30)+m*0.5+s/120.0;
		double d=m*6.0+s*1.0/60.0;
		double e=s*6.0;
		
		int ans=0;
		ans+=solve(a,b,c);
		ans+=solve(a,b,e);
		ans+=solve(a,b,d);
		if(ans==0||ans==3)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
	}
	return 0;
}

C. Qualification Rounds
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quarter-finals. They have a bank of nproblems, and they want to select any non-empty subset of it as a problemset.

k experienced teams are participating in the contest. Some of these teams already know some of the problems. To make the contest interesting for them, each of the teams should know at most half of the selected problems.

Determine if Snark and Philip can make an interesting problemset!

Input

The first line contains two integers nk (1 ≤ n ≤ 1051 ≤ k ≤ 4) — the number of problems and the number of experienced teams.

Each of the next n lines contains k integers, each equal to 0 or 1. The j-th number in the i-th line is 1 if j-th team knows i-th problem and 0 otherwise.

Output

Print "YES" (quotes for clarity), if it is possible to make an interesting problemset, and "NO" otherwise.

You can print each character either upper- or lowercase ("YeS" and "yes" are valid when the answer is "YES").

Examples
input
5 3
1 0 1
1 1 0
1 0 0
1 0 0
1 0 0
output
NO
input
3 2
1 0
1 1
0 1
output
YES
Note

In the first example you can't make any interesting problemset, because the first team knows all problems.

In the second example you can choose the first and the third problems.


给定n门课,有k个有经验的人,出一套题,要使每个队至少有一半以上的人没做过,问最后是否存在这样的题。

由于k最大为4,也就是状态最多为2^4=16种,可是n最大是10^5这样会有很多重复状态的题,所以将其去重,进行状态压缩,枚举两题出现这种情况即可,两层循环最多16*16的耗时,只需&运算一下,若等于0,则输出答案YES,否则输出NO。

代码实现:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
int visit[20];

int main()
{
	int n,k,i,j,a;
	while(cin>>n>>k)
	{
		mset(visit,0);
		for(i=0;i<n;i++)
		{
			int ans=0;
			for(j=0;j<k;j++)
			{
				cin>>a;
				ans=ans*2+a;
			}
			visit[ans]++;
		}
		
		for(i=0;i<pow(2,k);i++)
		{
			for(j=0;j<pow(2,k);j++)
			{
				if(visit[i]>0&&visit[j]>0&&((i&j)==0))
				{
					cout<<"YES"<<endl;
					goto q;
				}
			}
		}
		cout<<"NO"<<endl;
q:		;
	}
	return 0;
}


0
0
查看评论

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)A+B

A. Gotta Catch Em’ All!time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:standard outputBash wants to become a...
  • qq_33183401
  • qq_33183401
  • 2017-01-16 17:12
  • 240

ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) E. The Holmes Children

E. The Holmes Children time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard ...
  • mystery_guest
  • mystery_guest
  • 2017-03-04 18:40
  • 284

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) A. Gotta Catch Em' All!(水题)

题目链接:http://codeforces.com/contest/757/problem/AA. Gotta Catch Em’ All! time limit per test1 second memory limit per test256 megabytes inputstandar...
  • qq_32866009
  • qq_32866009
  • 2017-01-13 09:17
  • 200

Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem(二分染色?/2-sat,好题)

题目链接 D. The Door Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard input output stan...
  • fouzhe
  • fouzhe
  • 2017-02-24 20:57
  • 283

解题报告:Codeforces Round #433 (Div. 2) E. Boredom ( 离线处理+树状数组)

题目链接 题意: n*n的矩阵,有n个不同行列的格子染色,染色的格子两两之间组成的矩阵定义为beautiful。 q组询问,每次给出一个矩阵,询问与它相交的beautiful的矩阵的数目 n,q 思路: 每次查询分成九个矩阵,只需要知道各个矩阵中的染色的点数,即可得出答案 其中...
  • qq_32570675
  • qq_32570675
  • 2017-09-07 09:56
  • 336

【解题报告】Codeforces Round #401 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017-02-25 17:55
  • 411

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作,都会拿走一个横行,一个竖行。 所以一共会操作min(横行,竖行)次。 #include #include #include #include #include #include #include #include #pragma commen...
  • rowanhaoa
  • rowanhaoa
  • 2014-07-25 14:39
  • 1473

Codeforces Round #400 (Div. 1 + Div. 2, combined)A. A Serial Killer【模拟】

A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Our belove...
  • mengxiang000000
  • mengxiang000000
  • 2017-02-24 12:00
  • 292

Codeforces Round #395 (Div. 2)(ABCD)

ps:打完这场cf才知道自己真的很菜,还是停留在AB题的水平,有时候CD其实很简单,但就是想不到,别人一眼看出而我就是想不到,有时候想到了点子上但就是突破不了 题目链接:  Codeforces Round #395 (Div. 2) A题: &...
  • qq_34731703
  • qq_34731703
  • 2017-02-03 12:50
  • 492

Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) -- C. Destroying Array (并查集)

大体题意: 给你n 个数字,告诉你破坏每个数字的顺序,每次破坏后你要求一个线段,使得线段不包含破坏的点,并且线段上的值和最大? 思路: 正着删除感觉不好处理,就反过来求了,相当于从一个空序列往上面添加数值,加一次,求一次线段的最大和,在加之前,我们可以先定义一个变量Max 表示之前的操作所得到...
  • aozil_yang
  • aozil_yang
  • 2016-10-02 00:37
  • 372
    个人资料
    • 访问:59349次
    • 积分:2088
    • 等级:
    • 排名:千里之外
    • 原创:154篇
    • 转载:1篇
    • 译文:0篇
    • 评论:17条
    联系方式
    欢迎谈论交流:1245985209
    博客专栏