8VC Venture Cup 2016 - Elimination Round题解

来源:互联网 发布:scdma是什么网络类型 编辑:程序博客网 时间:2024/05/01 02:28

A. Robot Sequence

Calvin the robot lies in an infinite rectangular grid. Calvin’s source code contains a list of n commands, each either ‘U’, ‘R’, ‘D’, or ‘L’ — instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices.

Input

The first line of the input contains a single positive integer, n (1 ≤ n ≤ 200) — the number of commands.

The next line contains n characters, each either ‘U’, ‘R’, ‘D’, or ‘L’ — Calvin’s source code.

Output

Print a single integer — the number of contiguous substrings that Calvin can execute and return to his starting square.

Examples

Input
6
URLLDR
Output
2
Input
4
DLUU
Output
0
Input
7
RLRLRLR
Output
12
Note
In the first case, the entire source code works, as well as the “RL” substring in the second and third characters.
Note that, in the third case, the substring “LR” appears three times, and is therefore counted three times to the total result.


题意
给一个机器人走的序列,有多少子序列能使机器人走到开始的位置
思路
由于n<=200,所以我们只要暴力搜索就行了

#include <bits/stdc++.h>#define N 1000005#define ll long longusing namespace std;int main(){#ifndef ONLINE_JUDGE    freopen("1.txt", "r", stdin);#endif    int i, j, n, ans = 0, t1, t2;    string s;    cin >> n;    cin >> s;    for (i = 0; i < n; i++)    {        t1 = t2 = 0;        for (j = i; j < n; j++)        {            switch(s[j])            {                case 'U': t1 += 1; break;                case 'D': t1 += -1; break;                case 'L': t2 += -1; break;                case 'R': t2 += 1; break;            }            if (!t1 && !t2) ans++;        }    }    cout << ans;    return 0;}

B. Cards

Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions:

take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color;take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color. 

She repeats this process until there is only one card left. What are the possible colors for the final card?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards.

The next line contains a string s of length n — the colors of the cards. s contains only the characters ‘B’, ‘G’, and ‘R’, representing blue, green, and red, respectively.

Output

Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order.

Examples

Input
2
RB
Output
G
Input
3
GRG
Output
BR
Input
5
BBBBB
Output
B
Note
In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card.

In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and a red card for a blue card, then exchange the blue card and remaining green card for a red card.

In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards.
题意
有n张颜色为红、黄、蓝的卡片,进行以下两种操作
1. 任取两种颜色不同的卡片,换成另外的颜色的卡片
2. 任取两张颜色相同的卡片,换成一张这种颜色的卡片
问,最后可能剩下哪些卡片?
思路
感觉我写的好麻烦啊
具体方法就是分类讨论
如果三种卡片都有,则最后每一种卡片都可能剩下, 如:
RGB -> BB -> B
RGB -> RR -> R
RGB -> GG -> G
如果只有一种卡片,则只会剩下这一种卡片
如果有两种卡片
如:有Red卡片r张,Green卡片g张,其中r和g都大于0
分类讨论:
如果r == g == 1,则只会剩下Blue卡片
如果r >=2 && g >= 2,分别取一张Red和Green卡片,换成一种Blue卡片,这样就又变成上面说的那种情况了
如果r==1 && g > 1,我们可以执行第二个操作,直到g = 2,即RGG
RGG -> BG -> R;
RGG -> RG -> B;
这样结果就是BR;
同理,如果g==1 && r > 1,结果是BG

把以上所有情况都写一遍就行了

