题目链接:点击打开链接
On her way to programming school tiger Dasha faced her first test — a huge staircase!
![](https://i-blog.csdnimg.cn/blog_migrate/b7a851f57b86c4cf85f430113a74dfac.png)
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.
In the only line you are given two integers a, b (0 ≤ a, b ≤ 100) — the number of even and odd steps, accordingly.
In the only line print "YES", if the interval of steps described above exists, and "NO" otherwise.
2 3
YES
3 1
NO
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: 1, 3 and 5.
大意:给出 a 个偶数,b 个奇数。问是否存在合法的连续序列
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a,b;
int main()
{
while(~scanf("%d%d",&a,&b))
{
if(a==0&&b==0)
puts("NO");
else if(abs(a-b)<=1)
puts("YES");
else
puts("NO");
}
return 0;
}
题目链接: 点击打开链接
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.
![](https://i-blog.csdnimg.cn/blog_migrate/21725f14041df8b939b38c5eb31ff3f3.png)
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.
The first line contains two integers n and L (1 ≤ n ≤ 50, n ≤ 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.
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).
3 8 2 4 6 1 5 7
YES
4 9 2 3 5 8 0 1 3 6
YES
2 4 1 3 1 2
NO
The first test is analyzed in the statement.
大意:有 n 个障碍遍布在一个总长为 L 的环形轨迹上。给出两个人距每个障碍的距离(沿逆时针方向),问是否存在合法的障碍排列。
思路:就是找到每个障碍的间隔距离,这个容易想。刚开始我以为找到之后两个 sort 一下,比较只要相同就是合法的,但是挂了。后来想了好久,才明白不能这样搞。需要暴力查找
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,l,x[55],y[55],nx[55],ny[55];
int main()
{
while(~scanf("%d%d",&n,&l))
{
for(int i=1;i<=n;i++)
scanf("%d",x+i);
for(int i=1;i<=n;i++)
scanf("%d",y+i);
nx[1]=l-x[n]+x[1];
ny[1]=l-y[n]+y[1];
for(int i=2;i<=n;i++)
{
nx[i]=x[i]-x[i-1];
ny[i]=y[i]-y[i-1];
}
bool flag=0;
for(int i=0;i<n;i++)
{
bool temp=1;
for(int j=1;j<=n;j++)
{
int xx=j;
int yy=j+i;
if(yy>n)
yy-=n;
if(nx[xx]!=ny[yy])
{
temp=0;
break;
}
}
if(temp)
{
flag=1;
break;
}
}
if(flag)
puts("YES");
else
puts("NO");
}
return 0;
}