CodeForces 375(div2)

来源:互联网 发布:整理收集文档软件 编辑:程序博客网 时间:2024/06/05 04:34
A. The New Year: Meeting Friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 x1x2 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.

题意:

给你3个数,输出最大值与最小值的差。

#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <iostream>#include <cmath>using namespace std;typedef long long int ll;int main(){  int a[3]; for(int i=0; i<3; i++) scanf("%d",&a[i]);sort(a,a+3);printf("%d\n",a[2]-a[0]);return 0;}


B. Text Document Analysis
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.


题意:

给你一串字符串,统计括号内有多少个单词,括号外最长的单词多长。

括号内外一样统计,用一个flag标记一下是内是外即可,遇到左括号flag=1,表示接下来的统计的是括号店内的数据,遇到右括号flag=0。

#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <iostream>#include <cmath>using namespace std;typedef long long int ll;int main(){char s[300];int N;scanf("%d",&N);scanf("\n%s",s);int max_len = 0;int Count_in = 0;int flag = 0;int len=0;for(int i=0; i<N; ){if(s[i]=='('){flag = 1;i++;}else if(s[i]==')'){flag = 0;i++;}else if(s[i]=='_')i++;else{int haha=0;while((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){i++;haha++;}if(flag==1)Count_in++;elsemax_len = max(max_len,haha);}}printf("%d %d\n",max_len,Count_in);return 0;}


C. Polycarp at the Radio
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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, whereai 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 a1, a2, ..., an (1 ≤ ai ≤ 109), 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 21 2 3 2
output
2 11 2 1 2 



input
7 31 3 2 2 2 2 1
output
2 11 3 3 2 2 2 1 



input
4 41000000000 100 7 1000000000
output
1 41 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.

题意:

有一个歌单,歌单上面有n首歌,你喜欢1,2,3....m号乐队唱的歌。

你希望改变这个歌单,使得你喜欢的乐队们唱歌唱得最少的尽量大。

问你最少修改多少次,且输出方案。

注意并非歌单上所有的歌都需要修改成1-m乐队唱的,只要满足使得你喜欢的乐队们唱歌唱得最少的尽量大即可。


#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <iostream>#include <cmath>using namespace std;typedef long long int ll;int a[2005],b[2005];int main(){int N,M;scanf("%d%d",&N,&M);for(int i=0; i<N; i++){scanf("%d",&a[i]);if(a[i]<=M)b[a[i]]++;}int cur = 1,ans = 0;int num = N/M;for(int i=0; i<N; i++){if(a[i]<=M && b[a[i]]<=num)continue;//执行下来的话说明a[i]这个数要被替换//接下来寻找要被哪个数替换 while(cur<=M && b[cur]>=num)cur++; if(cur>M)   break;ans++;if(a[i]<=M)  b[a[i]]--;a[i] = cur;b[cur]++;}printf("%d %d\n",num,ans);int flag=1;for(int i=0; i<N; i++){if(--flag==0)printf("%d",a[i]);elseprintf(" %d",a[i]);}cout << endl;return 0;}


D. Lakes in Berland
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nm and k (1 ≤ n, m ≤ 500 ≤ 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.


题意:

给你一张图,星代表陆地,点代表水域。lake是这样定义的:不与海洋相连的水域。让你留下K个lake,其余的lake你要填上土变成陆地,问你最少填多少土。并将填好的图输出。数据量很小,直接BFS各种搜就可以。

#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <iostream>#include <cmath>using namespace std;typedef long long int ll;char a[55][55];int N,M,K;int book[55][55];struct Node{int x;int y;int big;};struct hehe{int x;int y;};hehe Q[3000];int Count = -1;Node lake[3000];int sum=-1;int tmp=0;void F(int i,int j){a[i][j] = '*';Q[++Count].x = i;Q[Count].y = j;if(i-1>=1 && a[i-1][j]=='.')F(i-1,j);if(i+1<=N && a[i+1][j]=='.')F(i+1,j);if(j-1>=1 && a[i][j-1]=='.')F(i,j-1);if(j+1<=M && a[i][j+1]=='.')F(i,j+1);}void FF(int i,int j){tmp++;book[i][j] = 1;if(i-1>=1 && a[i-1][j]=='.' && book[i-1][j]==0)FF(i-1,j);if(i+1<=N && a[i+1][j]=='.' && book[i+1][j]==0)FF(i+1,j);if(j-1>=1 && a[i][j-1]=='.' && book[i][j-1]==0)FF(i,j-1);if(j+1<=M && a[i][j+1]=='.' && book[i][j+1]==0)FF(i,j+1);}bool cmp(Node a,Node b){return a.big<b.big;}void FFF(int i,int j){a[i][j] = '*';if(i-1>=1 && a[i-1][j]=='.')FFF(i-1,j);if(i+1<=N && a[i+1][j]=='.')FFF(i+1,j);if(j-1>=1 && a[i][j-1]=='.')FFF(i,j-1);if(j+1<=M && a[i][j+1]=='.')FFF(i,j+1);}int main(){scanf("%d %d %d",&N,&M,&K);for(int i=1; i<=N; i++)scanf("\n%s",a[i]+1);for(int i=1; i<=N; i++)for(int j=1; j<=M; j++){if(a[i][j]=='.'&&(i==1 || i==N || j==1 || j==M))F(i,j);//把与海洋相连的区域都给先填上土,并用Q记录下来坐标 }memset(book,0,sizeof(book));for(int i=1; i<=N; i++)for(int j=1; j<=M; j++){if(a[i][j]=='.' && book[i][j]==0){ //统计每个lake的大小并记录其位置 tmp = 0;FF(i,j);lake[++sum].big = tmp;lake[sum].x = i;lake[sum].y = j;}}sort(lake,lake+sum+1,cmp);//lake的大小排序 int ans=0;for(int i=0; i<sum+1-K; i++){ans += lake[i].big;FFF(lake[i].x,lake[i].y);//讲sum+1-K个lake给填上土,ans记录土的用量 }for(int i=0; i<=Count; i++)//把与海相连的水域还原 a[Q[i].x][Q[i].y] = '.';printf("%d\n",ans);for(int i=1; i<=N; i++)printf("%s\n",a[i]+1);return 0;}




0 0
原创粉丝点击