Codeforces Round #516 Div. 2 (部分题解)

A. Make a triangle!

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Masha has three sticks of length aa, bb and cc centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks.

What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices.

Input

The only line contains tree integers aa, bb and cc (1≤a,b,c≤1001≤a,b,c≤100) — the lengths of sticks Masha possesses.

Output

Print a single integer — the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks.

Examples

input

Copy

3 4 5

output

Copy

0

input

Copy

2 5 3

output

Copy

1

input

Copy

100 10 10

output

Copy

81

Note

In the first example, Masha can make a triangle from the sticks without increasing the length of any of them.

In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length 22 centimeter stick by one and after that form a triangle with sides 33, 33 and 55 centimeters.

In the third example, Masha can take 3333 minutes to increase one of the 1010 centimeters sticks by 3333 centimeters, and after that take 4848minutes to increase another 1010 centimeters stick by 4848 centimeters. This way she can form a triangle with lengths 4343, 5858 and 100100centimeters in 8181 minutes. One can show that it is impossible to get a valid triangle faster.

思路:直接暴力找就行

#include<bits/stdc++.h>
#define maxn 50005

using namespace std;

typedef long long ll;

typedef long double ld;

priority_queue<ll,vector<ll>,greater<ll> > q;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    ll a,b,c;
    cin >> a >> b >> c;
    q.push(a);
    q.push(b);
    q.push(c);

    ll i = 0;
    while(1)
    {
        a = q.top();
        q.pop();
        b = q.top();
        q.pop();
        c = q.top();
        q.pop();
        if(a + b > c)
        {
            cout << i << endl;
            break;
        }
        else
        {
            i++;
            q.push(a+1);
            q.push(b);
            q.push(c);
        }
    }
    return 0;
}

B. Equations of Mathematical Magic

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Colossal! — exclaimed Hawk-nose. — A programmer! That's exactly what we are looking for.

Arkadi and Boris Strugatsky. Monday starts on Saturday

Reading the book "Equations of Mathematical Magic" Roman Oira-Oira and Cristobal Junta found an interesting equation: a−(a⊕x)−x=0a−(a⊕x)−x=0 for some given aa, where ⊕⊕ stands for a bitwise exclusive or (XOR) of two integers (this operation is denoted as ^ or xor in many modern programming languages). Oira-Oira quickly found some xx, which is the solution of the equation, but Cristobal Junta decided that Oira-Oira's result is not interesting enough, so he asked his colleague how many non-negative solutions of this equation exist. This task turned out to be too difficult for Oira-Oira, so he asks you to help.

Input

Each test contains several possible values of aa and your task is to find the number of equation's solution for each of them. The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of these values.

The following tt lines contain the values of parameter aa, each value is an integer from 00 to 230−1230−1 inclusive.

Output

For each value of aa print exactly one integer — the number of non-negative solutions of the equation for the given value of the parameter. Print answers in the same order as values of aa appear in the input.

One can show that the number of solutions is always finite.

Example

input

Copy

3
0
2
1073741823

output

Copy

1
2
1073741824

Note

Let's define the bitwise exclusive OR (XOR) operation. Given two integers xx and yy, consider their binary representations (possibly with leading zeroes): xk…x2x1x0xk…x2x1x0 and yk…y2y1y0yk…y2y1y0. Here, xixi is the ii-th bit of the number xx and yiyi is the ii-th bit of the number yy. Let r=x⊕yr=x⊕y be the result of the XOR operation of xx and yy. Then rr is defined as rk…r2r1r0rk…r2r1r0 where:

