Codeforces Round #250 (Div. 2) A-D
A:
注意要是有多選的情況輸出C。
Once upon a time a child got a test consisting of multiple-choice questions as homework. A multiple-choice question consists of four choices: A, B, C and D. Each choice has a description, and the child should find out the only one that is correct.
Fortunately the child knows how to solve such complicated test. The child will follow the algorithm:
- If there is some choice whose description at least twice shorter than all other descriptions, or at least twice longer than all other descriptions, then the child thinks the choice is great.
- If there is exactly one great choice then the child chooses it. Otherwise the child chooses C (the child think it is the luckiest choice).
You are given a multiple-choice questions, can you predict child's choose?
The first line starts with "A." (without quotes), then followed the description of choice A. The next three lines contains the descriptions of the other choices in the same format. They are given in order: B, C, D. Please note, that the description goes after prefix "X.", so the prefix mustn't be counted in description's length.
Each description is non-empty and consists of at most 100 characters. Each character can be either uppercase English letter or lowercase English letter, or "_".
Print a single line with the child's choice: "A", "B", "C" or "D" (without quotes).
A.VFleaKing_is_the_author_of_this_problem B.Picks_is_the_author_of_this_problem C.Picking_is_the_author_of_this_problem D.Ftiasch_is_cute
D
A.ab B.abcde C.ab D.abc
C
A.c B.cc C.c D.c
B
In the first sample, the first choice has length 39, the second one has length 35, the third one has length 37, and the last one has length 15. The choice D (length 15) is twice shorter than all other choices', so it is great choice. There is no other great choices so the child will choose D.
In the second sample, no choice is great, so the child will choose the luckiest choice C.
In the third sample, the choice B (length 2) is twice longer than all other choices', so it is great choice. There is no other great choices so the child will choose B.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7f3f3ff
#define PI 3.1415926535898
const int maxn = 10010;
using namespace std;
struct node
{
int num;
int len;
char str[maxn];
} f[10];
bool cmp(node a, node b)
{
return a.len < b.len;
}
int main()
{
for(int i = 0; i < 4; i++)
{
cin >>f[i].str;
f[i].len = strlen(f[i].str)-2;
f[i].num = i;
}
sort(f, f+4, cmp);
char st;
int cnt = 0;
if(f[0].len*2 <= f[1].len)
{
cnt++;
st = 'A'+f[0].num;
}
if(f[3].len >= 2*f[2].len)
{
cnt++;
st = 'A'+f[3].num;
}
if(cnt == 1)
cout<<st<<endl;
else
cout<<"C"<<endl;
return 0;
}
B:可以從大到小列舉。
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite set of Picks.
Fortunately, Picks remembers something about his set S:
- its elements were distinct integers from 1 to limit;
- the value of was equal to sum; here lowbit(x) equals 2k where k is the position of the first one in the binary representation of x. For example, lowbit(100102) = 102, lowbit(100012) = 12, lowbit(100002) = 100002 (binary representation).
Can you help Picks and find any set S, that satisfies all the above conditions?
The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 105).
In the first line print an integer n (1 ≤ n ≤ 105), denoting the size of S. Then print the elements of set S in any order. If there are multiple answers, print any of them.
If it's impossible to find a suitable set, print -1.
5 5
2 4 5
4 3
3 2 3 1
5 1
-1
In sample test 1: lowbit(4) = 4, lowbit(5) = 1, 4 + 1 = 5.
In sample test 2: lowbit(1) = 1, lowbit(2) = 2, lowbit(3) = 1, 1 + 2 + 1 = 4.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7f3f3ff
#define PI 3.1415926535898
const int maxn = 100010;
using namespace std;
int num[maxn];
int main()
{
int n, m;
while(cin >>n>>m)
{
int t = 0;
for(int i = m; i >= 1; i--)
{
int j;
for(j = 0; j < 22; j++)
if(i&(1<<j))
break;
if((1<<j) <= n)
{
num[t++] = i;
n -= (1<<j);
}
/*
i&(-i)就可以快速求低位的1。這樣就是n,我那樣寫是nlogn。
*/
}
if(n)
cout<<-1<<endl;
else
{
cout<<t<<endl;
for(int i = 0; i < t; i++)
cout<<num[i]<<" ";
cout<<endl;
}
}
}
C:想明白了就很水了啊。
On Children's Day, the child got a toy from Delayyy as a present. However, the child is so naughty that he can't wait to destroy the toy.
The toy consists of n parts and m ropes. Each rope links two parts, but every pair of parts is linked by at most one rope. To split the toy, the child must remove all its parts. The child can remove a single part at a time, and each remove consume an energy. Let's define an energy value of part i as vi. The child spend vf1 + vf2 + ... + vfk energy for removing part i where f1, f2, ..., fk are the parts that are directly connected to the i-th and haven't been removed.
Help the child to find out, what is the minimum total energy he should spend to remove all n parts.
The first line contains two integers n and m (1 ≤ n ≤ 1000; 0 ≤ m ≤ 2000). The second line contains n integers: v1, v2, ..., vn (0 ≤ vi ≤ 105). Then followed m lines, each line contains two integers xi and yi, representing a rope from part xi to part yi (1 ≤ xi, yi ≤ n; xi ≠ yi).
Consider all the parts are numbered from 1 to n.
Output the minimum total energy the child should spend to remove all n parts of the toy.
4 3 10 20 30 40 1 4 1 2 2 3
40
4 4 100 100 100 100 1 2 2 3 2 4 3 4
400
7 10 40 10 20 10 20 80 40 1 5 4 7 4 5 5 2 5 7 6 4 1 6 1 3 4 3 1 4
160
One of the optimal sequence of actions in the first sample is:
- First, remove part 3, cost of the action is 20.
- Then, remove part 2, cost of the action is 10.
- Next, remove part 4, cost of the action is 10.
- At last, remove part 1, cost of the action is 0.
So the total energy the child paid is 20 + 10 + 10 + 0 = 40, which is the minimum.
In the second sample, the child will spend 400 no matter in what order he will remove the parts.
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7f3f3ff
#define PI 3.1415926535898
const int maxn = 100010;
using namespace std;
int num[maxn];
int f[maxn];
int main()
{
int n, m;
while(cin >>n>>m)
{
for(int i = 1; i <= n; i++)
cin >>num[i];
int x, y;
for(int i = 0; i < m; i++)
{
cin >>x>>y;
f[i] = min(num[x], num[y]);
}
int cnt = 0;
for(int i = 0; i < m; i++)
cnt += f[i];
cout<<cnt<<endl;
}
return 0;
}
D:並查集。
以每條邊小的節點建邊,然後從大到小排序,然後判斷是否相關,如果不相關說明只有一種唯一的到達方式求出組合的和。注意資料範圍啊,一定要開LL啊,否則會越界啊,找了一中午的bug才發現是資料範圍的原因啊、、、sad啊、、
Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The i-th area contains ai animals in it. Also there are m roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo from any other area using the roads.
Our child is very smart. Imagine the child want to go from area p to area q. Firstly he considers all the simple routes from p to q. For each route the child writes down the number, that is equal to the minimum number of animals among the route areas. Let's denote the largest of the written numbers as f(p, q). Finally, the child chooses one of the routes for which he writes down the value f(p, q).
After the child has visited the zoo, he thinks about the question: what is the average value of f(p, q) for all pairs p, q (p ≠ q)? Can you answer his question?
The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105). The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105). Then follow m lines, each line contains two integers xi and yi (1 ≤ xi, yi ≤ n; xi ≠ yi), denoting the road between areas xi and yi.
All roads are bidirectional, each pair of areas is connected by at most one road.
Output a real number — the value of .
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
4 3 10 20 30 40 1 3 2 3 4 3
16.666667
3 3 10 20 30 1 2 2 3 3 1
13.333333
7 8 40 20 10 30 20 50 40 1 2 2 3 3 4 4 5 5 6 6 7 1 4 5 7
18.571429
Consider the first sample. There are 12 possible situations:
- p = 1, q = 3, f(p, q) = 10.
- p = 2, q = 3, f(p, q) = 20.
- p = 4, q = 3, f(p, q) = 30.
- p = 1, q = 2, f(p, q) = 10.
- p = 2, q = 4, f(p, q) = 20.
- p = 4, q = 1, f(p, q) = 10.
Another 6 cases are symmetrical to the above. The average is .
Consider the second sample. There are 6 possible situations:
- p = 1, q = 2, f(p, q) = 10.
- p = 2, q = 3, f(p, q) = 20.
- p = 1, q = 3, f(p, q) = 10.
Another 3 cases are symmetrical to the above. The average is .
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x7f3f3ff
#define PI 3.1415926535898
const int maxn = 1000010;
using namespace std;
LL fa[maxn];
LL num[maxn];
LL sum[maxn];
struct node
{
int u, v, w;
} f[maxn];
int Find(int x)
{
if(x != fa[x])
fa[x] = Find(fa[x]);
return fa[x];
}
bool cmp(node a, node b)
{
return a.w > b.w;
}
int main()
{
LL n, m;
while(cin >>n>>m)
{
for(int i = 1; i <= n; i++)
cin >>num[i];
for(int i = 1; i <= n; i++)
{
fa[i] = i;
sum[i] = 1;
}
for(int i = 0; i < m; i++)
{
int x, y;
cin >>x>>y;
f[i].u = x;
f[i].v = y;
f[i].w = min(num[x], num[y]);
}
sort(f, f+m, cmp);
double cnt = 0.0;
for(int i = 0; i < m; i++)
{
int x = Find(f[i].u);
int y = Find(f[i].v);
if(x == y)
continue;
fa[x] = y;
cnt += (f[i].w)*sum[x]*sum[y];
sum[y] += sum[x];
}
cnt *= 2.0;
printf("%.6lf\n",cnt/(n*(n-1)));
}
return 0;
}
相關文章
- Codeforces Round #256 (Div. 2)A-D
- Codeforces Round #263 (Div. 2) A-D
- Codeforces Round 720 (Div. 2) A-D
- Codeforces Round #673 (Div. 2)(A-D)題解
- Educational Codeforces Round 170 (Rated for Div. 2) A-D
- Educational Codeforces Round 142 (Rated for Div. 2) A-D
- Codeforces Round #250 (Div. 2) C、The Child and Toy
- Codeforces Round #250 (Div. 2) A. The Child and Homework
- Codeforces Round #844 (Div. 1 + Div. 2, based on VK Cup 2022 - Elimination Round) A-D
- Codeforces Round #639 (Div. 2)
- Codeforces Round #541 (Div. 2)
- Codeforces Round #682 (Div. 2)
- Codeforces Round #678 (Div. 2)
- Codeforces Round #747 (Div. 2)
- Codeforces Round #673 (Div. 2)
- Codeforces Round #672 (Div. 2)
- Codeforces Round #448 (Div. 2) A
- Codeforces Round #217 (Div. 2)
- Codeforces Round #256 (Div. 2)
- Codeforces Round #259 (Div. 2)
- Codeforces Round #257 (Div. 2)
- Codeforces Round #258 (Div. 2)
- Codeforces Round #171 (Div. 2)
- Codeforces Round #173 (Div. 2)
- Codeforces Round 951 (Div. 2)
- Codeforces Round 955 (Div. 2)
- Codeforces Round 953 (Div. 2)
- Codeforces Round 975 (Div. 2)
- Codeforces Round 976 (Div. 2)
- Codeforces Round 972 (Div. 2)
- Codeforces Round 979 (Div. 2)
- Codeforces Round 982 (Div. 2)
- Codeforces Round 932 (Div. 2)
- Codeforces Round 934 (Div. 2)
- Codeforces Round 940 (Div. 2)
- Codeforces Round 973 (Div. 2)
- Codeforces Round 960 (Div. 2)
- Codeforces Round 958 (Div. 2)