#include <bits/stdc++.h>#define N 1000005#define ll long longusing namespace std;int main(){#ifndef ONLINE_JUDGE//  freopen("1.txt", "r", stdin);#endif    int i, j, n, ans = 0, t1, t2, color[3], r, g, b;    bool aa, bb, cc;    map<char, int> mm;    mm.insert(pair<char, int> ('R', 0));    mm.insert(pair<char, int> ('G', 1));    mm.insert(pair<char, int> ('B', 2));    aa = bb = cc =false;    string s;    cin >> n;    cin >> s;    color[0] = color[1] = color[2] = 0;    for (i = 0; i < n; i++)        color[mm[s[i]]]++;    r = g = b = false;    if (color[0] && color[1] && color[2])        r = g = b = true;    else if (color[0])    {        if (color[1])        {            b = true;            if (color[0] > 1 && color[1] > 1)   r = g = true;            else            {                if (color[0] > 1 || color[1] > 1)   r = g = true;                if (color[0] > 1)   r = false;                if (color[1] > 1)   g = false;            }        }        else if (color[2])        {            g = true;            if (color[0] > 1 && color[2] > 1)   r = b = true;            else            {                if (color[0] >1 || color[2] > 1)    r = b = true;                if (color[0] > 1)   r = false;                if (color[2] > 1)   b = false;            }        }        else            r = true;    }    else if (color[1])    {        if (color[0])        {            b = true;            if (color[0] > 1 && color[1] > 1)   r = g = true;            else            {                if (color[0] > 1 || color[1] > 1)   r = g = true;                if (color[0] > 1)   r = false;                if (color[1] > 1)   g = false;            }        }        else if (color[2])        {            r = true;            if (color[1] > 1 && color[2] > 1)   g = b = true;            else            {                if (color[1] > 1 || color[2] > 1)   b = g = true;                if (color[1] > 1)   g = false;                if (color[2] > 1)   b = false;            }        }        else            g = true;    }    else if (color[2])    {        if (color[0])        {            g = true;            if (color[0] > 1 && color[2] > 1)   r = b = true;            else            {                if (color[0] > 1 || color[1] > 1)   r = b = true;                if (color[0] > 1)   r = false;                if (color[2] > 1)   b = false;            }        }        else if (color[1])        {            r = true;            if (color[1] > 1 && color[2] > 1)   g = b = true;            else            {                if (color[1] > 1 || color[2] > 1)   b = g = true;                if (color[1] > 1)   g = false;                if (color[2] > 1)   b = false;            }        }        else            b = true;    }    if (b)  putchar('B');    if (g)  putchar('G');    if (r)  putchar('R');    return 0;}

C. Block Towers

Students in a class are making towers of blocks. Each student makes a (non-zero) tower by stacking pieces lengthwise on top of each other. n of the students use pieces made of two blocks and m of the students use pieces made of three blocks.

The students don’t want to use too many blocks, but they also want to be unique, so no two students’ towers may contain the same number of blocks. Find the minimum height necessary for the tallest of the students’ towers.

Input

The first line of the input contains two space-separated integers n and m (0 ≤ n, m ≤ 1 000 000, n + m > 0) — the number of students using two-block pieces and the number of students using three-block pieces, respectively.

Output

Print a single integer, denoting the minimum possible height of the tallest tower.

Examples

input
1 3
output
9
input
3 2
output
8
input
5 0
output
10
Note
In the first case, the student using two-block pieces can make a tower of height 4, and the students using three-block pieces can make towers of height 3, 6, and 9 blocks. The tallest tower has a height of 9 blocks.
In the second case, the students can make towers of heights 2, 4, and 8 with two-block pieces and towers of heights 3 and 6 with three-block pieces, for a maximum height of 8 blocks.


题意
一些人在用方块拼塔,有n个人用的是两块一片的,m个人用的是三块一片的。每个人使用的块数不一样,问满足要求的最大高度的最小值是多少?(感觉翻译的好别扭啊/(ㄒoㄒ)/~~ 还是看英文吧,翻译仅供参考)
思路
结果不会很大,所以直接暴力搜索就行了
a1是小于等于i中是2的倍数但不是6的倍数的数的个数
a2是小于等于i中是3的倍数但不是6的倍数的数的个数
a3是小于等于i中是6的倍数的个数
a1+a2+a3>=n+m就是符合条件的解

