Codeforces Round #394 (Div. 2) A---D

来源:互联网 发布:dnspod和阿里云解析 编辑:程序博客网 时间:2024/06/05 21:04
A. Dasha and Stairs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

On her way to programming school tiger Dasha faced her first test — a huge staircase!

The steps were numbered from one to infinity. As we know, tigers are very fond of all striped things, it is possible that it has something to do with their color. So on some interval of her way she calculated two values — the number of steps with even and odd numbers.

You need to check whether there is an interval of steps from the l-th to the r-th (1 ≤ l ≤ r), for which values that Dasha has found are correct.

Input

In the only line you are given two integers ab (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.

Output

In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.

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

In the first example one of suitable intervals is from 1 to 5. The interval contains two even steps — 2 and 4, and three odd: 13 and 5.

题意: 给你a b 分别为任意一段区间中偶数和奇数的个数 问你a b 可不可能同时出现在一个区间里

思路:写写区间就可以发现 直接判断 fabs(a-b)<=1 即可

注意特判 a 和 b 同时为0的情况  

#include<bits/stdc++.h>using namespace std;int main(){int a,b;cin>>a>>b;if(a==0&&b==0)printf("NO");else{if(fabs(b-a)<=1)printf("YES");elseprintf("NO");}return 0;  } 



B. Dasha and friends
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the following situation:

The track is the circle with length L, in distinct points of which there are n barriers. Athlete always run the track in counterclockwise direction if you look on him from above. All barriers are located at integer distance from each other along the track.

Her friends the parrot Kefa and the leopard Sasha participated in competitions and each of them ran one lap. Each of the friends started from some integral point on the track. Both friends wrote the distance from their start along the track to each of the n barriers. Thus, each of them wrote n integers in the ascending order, each of them was between 0 and L - 1, inclusively.

Consider an example. Let L = 8, blue points are barriers, and green points are Kefa's start (A) and Sasha's start (B). Then Kefa writes down the sequence [2, 4, 6], and Sasha writes down [1, 5, 7].

There are several tracks in the country, all of them have same length and same number of barriers, but the positions of the barriers can differ among different tracks. Now Dasha is interested if it is possible that Kefa and Sasha ran the same track or they participated on different tracks.

Write the program which will check that Kefa's and Sasha's tracks coincide (it means that one can be obtained from the other by changing the start position). Note that they always run the track in one direction — counterclockwise, if you look on a track from above.

Input

The first line contains two integers n and L (1 ≤ n ≤ 50n ≤ L ≤ 100) — the number of barriers on a track and its length.

The second line contains n distinct integers in the ascending order — the distance from Kefa's start to each barrier in the order of its appearance. All integers are in the range from 0 to L - 1 inclusively.

The second line contains n distinct integers in the ascending order — the distance from Sasha's start to each barrier in the order of its overcoming. All integers are in the range from 0 to L - 1 inclusively.

Output

Print "YES" (without quotes), if Kefa and Sasha ran the coinciding tracks (it means that the position of all barriers coincides, if they start running from the same points on the track). Otherwise print "NO" (without quotes).

Examples
input
3 82 4 61 5 7
output
YES
input
4 92 3 5 80 1 3 6
output
YES
input
2 41 31 2
output
NO
Note

The first test is analyzed in the statement.


题目大意:

有两个人在任意一个起点逆时针奔跑,这个圈长度为L,其中有N个障碍点,给出每个人逆时针奔跑的时候跑多长距离会遇到一个障碍物,问两人跑的是否是一个地图。


思路:

对于这个题我们的思想不要局限在两个人的起点到底在哪里,我们可以知道,如果两个人走的是同一个地图的话,那么图中障碍物的相对位置一定是确定的,也就是相邻障碍物之间的距离是一定的,我们根据这个特点来确定是否为一个地图即可;

需要注意的地方:

这里是一个环,第一个和最后一个障碍物的相邻距离需要把握好;另外由于两个人的起点不同,所以所选障碍物的起点不同,在比较的过程中需要移位比较;

#include<bits/stdc++.h>using namespace std;int a[111],b[111];int ans1[111],ans2[111];int n,l;int main(){int i,j;cin>>n>>l;for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=n;i++)scanf("%d",&b[i]);for(i=1;i<n;i++){ans1[i]=a[i+1]-a[i];ans2[i]=b[i+1]-b[i];}ans1[0]=a[1]-a[n]+l;ans2[0]=b[1]-b[n]+l;int flag=0,ok=0;for(i=0;i<n;i++){flag=0;for(j=0;j<n;j++){if(ans2[j]!=ans1[(i+j)%n])//移位比较的处理{flag=1;break;} }  if(!flag) ok=1;}if(ok==1)printf("YES\n");elseprintf("NO\n"); return 0;}


C. Dasha and Password
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length n which satisfies the following requirements:

  • There is at least one digit in the string,
  • There is at least one lowercase (small) letter of the Latin alphabet in the string,
  • There is at least one of three listed symbols in the string: '#', '*', '&'.

Considering that these are programming classes it is not easy to write the password.

For each character of the password we have a fixed string of length m, on each of these n strings there is a pointer on some character. Thei-th character displayed on the screen is the pointed character in the i-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).

