Codeforces Round #250 (Div. 2) A-D

畫船聽雨發表於2014-06-03

A:

注意要是有多選的情況輸出C。

A. The Child and Homework
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Once upon a time a child got a test consisting of multiple-choice questions as homework. A multiple-choice question consists of four choices: ABC 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?

Input

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: BCDPlease 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 "_".

Output

Print a single line with the child's choice: "A", "B", "C" or "D" (without quotes).

Sample test(s)
input
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
output
D
input
A.ab
B.abcde
C.ab
D.abc
output
C
input
A.c
B.cc
C.c
D.c
output
B
Note

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:可以從大到小列舉。

B. The Child and Set
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

The first line contains two integers: sum, limit (1 ≤ sum, limit ≤ 105).

Output

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.

Sample test(s)
input
5 5
output
2
4 5
input
4 3
output
3
2 3 1
input
5 1
output
-1
Note

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:想明白了就很水了啊。

C. The Child and Toy
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line contains two integers n and m (1 ≤ n ≤ 10000 ≤ 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 ≤ nxi ≠ yi).

Consider all the parts are numbered from 1 to n.

Output

Output the minimum total energy the child should spend to remove all n parts of the toy.

Sample test(s)
input
4 3
10 20 30 40
1 4
1 2
2 3
output
40
input
4 4
100 100 100 100
1 2
2 3
2 4
3 4
output
400
input
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
output
160
Note

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啊、、

D. The Child and Zoo
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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?

Input

The first line contains two integers n and m (2 ≤ n ≤ 1050 ≤ 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 ≤ nxi ≠ 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

Output a real number — the value of .

The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.

Sample test(s)
input
4 3
10 20 30 40
1 3
2 3
4 3
output
16.666667
input
3 3
10 20 30
1 2
2 3
3 1
output
13.333333
input
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
output
18.571429
Note

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;
}


相關文章