#include <bits/stdc++.h>#define N 1000005#define ll long longusing namespace std;int main(){#ifndef ONLINE_JUDGE//  freopen("1.txt", "r", stdin);#endif    int n, m, i, j, ans = 0, a1, a2, a3;    cin >> n >> m;    for (i = 1; ; i++)    {        a1 = min(i/2-i/6, n);        a2 = min(i/3-i/6, m);        a3 = i/6;        if (a1+a2+a3 >= n+m)        {            cout << i;            return 0;        }    }    return 0;}

D. Jerry’s Protest

Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing n balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds.

Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew?

Input

The first line of input contains a single integer n (2 ≤ n ≤ 2000) — the number of balls in the jar.

The second line contains n integers ai (1 ≤ ai ≤ 5000) — the number written on the ith ball. It is guaranteed that no two balls have the same number.

Output

Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let’s assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if 这里写图片描述.

Examples

input
2
1 2
output
0.0000000000
input
3
1 2 10
output
0.0740740741
Note
In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew’s sum is 5 and Jerry’s sum is 4, so Jerry never has a higher total.

In the second case, each game could’ve had three outcomes — 10 - 2, 10 - 1, or 2 - 1. Jerry has a higher total if and only if Andrew won 2 - 1 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability 这里写图片描述.


题意:Andrew和Jerry 正在玩一种游戏。他们俩从一个坛子里取一个球,谁的球上的编号大谁获胜。Andrew 赢了第一局和第二局,Jerry 赢了第三局。问Jerry 取的三个球数字总和大于Andrew 的概率是多少
思路:由于结果是固定的,所以我们只需枚举出所有的胜利方式,
用a[i]记录胜利方比失败方都i分的概率,sum[i]表示胜利方比失败方多的分数大于等于i的概率。因为i<=5000,所以我们直接暴力枚举就行了

#include <bits/stdc++.h>#define N 2005#define ll long longusing namespace std;int num[N], n;double  ans = 0, sum[5005], a[5005];int main(){#ifndef ONLINE_JUDGE    freopen("1.txt", "r", stdin);#endif    int i, j, cur; //cur为总共的方案数    scanf("%d", &n);    for (i = 0; i < n; i++)        scanf("%d", &num[i]);    sort(num, num+n);    cur = 0;    for (i = n-1; i >= 0; i--)    {        for (j = i-1; j >= 0; j--)        {            cur++;            a[num[i]-num[j]]++;        }    }    for (i = 5000; i > 0; i--)        sum[i] = a[i] + sum[i+1];    for (i = 1; i <= 5000; i++)    {        a[i] /= cur;        sum[i] /=cur;    }    for (i = 1; i <= 5000; i++)    {        for (j = 1; j <= 5000; j++)        {            if (i+j < 5000)                ans += sum[i+j+1]*a[i]*a[j];        }    }    printf("%.15lf", ans);    return 0;}

E. Simple Skewness

Define the simple skewness of a collection of numbers to be the collection’s mean minus its median. You are given a list of n (not necessarily distinct) integers. Find the non-empty subset (with repetition) with the maximum simple skewness.

The mean of a collection is the average of its elements. The median of a collection is its middle element when all of its elements are sorted, or the average of its two middle elements if it has even size.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200 000) — the number of elements in the list.

The second line contains n integers xi (0 ≤ xi ≤ 1 000 000) — the ith element of the list.

Output

In the first line, print a single integer k — the size of the subset.

In the second line, print k integers — the elements of the subset in any order.

If there are multiple optimal subsets, print any.

Examples

Input
4
1 2 3 12
Output
3
1 2 12
Input
4
1 1 2 2
Output
3
1 1 2
Input
2
1 2
Output
2
1 2
Note
In the first case, the optimal subset is{1, 2, 12} , which has mean 5, median 2, and simple skewness of 5 - 2 = 3.
In the second case, the optimal subset is {1, 1, 2}. Note that repetition is allowed.
In the last case, any subset has the same median and mean, so all have simple skewness of 0.


题意
给一个集合,找到一个子集,使这个子集排序后的平均数减去中位数最大
思路

继续写~~~~

0 0
原创粉丝点击