During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index m, and when we move it to the right from the position m it moves to the position 1.

You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password.

Input

The first line contains two integers nm (3 ≤ n ≤ 50, 1 ≤ m ≤ 50) — the length of the password and the length of strings which are assigned to password symbols.

Each of the next n lines contains the string which is assigned to the i-th symbol of the password string. Its length is m, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.

You have such input data that you can always get a valid password.

Output

Print one integer — the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password.

Examples
input
3 41**2a3*0c4**
output
1
input
5 5#*&#**a1c&&q2w*#a3c#*&#*&
output
3
Note

In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.

In the second test one of possible algorithms will be:

  • to move the pointer of the second symbol once to the right.
  • to move the pointer of the third symbol twice to the right.
题目大意:

这是一个类似密码锁的东西。

一开始的时候密码设置在第一列,我们可以通过拨动来改变每一行的密码。

合法的密码必须满足三个条件:

①至少有一个数字。

②至少有一个小写字母。

③至少有一个特殊字符(*&#)

问最少拨动多少下就能得到合法的密码。

(只能在给定的每个字符串中取一个字符,这是这道题的关键)

思路:

给定数据比较小,又是个C题我们完全就直接暴力就行,

我们只需要找出其中三行 一个拨动到数字的最小次数 一个拨动到字母 一个拨动到特殊字符,剩下的字符串我们就根本不动,因为这样才最小;即找到该行数字 字母 和特殊字符的位置

注意这三个最小每个都不能再同一行,而且由于我们既可以往左面拨动又可以往右面拨动,.)


#include<stdio.h>  #include<iostream>  #include<string.h>#define inf 0x3f3f3f3f#include<algorithm>using namespace std;char a[55][55];int main(){int n,m;int i,j,k,l;int s1,s2,s3,s;while(~scanf("%d%d",&n,&m)){for(i=1;i<=n;i++){  scanf("%s",a[i]);}s=inf;for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ for(k=1;k<=n;k++){s1=s2=s3=inf;if(i==j||i==k||j==k)continue;for(l=0;l<m;l++){if(a[i][l]>='0'&&a[i][l]<='9'){s1=min(s1,min(l,m-l));} }  for(l=0;l<m;l++) { if(a[j][l]>='a'&&a[j][l]<='z') { s2=min(s2,min(l,m-l)); } } for(l=0;l<m;l++) { if(a[k][l]=='#'||a[k][l]=='*'||a[k][l]=='&') { s3=min(s3,min(l,m-l)); } } if(s1==inf||s2==inf||s3==inf)//这里如果不特判一下相加会爆int continue; s=min(s,s1+s2+s3);}}}  printf("%d\n",s);}return 0; } 


