Codeforces Round #516 (Div. 2)

19 篇文章 0 订阅
11 篇文章 0 订阅

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>
using namespace std;
#define debug puts("-----")
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf (1<<30)
typedef long long LL;
int main()
{
	int a[3];
	for(int i = 0; i < 3; i++)
		scanf("%d", &a[i]);
	sort(a, a + 3);
	int ans = a[2] - a[0] - a[1] + 1;
	if(ans < 0)
		puts("0");
	else
		printf("%d\n", ans);	
	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.

题意:给你一个数N,求[0, N]中有几个数满足等式:a - x = a ⊕ x;

思路:个数 为 2 ^ (N中二进制1的个数);

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll func(ll x)
{
	int countx = 0;
	while(x)
	{
		countx ++;
		x = x&(x-1);
	}
	return countx;
}
int main()
{
	int t;
	ll b;
	cin >> t;
	while(t--)
	{
		scanf("%lld", &b);
		ll m = func(b);
		printf("%.0lf\n", pow(2, m)); 
	}
	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.

题意:给你一个字符串,问怎样调整字符可以使字符串权值最大,权值为字符串中回文串的个数;

思路: 假设 S为一个回文串,那么aSa对权值的贡献和aaS对权值的贡献是一样的,所以只要排序一下即可;

代码:

#include<bits/stdc++.h>
using namespace std;
char s[100005];
int main()
{
	int l;
	cin >> l >> s;
	sort(s, s + l);
	cout << s << endl;
	return 0;
 } 

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:

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

题意:有一个N*M的迷宫, 一个人在(r,c)这个点上,只要路是通的,他可以上下无限制移动,但是左右移动次数受限,问这个人最多可以到达几个点;

思路:题目有坑。。。。。不是简单地BFS,因为左右移动次数受限,所以我们只有先将上下移动可以走到的点走完,再去找左右可以移动到的点才能保证得到的结果是最大的,所以就存在了一个优先级的问题,所以这个题可以用优先队列或者双端队列来搜索;

代码:

/*
* @Author: GTUIF
* @Date:   2018-10-16 19:18:24
* @Last Modified by:   GTUIF
* @Last Modified time: 2018-10-16 19:47:28
*/
#include<bits/stdc++.h>
using namespace std;
#define debug puts("-----")
#define pi (acos(-1.0))
#define eps (1e-8)
#define inf (1<<30)
typedef long long LL;
struct node{
	int x, y;
	int l, r;
};
int vis[2005][2005];
char mapp[2005][2005];
int n, m, stx, sty, l, r;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
bool check(int x, int y)
{
	if(x < 0 || x >= n || y < 0 || y >= m || mapp[x][y] == '*' || vis[x][y])
		return false;
	return true;
}
void bfs(int stx, int sty, int l, int r)
{
	memset(vis, 0, sizeof(vis));
	int ans = 0;
	deque<node> q;
	q.push_front((node){stx, sty, l, r});
	while(!q.empty())
	{
		node k = q.front();
		q.pop_front(); 
		if(!vis[k.x][k.y])
		{
		 	ans++;
			vis[k.x][k.y] = 1;
		}
		else continue; 
		for(int i = 0; i < 4; i++)
		{
			int fx = k.x + dx[i];
			int fy = k.y + dy[i];
			if(!check(fx, fy) || (i == 1 && !k.l ) || (i == 3 && !k.r))
				continue;
			if(i == 0 || i == 2)
				q.push_front((node){fx, fy, k.l, k.r});
			else if(i == 1)
				q.push_back((node){fx, fy, k.l - 1, k.r});
			else 
				q.push_back((node){fx, fy, k.l, k.r - 1});   
		}
	}
	cout << ans << endl;
}
int main()
{
	cin >> n >> m;
	cin >> stx >> sty;
	cin >> l >> r;
	for(int i = 0; i < n; i++)
		scanf("%s", mapp[i]);
	stx--, sty--;
	bfs(stx, sty, l, r);
// 	for(int i = 0; i < n; i++)
// 	{
// 		for(int j = 0; j < m; j++)
// 		{
// 			if(vis[i][j])
// 				printf("+");
// 			else
// 				printf("%c", mapp[i][j]);
// 		}
// 		puts("");
// 	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值