Codeforces Round #394(Div. 2)B. Dasha and friends【思维+暴力】
2017-02-01 17:23
656 查看
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.
![](http://codeforces.com/predownloaded/e5/9e/e59eb34dbd789f102f8803e5c23abaf7bcc7973c.png)
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 ≤ 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.
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
Output
Input
Output
Input
Output
Note
The first test is analyzed in the statement.
题目大意:
有两个人在任意一个起点逆时针奔跑,这个圈长度为L,其中有N个障碍点,给出每个人逆时针奔跑的时候跑多长距离会遇到一个障碍物,问两人跑的是否是一个地图。
思路:
1、无论逆时针跑还是顺时针跑,如果想要跑的是一个地图,那么对于每两个障碍相邻的距离肯定是相等的。
2、那么我们接下来O(n)求得两个人跑的地图的相邻两个障碍物的距离。
再接下来可以用最小表示法来判断两个数组是否一致。
但是因为N比较小,所以这里直接暴力O(n^2)判断即可。
将第一个数组一个一个位子推动。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
#define mod 1000000007
int chaa[1000];
int chab[1000];
int a[1000];
int b[1000];
int c[1000];
int main()
{
int n,l;
while(~scanf("%d%d",&n,&l))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++)
{
chaa[i]=a[i]-a[i+1];
if(i==n)chaa[i]=a[i]-a[1];
if(chaa[i]<0)chaa[i]+=l;
}
for(int i=1;i<=n;i++)
{
chab[i]=b[i]-b[i+1];
if(i==n)chab[i]=b[i]-b[1];
if(chab[i]<0)chab[i]+=l;
}
int ok=0;
for(int i=1;i<=n;i++)
{
int cnt=1;
for(int j=i;j<=n;j++)
{
c[cnt++]=chaa[j];
}
for(int j=1;j<i;j++)
{
c[cnt++]=chaa[j];
}
int flag=0;
for(int j=1;j<=n;j++)
{
if(chab[j]==c[j])continue;
else flag=1;
}
if(flag==0)ok=1;
}
if(ok==1)printf("YES\n");
else printf("NO\n");
}
}
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.
![](http://codeforces.com/predownloaded/e5/9e/e59eb34dbd789f102f8803e5c23abaf7bcc7973c.png)
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 ≤ 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.
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 8 2 4 6 1 5 7
Output
YES
Input
4 9 2 3 5 8 0 1 3 6
Output
YES
Input
2 4 1 3 1 2
Output
NO
Note
The first test is analyzed in the statement.
题目大意:
有两个人在任意一个起点逆时针奔跑,这个圈长度为L,其中有N个障碍点,给出每个人逆时针奔跑的时候跑多长距离会遇到一个障碍物,问两人跑的是否是一个地图。
思路:
1、无论逆时针跑还是顺时针跑,如果想要跑的是一个地图,那么对于每两个障碍相邻的距离肯定是相等的。
2、那么我们接下来O(n)求得两个人跑的地图的相邻两个障碍物的距离。
再接下来可以用最小表示法来判断两个数组是否一致。
但是因为N比较小,所以这里直接暴力O(n^2)判断即可。
将第一个数组一个一个位子推动。
Ac代码:
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
#define mod 1000000007
int chaa[1000];
int chab[1000];
int a[1000];
int b[1000];
int c[1000];
int main()
{
int n,l;
while(~scanf("%d%d",&n,&l))
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++)
{
chaa[i]=a[i]-a[i+1];
if(i==n)chaa[i]=a[i]-a[1];
if(chaa[i]<0)chaa[i]+=l;
}
for(int i=1;i<=n;i++)
{
chab[i]=b[i]-b[i+1];
if(i==n)chab[i]=b[i]-b[1];
if(chab[i]<0)chab[i]+=l;
}
int ok=0;
for(int i=1;i<=n;i++)
{
int cnt=1;
for(int j=i;j<=n;j++)
{
c[cnt++]=chaa[j];
}
for(int j=1;j<i;j++)
{
c[cnt++]=chaa[j];
}
int flag=0;
for(int j=1;j<=n;j++)
{
if(chab[j]==c[j])continue;
else flag=1;
}
if(flag==0)ok=1;
}
if(ok==1)printf("YES\n");
else printf("NO\n");
}
}
相关文章推荐
- Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力
- Codeforces Round #394 (Div. 2)E. Dasha and Puzzle【思维+Dfs构造】
- Codeforces Round #394 (Div. 2) B. Dasha and friends
- Codeforces Round #394 (Div. 2)C. Dasha and Password【暴力】
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(想法题,构造题)
- Codeforces Round #394 (Div. 2) F. Dasha and Photos(二位前缀和+线段树/二维前缀和,好题)
- Codeforces Round #394 (Div. 2) D. Dasha and Very Difficult Problem 贪心,二分
- Codeforces Round #394 (Div. 2) A. Dasha and Stairs
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(分形)
- Codeforces Round #394 (Div. 2) A. Dasha and Stairs 水题
- 【Codeforces Round 269 (Div 2)C】【暴力】MUH and Sticks 纸牌搭楼n张牌恰好搭楼数
- Codeforces Round #253 (Div. 2) E. Artem and Array (思维)
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi 暴力
- 【Codeforces Round 330 (Div 2)B】【数值统计 端点思维】Pasha and Phone 电话号码 每块数是x倍数却不能以y开头方案数
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs
- 【Codeforces Round 272 (Div 2)B】【暴力dfs or 组合数】Dreamoon and WiFi 问号填加减方案数使得最后恰好增量为aim
- Codeforces Round #312 (Div. 2)-B. Amr and The Large Array-暴力
- Codeforces Round #325 (Div. 2) B. Laurenty and Shop(暴力)
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B. Bear and Three Musketeers(STL_暴力)
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)