Codeforces Round #464 (Div. 2) (ABC)

A. Love Triangle
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 ≤ fi ≤ n and fi ≠ i.

We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.

Input

The first line contains a single integer n (2 ≤ n ≤ 5000) — the number of planes.

The second line contains n integers f1, f2, ..., fn (1 ≤ fi ≤ n, fi ≠ i), meaning that the i-th plane likes the fi-th.

Output

Output «YES» if there is a love triangle consisting of planes on Earth. Otherwise, output «NO».

You can output any letter in lower case or in upper case.

Examples
Input
Copy
5
2 4 5 1 3
Output
YES
Input
Copy
5
5 5 5 5 1
Output
NO
Note

In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.

In second example there are no love triangles.

存在三个点组成一个环的就输出YES否则NO

 1 #include <iostream>
 2 #define N 5005
 3 using namespace std;
 4 int vis[N];
 5 
 6 int n;
 7 int main(){
 8     cin>>n;
 9     for(int i=1;i<=n;i++)
10         cin>>vis[i];
11     for(int i=1;i<=n;i++){
12         if(vis[vis[vis[i]]]==i){
13             cout<<"YES"<<endl;
14             return 0;
15         }
16     }
17     cout<<"NO"<<endl;
18     return 0;
19 }
B. Hamster Farm
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.

Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.

Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.

Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.

Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.

Input

The first line contains two integers N and K (0 ≤ N ≤ 1018, 1 ≤ K ≤ 105) — the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces.

The second line contains K integers a1, a2, ..., aK (1 ≤ ai ≤ 1018 for all i) — the capacities of boxes.

Output

Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input.

If there are many correct answers, output any of them.

Examples
Input
Copy
19 3
5 4 10
Output
2 4
Input
Copy
28 3
5 6 30
Output
1 5

 这其实就可以模拟一下,求余就行了。

 1 #include <iostream>
 2 #define ll long long int
 3 using namespace std;
 4 
 5 ll n,m;
 6 int main(){
 7     ll ans,cnt,vis;
 8     ll x;
 9     cin>>n>>m;
10     cin>>x;
11     ans = n/x;
12     vis = n%x;
13     cnt = 1;
14     for(int i=2;i<=m;i++){
15         cin>>x;
16         ll an = n%x;
17         if(an<vis){
18             ans = n/x;
19             vis = an;
20             cnt = i;
21         }
22     }
23     cout<<cnt<<" "<<ans<<endl;
24     return 0;
25 }
C. Convenient For Everybody
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In distant future on Earth day lasts for n hours and that's why there are n timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to n are used, i.e. there is no time "0 hours", instead of it "n hours" is used. When local time in the 1-st timezone is 1 hour, local time in the i-th timezone is i hours.

Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are ai people from i-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than s hours 00 minutes local time and ends not later than f hours 00 minutes local time. Values s and f are equal for all time zones. If the contest starts at f hours 00 minutes local time, the person won't participate in it.

Help platform select such an hour, that the number of people who will participate in the contest is maximum.

Input

The first line contains a single integer n (2 ≤ n ≤ 100 000) — the number of hours in day.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 10 000), where ai is the number of people in the i-th timezone who want to participate in the contest.

The third line contains two space-separated integers s and f (1 ≤ s < f ≤ n).

Output

Output a single integer — the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them.

Examples
Input
Copy
3
1 2 3
1 3
Output
3
Input
Copy
5
1 2 3 4 1
1 3
Output
4
Note

In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate.

In second example only people from the third and the fourth timezones will participate.

某星球的一天被划分成n个小时,于是这个星球的世界被划分成n个时区,标记为1~n

一个时区的“本地时间”(Local Time)表示为1~n。相邻两个时区的“本地时间”间隔为1小时。当1时区的“本地时间”为1时,i时区的“本地时间”为i

现在举行一场在线赛事,持续时间为1小时。在i时区,将有ai人参赛。若赛事在“本地时间”s之后开始,且在“本地时间”f之前结束,则该时区的人将参赛,否则不参赛。为使得这个世界的参赛人数最多,求赛事开始的最早时间(表示为1时区的“本地时间”)。

在这个问题中,需要定义一个“标准时间”(Standard Time),之后将所有的“本地时间”折算成“标准时间”。

可以假设一个虚拟的0时区,将这个时区的时间定义为“标准时间”。于是:当0时区的“本地时间”(即“标准时间”)为0时,i时区的“本地时间”为i

设赛事开始的“标准时间”为x,于是:若i时区参赛,则有:

s-i≤x

f-i≥x+1;

于是,参赛时区区间为[s-x..f-x-1]。只需枚举x,并将对应区间中的参赛人数求和,即可寻找参赛人数的最大值。可以考虑前缀和,或者滑动窗口(注:窗口的滑动方向应该是循环向左的)。参考程序如下:

 1 #include <iostream>
 2 #define ll long long int
 3 #define N 100005
 4 
 5 using namespace std;
 6 int n,s,f;
 7 ll vis[N];
 8 int main(){
 9     cin>>n;
10     for(int i=1;i<=n;i++){
11         cin>>vis[i%n];
12     }
13     cin>>s>>f;
14     ll ans = 0;
15     int max = 0,cnt = -1;
16     for(int i=s+1;i<=f;i++)
17         ans+=vis[i%n];
18     for(int i=0;i<n;i++){
19         int l = (s-i+n)%n;
20         int r = (f-i+n)%n;
21         ans-=vis[r];
22         ans+=vis[l];
23         if(ans>max){
24             cnt=i;
25             max=ans;
26         }
27     }
28     cout<<cnt+1<<endl;
29     return 0;
30 }
原文地址:https://www.cnblogs.com/zllwxm123/p/8505910.html