滴答滴答---题目小连接
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The circle line of the Roflanpolis subway has nn stations.
There are two parallel routes in the subway. The first one visits stations in order 1→2→…→n→1→2→…1→2→…→n→1→2→… (so the next stop after station xx is equal to (x+1)(x+1) if x<nx<n and 11 otherwise). The second route visits stations in order n→(n−1)→…→1→n→(n−1)→…n→(n−1)→…→1→n→(n−1)→… (so the next stop after station xx is equal to (x−1)(x−1) if x>1x>1 and nn otherwise). All trains depart their stations simultaneously, and it takes exactly 11 minute to arrive at the next station.
Two toads live in this city, their names are Daniel and Vlad.
Daniel is currently in a train of the first route at station aa and will exit the subway when his train reaches station xx.
Coincidentally, Vlad is currently in a train of the second route at station bb and he will exit the subway when his train reaches station yy.
Surprisingly, all numbers a,x,b,ya,x,b,y are distinct.
Toad Ilya asks you to check if Daniel and Vlad will ever be at the same station at the same time during their journey. In other words, check if there is a moment when their trains stop at the same station. Note that this includes the moments when Daniel or Vlad enter or leave the subway.
Input
The first line contains five space-separated integers nn, aa, xx, bb, yy (4≤n≤1004≤n≤100, 1≤a,x,b,y≤n1≤a,x,b,y≤n, all numbers among aa, xx, bb, yy are distinct) — the number of stations in Roflanpolis, Daniel's start station, Daniel's finish station, Vlad's start station and Vlad's finish station, respectively.
Output
Output "YES" if there is a time moment when Vlad and Daniel are at the same station, and "NO" otherwise. You can print each letter in any case (upper or lower).
Examples
input
Copy
5 1 4 3 2
output
Copy
YES
input
Copy
10 2 1 9 10
output
Copy
NO
Note
In the first example, Daniel and Vlad start at the stations (1,3)(1,3). One minute later they are at stations (2,2)(2,2). They are at the same station at this moment. Note that Vlad leaves the subway right after that.
Consider the second example, let's look at the stations Vlad and Daniel are at. They are:
- initially (2,9)(2,9),
- after 11 minute (3,8)(3,8),
- after 22 minutes (4,7)(4,7),
- after 33 minutes (5,6)(5,6),
- after 44 minutes (6,5)(6,5),
- after 55 minutes (7,4)(7,4),
- after 66 minutes (8,3)(8,3),
- after 77 minutes (9,2)(9,2),
- after 88 minutes (10,1)(10,1),
- after 99 minutes (1,10)(1,10).
After that, they both leave the subway because they are at their finish stations, so there is no moment when they both are at the same station.
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1010;
int l[maxn];
int r[maxn];
int main()
{
int n,a,b,x,y;
scanf("%d%d%d%d%d",&n,&a,&x,&b,&y);
int flas=0;
int l1,r1;
l1=r1=0;
for(int i=a; i<=n; i++)
{
l[l1++]=i;
}
for(int i=1; i<a; i++)
{
l[l1++]=i;
}
for(int i=b; i>=1; i--)
{
r[r1++]=i;
}
for(int i=n; i>b; i--)
{
r[r1++]=i;
}
for(int i=1; i<=n; i++)
{
if(l[i]==r[i])
{
flas=1;
break;
}
else if(l[i]==x||r[i]==y)
break;
}
cout<<(flas==1?"YES":"NO")<<endl;
return 0;
}
方法二:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,a,b,c,d;
cin>>n>>a>>b>>c>>d;
while(a!=b&&c!=d){
if(a==n)a=1;
else a++;
if(c==1)c=n;
else c--;
if(a==c)return puts("YES"),0;
}
puts("NO");
}