D. Dasha and Very Difficult Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dasha logged into the system and began to solve problems. One of them is as follows:

Given two sequences a and b of length n each you need to write a sequence c of length n, the i-th element of which is calculated as follows: ci = bi - ai.

About sequences a and b we know that their elements are in the range from l to r. More formally, elements satisfy the following conditions:l ≤ ai ≤ r and l ≤ bi ≤ r. About sequence c we know that all its elements are distinct.

Dasha wrote a solution to that problem quickly, but checking her work on the standard test was not so easy. Due to an error in the test system only the sequence a and the compressed sequence of the sequence c were known from that test.

Let's give the definition to a compressed sequence. A compressed sequence of sequence c of length n is a sequence p of length n, so that pi equals to the number of integers which are less than or equal to ci in the sequence c. For example, for the sequencec = [250, 200, 300, 100, 50] the compressed sequence will be p = [4, 3, 5, 2, 1]. Pay attention that in c all integers are distinct. Consequently, the compressed sequence contains all integers from 1 to n inclusively.

Help Dasha to find any sequence b for which the calculated compressed sequence of sequence c is correct.

Input

The first line contains three integers nlr (1 ≤ n ≤ 105, 1 ≤ l ≤ r ≤ 109) — the length of the sequence and boundaries of the segment where the elements of sequences a and b are.

The next line contains n integers a1,  a2,  ...,  an (l ≤ ai ≤ r) — the elements of the sequence a.

The next line contains n distinct integers p1,  p2,  ...,  pn (1 ≤ pi ≤ n) — the compressed sequence of the sequence c.

Output

If there is no the suitable sequence b, then in the only line print "-1".

Otherwise, in the only line print n integers — the elements of any suitable sequence b.

Examples
input
5 1 51 1 1 1 13 1 5 4 2
output
3 1 5 4 2 
input
4 2 93 4 8 93 2 1 4
output
2 2 2 9 
input
6 1 51 1 1 1 1 12 3 5 4 1 6
output
-1
Note

Sequence b which was found in the second sample is suitable, because calculated sequencec = [2 - 3, 2 - 4, 2 - 8, 9 - 9] = [ - 1,  - 2,  - 6, 0] (note that ci = bi - ai) has compressed sequence equals to p = [3, 2, 1, 4].



题意:题中给出a[i]数组和c[i]经过离散化后的数组p[i]  给定区间(l,r) , c[i]=b[i]-a[i],问你在给定区间中能否找到合适的b[i]的值并输入任意一组;

思路: 因为c[i]=b[i]-a[i]   b[i]=c[i]+a[i] 经过离散后 b[i]=p[i]+a[i]+k ; 我们先通过p[i]和a[i]计算出b[i]的值并记录b[i]的区间范围 然后跟给定的区间范围比较,如果 b[i]的区间长度大于给定区间长度的话则一定不满足,否则一定可以找到可行解,在通过比较区间的端点 通过移动区间,将b[i]移动到给定的区间范围内即可

#include<bits/stdc++.h>#define N 100010#define inf 0x3f3f3f3fusing namespace std;int n,l,r;int a[N],b[N],p[N];int main(){while(cin>>n>>l>>r){for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<n;i++)cin>>p[i];int minn=inf,maxn=-1;for(int i=0;i<n;i++){b[i]=a[i]+p[i];minn=min(minn,b[i]);maxn=max(maxn,b[i]);}if(maxn-minn>r-l)cout<<-1<<endl;else{int x=0;if(minn<l)x=l-minn;//区间移动elsex=r-maxn;//区间移动    for(int i=0;i<n;i++)    {    printf(i==n-1?"%d\n":"%d ",b[i]+x);}}}return 0; } 





1 0
原创粉丝点击