Codeforces Round #516 Div. 2 A. B. Equations of Mathematical Magic C. Oh Those Pali D. Labyrinth
A. Make a triangle!
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
3 4 5
output
0
input
2 5 3
output
1
input
100 10 10
output
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, 33and 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 4848 minutes to increase another 1010 centimeters stick by 4848 centimeters. This way she can form a triangle with lengths 4343, 5858 and 100100 centimeters in 8181 minutes. One can show that it is impossible to get a valid triangle faster.
三个数
能否组成三角形
如果不能 输出最少增加多少能组成
两边之和大于第三边
//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
#define int long long
const int MAXN = 1e6 + 10;
int a[MAXN] = {0};
signed main()
{
//ios::sync_with_stdio(false);
//cin.tie(0); cout.tie(0);
//freopen("D://test.in", "r", stdin);
//freopen("D://test.out", "w", stdout);
cin>>a[0]>>a[1]>>a[2];
sort(a, a + 3);
if(a[0] + a[1] > a[2] && a[0] + a[2] > a[1] && a[1] + a[2] >a[0])
{
printf("0\n");
}
else
{
cout<<a[2] - (a[0] + a[1]) + 1<<endl;
}
return 0;
}
B. Equations of Mathematical Magic
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
3
0
2
1073741823
output
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 rris 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^x = a - x
打表找规律
转二进制有多少一输出二的多少次方
//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 10;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
while(n--)
{
long long t;
cin>>t;
int ans = 0;
while(t)
{
ans += t % 2;
t /= 2;
}
long long rs = 1;
while(ans--)
{
rs *= 2;
}
cout<<rs<<endl;
}
return 0;
}
C. Oh Those Palindromes
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
5
oolol
output
ololo
input
16
gagadbcgghhchbdf
output
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.
sort
//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
#define int long long
const int MAXN = 1e6 + 10;
signed main()
{
//ios::sync_with_stdio(false);
//cin.tie(0); cout.tie(0);
//freopen("D://test.in", "r", stdin);
//freopen("D://test.out", "w", stdout);
int n;
cin>>n;
string s;
cin>>s;
sort(s.begin(), s.end());
cout<<s<<endl;
return 0;
}
D. Labyrinth
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 n, m (1 ≤ n, m ≤ 2000) — the number of rows and the number columns in the labyrinth respectively.
The second line contains two integers r, c (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 x, y (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
4 5
3 2
1 2
.....
.***.
...**
*....
output
10
input
4 4
2 2
0 1
....
..*.
....
....
output
7
Note
Cells, reachable in the corresponding example, are marked with '+'.
First example:
+++..
+***.
+++**
*+++.
Second example:
.++.
.+*.
.++.
.++.
是BFS 但是左右移动权值不同
直接BFS 由于BFS最短路先占的特性会导致最优解被卡 无法下搜
所以应该重载优先队列 让最优解位于队头
易证 当 左右步数之和最大时解最优
另外也可使用双端队列更改搜索顺序达到最优解先搜
//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
const int MAXN = 2e3 + 5;
char arr[MAXN][MAXN];
char brr[MAXN][MAXN];
int n, m;
int r, c;
int x, y, ans = 1, ans1 = 1;
struct node
{
short nx, ny;
int rx, ry;
node(short a, short b, int c, int d)
{
nx=a,ny=b,rx=c,ry=d;
}
bool operator < (const node b) const
{
return rx + ry < b.rx + b.ry;
}
};
priority_queue <node> Q;
bool cango(short x, short y)
{
if(x >= 1 && x <= n && y >= 1 && y <= m && arr[x][y] == '.')
return true;
else
return false;
}
void bfs()
{
Q.push(node(r,c,x,y) );
arr[r][c] = '+';
while(!Q.empty())
{
node a = Q.top();
Q.pop();
if(cango(a.nx + 1, a.ny))
{
arr[a.nx + 1][a.ny] = '+';
ans++;
Q.push(node(a.nx + 1, a.ny, a.rx, a.ry) );
}
if(cango(a.nx - 1, a.ny))
{
arr[a.nx - 1][a.ny] = '+';
ans++;
Q.push(node(a.nx - 1, a.ny, a.rx, a.ry) );
}
if(a.rx && cango(a.nx, a.ny - 1))
{
arr[a.nx][a.ny - 1] = '+';
ans++;
Q.push(node(a.nx, a.ny - 1, a.rx - 1, a.ry) );
}
if(a.ry && cango(a.nx, a.ny + 1))
{
arr[a.nx][a.ny + 1] = '+';
ans++;
Q.push(node(a.nx, a.ny + 1, a.rx, a.ry - 1) );
}
}
}
signed main()
{
//freopen("D://test.in", "r", stdin);
//freopen("D://test.out", "w", stdout);
cin>>n>>m>>r>>c>>x>>y;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
cin>>arr[i][j];
bfs();
cout<<ans<<endl;
return 0;
}