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.
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 «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.
5 2 4 5 1 3
YES
5 5 5 5 5 1
NO
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.
穷举,水题
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=1000005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int a[maxn];
int main() {
int n,i,j,k,ans;
ans=0;
scanf("%d",&n);
for (i=1;i<=n;i++) scanf("%d",&a[i]);
for (i=1;i<=n;i++) {
j=a[i];k=a[j];
if (a[k]==i&&i!=j&&j!=k&&i!=k) ans++;
}
if (ans>0) cout << "YES"; else cout << "NO";
return 0;
}
O(n)枚举,同样水题
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
ll a[maxn];
int main() {
ll n,k,i,j,ans=-1;
scanf("%I64d%I64d",&n,&k);
for (i=1;i<=k;i++) {
scanf("%I64d",&a[i]);
ll now;
now=n/a[i]*a[i];
if (now>ans) {
ans=now;j=i;
}
}
printf("%I64d %I64d",j,n/a[j]);
return 0;
}
模拟题,可以发现第一个区域的时间每向后推移一个小时,满足条件的区间就向左移一格。可以用前缀和求每一次的人数,取个最大值就好。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
int a[maxn],sum[maxn];
int main() {
int n,i,ans,mx,s,f;
scanf("%d",&n);
sum[0]=0;
for (i=1;i<=n;i++) {
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
ans=1;mx=-1;
scanf("%d%d",&s,&f);
int l,r;
l=s;r=f-1;
for (i=1;i<=n;i++) {
int now=0;
if (l<=r) now=sum[r]-sum[l-1]; else now=sum[r]+sum[n]-sum[l-1];
if (now>mx) {
mx=now;ans=i;
}
l--;r--;
if (l==0) l+=n;
if (r==0) r+=n;
}
printf("%d\n",ans);
return 0;
}
Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length nconsisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (c1, c2) (where c1 and c2 are some lowercase English letters), which can arbitrary number of times transform a single letter c1 to c2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells.
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of the letterings.
The second line contains a string with length n, consisting of lowercase English letters — the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format.
In the first line output a single integer — the minimum amount of mana t required for rescuing love of Valya and Tolya.
In the next t lines output pairs of space-separated lowercase English letters — spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any.
3 abb dad
2 a d b a
8 drpepper cocacola
7 l e e d d c c p p o o r r a
In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'.
对于每两个对应的字母,用并查集合并它们。最后按照并查集,可以把26个字母分为若干独立的集合。每个集合当中,要连最少的边使它们相互转化,最简单的方法就是用n-1条边把n个字母相连。最后把所有边的数量加起来可以得到答案。
#include <cstdio>
#include <iostream>
#include <string.h>
#include <string>
#include <map>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <stack>
#include <iomanip>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
const int maxn=100005,inf=0x3f3f3f3f;
const ll llinf=0x3f3f3f3f3f3f3f3f;
const ld pi=acos(-1.0L);
char s[maxn],t[maxn];
int f[105];
vector<int> v[maxn];
int find(int now) {
if (f[now]==now) return now; else {
f[now]=find(f[now]);
return f[now];
}
}
int main() {
int len,i,j,ans=0;
scanf("%d",&len);
scanf("%s",s+1);
scanf("%s",t+1);
for (i=1;i<=26;i++) {
f[i]=i;
}
for (i=1;i<=len;i++) {
int fa=find(s[i]-'a'+1),fb=find(t[i]-'a'+1);
if (fa==fb) continue;
f[fa]=fb;
}
for (i=1;i<=26;i++) {
int fa=find(i);
v[fa].push_back(i);
}
for (i=1;i<=26;i++) if (v[i].size()>0) ans+=v[i].size()-1;
printf("%d\n",ans);
for (i=1;i<=26;i++) {
if (v[i].size()>1) {
for (j=1;j<v[i].size();j++) {
printf("%c %c\n",(char)(v[i][j-1]+'a'-1),(char)(v[i][j]+'a'-1));
}
}
}
return 0;
}
You are given a multiset S consisting of positive integers (initially empty). There are two kind of queries:
- Add a positive integer to S, the newly added integer is not less than any number in it.
- Find a subset s of the set S such that the value is maximum possible. Here max(s) means maximum value of elements in s, — the average value of numbers in s. Output this maximum possible value of .
The first line contains a single integer Q (1 ≤ Q ≤ 5·105) — the number of queries.
Each of the next Q lines contains a description of query. For queries of type 1 two integers 1 and x are given, where x (1 ≤ x ≤ 109) is a number that you should add to S. It's guaranteed that x is not less than any number in S. For queries of type 2, a single integer 2 is given.
It's guaranteed that the first query has type 1, i. e. S is not empty when a query of type 2 comes.
Output the answer for each query of the second type in the order these queries are given in input. Each number should be printed in separate line.
Your answer is considered correct, if each of your answers has absolute or relative error not greater than 10 - 6.
Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if .
6 1 3 2 1 4 2 1 8 2
0.0000000000 0.5000000000 3.0000000000
4 1 1 1 4 1 5 2
2.0000000000
E题贪心