ri={1, if xi≠yi0, if xi=yiri={1, if xi≠yi0, if xi=yi

For the first value of the parameter, only x=0x=0 is a solution of the equation.

For the second value of the parameter, solutions are x=0x=0 and x=2x=2.

思路:找出来a中有多少个1直接输出2的几次方就可

#include<bits/stdc++.h>
#define maxn 2005

using namespace std;

typedef long long ll;

ll b[maxn];
int main()
{
    for(int i = 0; i <= 35; i ++)
    {
        b[i] = pow(2,i);
    }

    ll t;
    cin >> t;
    while(t--)
    {
        ll a;
        cin >> a;
        ll i = 0;
        while(a)
        {
            if(a & 1)i++;
            a /= 2;
        }
        cout << b[i] << endl;
    }
    return 0;
}

C. Oh Those Palindromes

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A non-empty string is called palindrome, if it reads the same from the left to the right and from the right to the left. For example, "abcba", "a", and "abba" are palindromes, while "abab" and "xy" are not.

A string is called a substring of another string, if it can be obtained from that string by dropping some (possibly zero) number of characters from the beginning and from the end of it. For example, "abc", "ab", and "c" are substrings of the string "abc", while "ac" and "d" are not.

Let's define a palindromic count of the string as the number of its substrings that are palindromes. For example, the palindromic count of the string "aaa" is 66 because all its substrings are palindromes, and the palindromic count of the string "abc" is 33 because only its substrings of length 11 are palindromes.

You are given a string ss. You can arbitrarily rearrange its characters. You goal is to obtain a string with the maximum possible value of palindromic count.

Input

The first line contains an integer nn (1≤n≤1000001≤n≤100000) — the length of string ss.

The second line contains string ss that consists of exactly nn lowercase characters of Latin alphabet.

Output

Print string tt, which consists of the same set of characters (and each characters appears exactly the same number of times) as string ss. Moreover, tt should have the maximum possible value of palindromic count among all such strings strings.

If there are multiple such strings, print any of them.

Examples

input

Copy

5
oolol

output

Copy

ololo

input

Copy

16
gagadbcgghhchbdf

output

Copy

abccbaghghghgdfd

Note

In the first example, string "ololo" has 99 palindromic substrings: "o", "l", "o", "l", "o", "olo", "lol", "olo", "ololo". Note, that even though some substrings coincide, they are counted as many times as they appear in the resulting string.

In the second example, the palindromic count of string "abccbaghghghgdfd" is 2929.

思路:排序输出他的回文子串是最多的

#include<bits/stdc++.h>
#define maxn 200005

using namespace std;

typedef long long ll;

char a[maxn];
int main()
{
    ll n;
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
    }
    sort(a+1,a+n+1);
    for(int i = 1; i <= n; i ++)
    {
        cout << a[i];
    }
    cout << endl;
}

D. Labyrinth

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row r and column c. In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle. You can't move beyond the boundaries of the labyrinth.

Unfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition.

Now you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property?

Input

The first line contains two integers nm (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.

The second line contains two integers rc (1 ≤ r ≤ n, 1 ≤ c ≤ m) — index of the row and index of the column that define the starting cell.

The third line contains two integers xy (0 ≤ x, y ≤ 109) — the maximum allowed number of movements to the left and to the right respectively.

The next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'. The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.

It is guaranteed, that the starting cell contains no obstacles.

Output

Print exactly one integer — the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.

Examples

input

Copy

4 5
3 2
1 2
.....
.***.
...**
*....

output

Copy

10

input

Copy

4 4
2 2
0 1
....
..*.
....
....

output

Copy

7

Note

Cells, reachable in the corresponding example, are marked with '+'.

First example:

+++..
+***.
+++**
*+++.

Second example:

.++.
.+*.
.++.
.++.

思路:标准bfs只不过搜索的时候要优先搜上下在搜左右(顺序很重要)

#include<bits/stdc++.h>
#define maxn 2005

using namespace std;

typedef long long ll;

ll a[maxn][maxn];
ll n,m,x,y,ans,r,c;
struct node
{
    ll x,y,stex,stey;
    node (ll xx,ll yy,ll st,ll ste)
    {
        x = xx;
        y = yy;
        stex = st;
        stey = ste;
    }
};
void bfs(ll xx,ll yy,ll stexx,ll steyy)
{
    deque<node>q;
    q.push_front(node(xx,yy,stexx,steyy));
    while(!q.empty())
    {
        node k = q.front();
        q.pop_front();
        if(a[k.x - 1][k.y] == 0 && k.x - 1 > 0 && k.x - 1 <= n && k.y > 0 && k.y <= m)
        {
            a[k.x - 1][k.y] = 1;
            q.push_front(node(k.x-1,k.y,k.stex,k.stey));
            ans++;
        }
        if(a[k.x + 1][k.y] == 0 && k.x + 1 > 0 && k.x + 1 <= n && k.y > 0 && k.y <= m)
        {
            a[k.x + 1][k.y] = 1;
            q.push_front(node(k.x+1,k.y,k.stex,k.stey));
            ans++;
        }
        if(k.stex > 0 && a[k.x][k.y - 1] == 0 && k.x > 0 && k.x <= n && k.y  - 1 > 0 && k.y - 1 <= m)
        {
            a[k.x][k.y - 1] = 1;
            q.push_back(node(k.x,k.y-1,k.stex - 1,k.stey));
            ans++;
        }
        if(k.stey > 0 && a[k.x][k.y + 1] == 0 && k.x > 0 && k.x <= n && k.y  + 1 > 0 && k.y + 1 <= m)
        {
            a[k.x][k.y + 1] = 1;
            q.push_back(node(k.x,k.y+1,k.stex,k.stey - 1));
            ans++;
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);

    cin >> n >> m;
    cin >> r >> c;
    cin >> x >> y;
    for(int i = 1; i <= n; i ++)
    {
        for(int j = 1; j <= m; j ++)
        {
            char k;
            cin >> k;
            if(k == '*')
                a[i][j] = 1;
            else
                a[i][j] = 0;
        }
    }
    a[r][c] = 1;
    ans ++;
    bfs(r,c,x,y);

    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zzzanj/article/details/83054524