A. The New Year: Meeting Friends
|time limit per test1 second|memory limit per test256 megabytes|
|---
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?
It's guaranteed that the optimal answer is always integer.
Input
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.
Output
Print one integer — the minimum total distance the friends need to travel in order to meet together.
Examples
input
7 1 4
output
6
input
30 20 10
output
20
Note
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.
最大值与最小值的差就是答案。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
int main() {
int n,da;
int Min = 1100,Max = 0;
for(int i = 0;i<3;i++)
{
scanf("%d",&da);
Min = min(Min,da);
Max = max(da,Max);
}
printf("%d
",Max-Min);
return 0;
}
B. Text Document Analysis
|time limit per test1 second|memory limit per test256 megabytes|
|----
Modern text editors usually show some information regarding the document being edited. For example, the number of words, the number of pages, or the number of characters.
In this problem you should implement the similar functionality.
You are given a string which only consists of:
uppercase and lowercase English letters,
underscore symbols (they are used as separators),
parentheses (both opening and closing).
It is guaranteed that each opening parenthesis has a succeeding closing parenthesis. Similarly, each closing parentheses has a preceding opening parentheses matching it. For each pair of matching parentheses there are no other parenthesis between them. In other words, each parenthesis in the string belongs to a matching "opening-closing" pair, and such pairs can't be nested.
For example, the following string is valid: _Hello_Vasya(and_Petya)__bye_(and_OK)
.
Word is a maximal sequence of consecutive letters, i.e. such sequence that the first character to the left and the first character to the right of it is an underscore, a parenthesis, or it just does not exist. For example, the string above consists of seven words: "Hello", "Vasya", "and", "Petya", "bye", "and" and "OK". Write a program that finds:
the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses),
the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 255) — the length of the given string. The second line contains the string consisting of only lowercase and uppercase English letters, parentheses and underscore symbols.
Output
Print two space-separated integers:
the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses),
the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
Examples
input
37
_Hello_Vasya(and_Petya)__bye_(and_OK)
output
5 4
input
37
_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__
output
2 6
input
27
(LoooonG)__shOrt__(LoooonG)
output
5 2
input
5
(___)
output
0 0
Note
In the first sample, the words "Hello", "Vasya" and "bye" are outside any of the parentheses, and the words "and", "Petya", "and" and "OK" are inside. Note, that the word "and" is given twice and you should count it twice in the answer.
题意:计算不在括号之内的字符串的最长的长度和在括号中单词的个数。模拟
#include <bits/stdc++.h>
using namespace std;
char str[1100];
int main() {
int n;
scanf("%d",&n);
scanf("%s",str);
bool op = false;
int num = 0;
int ans1 = 0,ans2 = 0;
for(int i = 0; i<n; i++) {
if(str[i] == '(') {
if(num) {
if(op) {
ans2++;
} else ans1 = max(ans1,num);
}
op = true;
num = 0;
continue;
}
if(str[i] == ')') {
if(num) {
if(op) {
ans2++;
} else ans1 = max(ans1,num);
}
op = false;
num = 0;
continue;
}
if(str[i] == '_') {
if(num) {
if(op) {
ans2++;
} else ans1 = max(ans1,num);
}
num = 0;
} else num++;
}
if(num) {
if(op) {
ans2++;
} else ans1 = max(ans1,num);
}
num = 0;
printf("%d %d
",ans1,ans2);
return 0;
}
C. Polycarp at the Radio
|time limit per test2 seconds|memory limit per test256 megabytes|
|--
Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.
We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.
Find this maximum possible value of the minimum among the bj (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.
Input
The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000).
The second line contains n integers (a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9)), where ai is the performer of the i-th song.
Output
In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.
In the second line print the changed playlist.
If there are multiple answers, print any of them.
Examples
input
4 2
1 2 3 2
output
2 1
1 2 1 2
input
7 3
1 3 2 2 2 2 1
output
2 1
1 3 3 2 2 2 1
input
4 4
1000000000 100 7 1000000000
output
1 4
1 2 3 4
Note
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.
In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.
#include <bits/stdc++.h>
using namespace std;
int a[2200];
int has[2200];
int main() {
int n,m;
scanf("%d %d",&n,&m);
int st = n/m;
queue<int>Q;
for(int i = 0; i<n; i++) {
scanf("%d",&a[i]);
if(a[i]<=m) {
if(has[a[i]]< st) has[a[i]] ++;
else Q.push(i);
} else Q.push(i);
}
int ans1 = 0;
int ans2 = 0;
int tt = 1;
while(!Q.empty()) {
while(has[tt] >=st && tt <=m) tt++;
if( tt >m) break;
int u =Q.front();
Q.pop();
a[u] = tt;
ans2++;
has[tt] ++;
}
ans1 = st;
printf("%d %d
",ans1,ans2);
for(int i = 0; i<n; i++) {
if(i) printf(" ");
printf("%d",a[i]);
}
printf("
");
return 0;
}
D. Lakes in Berland
|time limit per test2 seconds|memory limit per test256 megabytes|
|--
The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean.
Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.
You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.
Input
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map.
The next n lines contain m characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land).
It is guaranteed that the map contain at least k lakes.
Output
In the first line print the minimum number of cells which should be transformed from water to land.
In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them.
It is guaranteed that the answer exists on the given data.
Examples
input
5 4 1
****
*..*
****
**.*
..**
output
1
****
*..*
****
****
..**
input
3 3 0
***
*.*
***
output
1
***
***
***
Note
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.
找出所有的湖,然后从小到大消灭足够的湖就行了。
#include <bits/stdc++.h>
using namespace std;
char str[55][55];
int n,m,k;
struct node {
int x,y,k;
bool operator < (const node &b) const {
return k>b.k;
}
}a[110000];
int dir[][2] = {{0,1},{0,-1},{1,0},{-1,0}};
bool vis[55][55];
int num;
bool dfs(int x,int y) {
num++;
bool flag = true;
vis[x][y] = true;
for(int i = 0;i<4;i++) {
int Fx = x+dir[i][0];
int Fy = y+dir[i][1];
if(Fx < 0 || Fx >= n|| Fy<0 ||Fy >=m) {
flag = false;
continue;
}
if(str[Fx][Fy]!='*' && !vis[Fx][Fy])
if(!dfs(Fx,Fy)) flag = false;
}
return flag;
}
void dfs1(int x,int y) {
str[x][y] = '*';
vis[x][y] = true;
for(int i = 0;i<4;i++) {
int Fx = x+dir[i][0];
int Fy = y+dir[i][1];
if(Fx < 0 || Fx >= n|| Fy<0 ||Fy >=m) continue;
if(str[Fx][Fy]!='*' && !vis[Fx][Fy]) dfs1(Fx,Fy);
}
}
int main() {
scanf("%d %d %d",&n,&m,&k);
for(int i = 0 ;i<n;i++) scanf("%s",str[i]);
int top = 0;
memset(vis,false,sizeof(vis));
for(int i = 0;i<n;i++) {
for(int j = 0;j<m;j++) {
if(str[i][j] == '.' && !vis[i][j]) {
num = 0;
if(dfs(i,j)) {
a[top].k = num;
a[top].x = i;
a[top].y = j;
top++;
}
}
}
}
sort(a,a+top);
int ans = 0;
memset(vis,false,sizeof(vis));
for(int i = top-1;i>=k;i--) {
dfs1(a[i].x,a[i].y);
ans+=a[i].k;
}
printf("%d
",ans);
for(int i = 0;i<n;i++) printf("%s
",str[i]);
return 0;
}