赞
踩
A. Circle Metro
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
The circle line of the Roflanpolis subway has n stations.
There are two parallel routes in the subway. The first one visits stations in order 1→2→…→n→1→2→… (so the next stop after station x is equal to (x+1) if x<n and 1 otherwise). The second route visits stations in order n→(n−1)→…→1→n→(n−1)→… (so the next stop after station x is equal to (x−1) if x>1 and n otherwise). All trains depart their stations simultaneously, and it takes exactly 1 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 a and will exit the subway when his train reaches station x.
Coincidentally, Vlad is currently in a train of the second route at station b and he will exit the subway when his train reaches station y.
Surprisingly, all numbers a,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 n, a, x, b, y (4≤n≤100, 1≤a,x,b,y≤n, all numbers among a, x, b, y 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
inputCopy
5 1 4 3 2
outputCopy
YES
inputCopy
10 2 1 9 10
outputCopy
NO
Note
In the first example, Daniel and Vlad start at the stations (1,3). One minute later they are at stations (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),
after 1 minute (3,8),
after 2 minutes (4,7),
after 3 minutes (5,6),
after 4 minutes (6,5),
after 5 minutes (7,4),
after 6 minutes (8,3),
after 7 minutes (9,2),
after 8 minutes (10,1),
after 9 minutes (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.
题意:车站有n(4<=n<=100)个站点,两个人坐车,一个人按照x->a+1->…n->1…->a的路线移动,另一个按照y->y-1->…1->n…->b,问是否会存在两个人在同一站点的时刻
题解:n范围很小直接模拟即可
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define debug(x) cout<<#x<<" is "<<x<<endl; int w[105]; int main(){ int n,a,x,b,y; cin>>n>>a>>x>>b>>y; if(x<a){ x=x+n; } int t=1; for(int i=a;i<=x;i++){ int xx=i%n; if(xx==0)xx=n; w[xx]=t; t++; } if(y>b)b=b+n; int t2=1; int f=0; for(int i=b;i>=y;i--){ int xx=i%n; if(xx==0)xx=n; if(w[xx]==t2){ f=1; // debug(t2); // debug(xx); break; } t2++; } if(f)printf("YES\n"); else printf("NO\n"); return 0; }
B. Pairs
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Toad Ivan has m pairs of integers, each integer is between 1 and n, inclusive. The pairs are (a1,b1),(a2,b2),…,(am,bm).
He asks you to check if there exist two integers x and y (1≤x<y≤n) such that in each given pair at least one integer is equal to x or y.
Input
The first line contains two space-separated integers n and m (2≤n≤300000, 1≤m≤300000) — the upper bound on the values of integers in the pairs, and the number of given pairs.
The next m lines contain two integers each, the i-th of them contains two space-separated integers ai and bi (1≤ai,bi≤n,ai≠bi) — the integers in the i-th pair.
Output
Output “YES” if there exist two integers x and y (1≤x<y≤n) such that in each given pair at least one integer is equal to x or y. Otherwise, print “NO”. You can print each letter in any case (upper or lower).
Examples
inputCopy
4 6
1 2
1 3
1 4
2 3
2 4
3 4
outputCopy
NO
inputCopy
5 4
1 2
2 3
3 4
4 5
outputCopy
YES
inputCopy
300000 5
1 2
1 2
1 2
1 2
1 2
outputCopy
YES
Note
In the first example, you can’t choose any x, y because for each such pair you can find a given pair where both numbers are different from chosen integers.
In the second example, you can choose x=2 and y=4.
In the third example, you can choose x=1 and y=2.
题意:有m个数对(ai,bi),问是否存在两个数x,y使每个数对至少有一个数字=x或者=y
题解:通过枚举第一个数对中的数字可以确定后面的数字。即如果a1=x,那么把所有不含a1的数对存起来,判断数对中数字最多的数量是否=不含a1的数的总个数,同理可以枚举b1=x。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。