Codeforces Round #375 (Div. 2)(A~D)

来源:互联网 发布:php音乐网站skin 编辑:程序博客网 时间:2024/06/01 09:32

点击打开链接

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 pointx2, 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.


题目大意:给你三个点,问以哪个为重点使另外两个点到这个点的距离和最小

思路:分类判断

#include <bits/stdc++.h>using namespace std;int main(){    int a[3];    while (~scanf("%d%d%d",&a[0],&a[1],&a[2]))    {        sort(a,a + 3);        int ans = a[2] - a[0] + a[2] - a[1];        int res = a[1] - a[0] + a[2] - a[1];        int x = a[1] - a[0] + a[2] - a[0];        int y = min(ans,min(x,res));        printf("%d\n",y);    }    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.


题目大意:给你一串长度为n的字符串,由字母,括号下划线组成。下划线隔开单词,括号里有字母的话就算一个单词。输出括号外的单词最长长度,以及多少个单词

思路:模拟

#include <bits/stdc++.h>using namespace std;char st[333];int main(){    int n;    while (~scanf("%d",&n))    {        scanf("%s",st);        int res = 0;        int ans = 0;        int temp = 0;        int flag = 0;        for (int i = 0 ; i < n ; i++ )        {            if ((st[i] >= 'a' && st[i] <= 'z') || (st[i] >= 'A' && st[i] <= 'Z'))            {                if (flag == 2 )                {                    flag = 3;                }                temp++;            }            else if ((flag == 1|| flag == 2) && (st[i] < 'a' || st[i] > 'z') || (st[i] || 'A' || st[i] > 'Z'))            {                if (ans < temp && flag != 2 && flag != 3)                {                    ans = temp;                }                if (st[i] == '(')                {                    flag = 2;                }                if ((flag == 2 || flag == 3) && st[i] == ')')                {                    if (temp > 0)                        res++;                    flag = 0;                }                if (flag == 3 && temp > 0)                {                    res++;                }                temp = 0;            }        }        if (temp > 0)        {            if (ans < temp)            {                ans = temp;            }        }        printf("%d %d\n",ans,res);    }}

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, 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 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,m,并且给你n个数字,改变其中的一些数字,使得1 ~m每个数字出现至少 n / m次

思路:设立两个指针,一个是改成的数字,一个是要改的数字的下标。注意如果每个数字都已经出现n/m次了,就可以不用改变了,这是个坑点

#include <bits/stdc++.h>using namespace std;int ar[2222];int cou[2222];int main(){    int n,m;    while (~scanf("%d%d",&n,&m))    {        memset(cou,0,sizeof(cou));        int x = n / m;        int y = n % m;        for (int i = 0 ; i < n ; i++ )        {            scanf("%d",&ar[i]);            if (ar[i] <= m)            {                cou[ar[i]]++;            }        }        int l = 1 , r = 0;        int res = 0;        while (l <= m && r < n)        {            while (ar[r] <= m && cou[ar[r]] <= x + 1)            {                if (cou[ar[r]] == x + 1 && y > 0)                {                    y--;                }                else if(cou[ar[r]] == x + 1 && y == 0)                {                    break;                }                r++;            }            while( cou[l] >= x )            {                //printf("%d %d\n",cou[l],l);                l++;            }            if ( l > m || r >= n)            {                break;            }           // printf("%d %d\n",l,r);            if (ar[r] <= m)            {                cou[ar[r]]--;            }            cou[l]++;            ar[r] = l;            res++;        }        printf("%d %d\n",x,res);        for (int i = 0 ; i < n ; i++ )        {            if (i != n - 1)            {                printf("%d ",ar[i]);            }            else             {                printf("%d\n",ar[i]);            }        }    }}

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.


题目大意:给你n*m的地图,*是陆地‘.’是水。最外面一圈是海洋。问至少填掉几个点,使得这个图上恰有k个湖泊。边缘的水连着海洋,所以不算湖泊

思路:一次dfs求出每个湖泊的大小以及这个湖泊的其中一个点,之后按照湖泊的大小排序,再dfs,开始填湖泊。也就是两次dfs

#include <bits/stdc++.h>using namespace std;char st[100][100];int vis[100][100];int n,m,k;int dir[4][2] = {0,1,0,-1,1,0,-1,0};struct N{    int x,y;    int cnt;}ar[100000];bool cmp(N a , N b){    return a.cnt < b.cnt;}int cnt = 0;int check(int a,int b){    if(a >= 0 && a < n && b >= 0 && b < m && vis[a][b] == 0)    {        if (st[a][b] == '.')        {            return 1;        }    }    return 0;}int temp = 0;int flag = 0;int i , j;void dfs(int x,int y){    temp++;    if (x == 0 || x == n - 1 || y == 0 || y == m-1)    {        flag = 1;    }    vis[x][y] = 1;    for (int i = 0 ; i < 4 ; i++ )    {        int a = x + dir[i][0];        int b = y + dir[i][1];        if (check(a,b))        {            dfs(a,b);        }    }    //printf("%d\n",flag);    if (flag != 1 && x == i && y == j)    {        ar[cnt].x = x;        ar[cnt].y = y;        ar[cnt].cnt = temp;        cnt++;    }    return;}void dfs1(int x,int y){    vis[x][y] = 1;    st[x][y] = '*';    for (int i = 0 ; i < 4 ; i++ )    {        int a = x + dir[i][0];        int b = y + dir[i][1];        if (check(a,b))        {            vis[a][b] = 1;            dfs1(a,b);        }    }    return;}int main(){    while (~scanf("%d%d%d",&n,&m,&k))    {        memset(vis,0,sizeof(vis));        for (int i = 0 ; i < n ; i++ )        {            getchar();            scanf("%s",st[i]);        }        for (i = 0 ; i < n ; i++ )        {            for (j = 0 ; j < m ; j++ )            {                if (vis[i][j] == 0 && st[i][j] == '.')                {                    flag = 0;                    temp = 0;                    dfs(i,j);                }            }        }        sort(ar , ar + cnt , cmp);        memset(vis,0,sizeof(vis));        int res = 0;        j = 0;        while (cnt > k)        {                        res += ar[j].cnt;            //printf("%d %d\n",ar[j].x,ar[j].y);            dfs1(ar[j].x,ar[j].y);            j++;            cnt--;        }        printf("%d\n",res);        for (i = 0 ; i < n ; i++ )        {            for (j = 0 ; j < m ; j++ )            {                printf("%c",st[i][j]);            }            printf("\n");        }    }    return 0;}



0 0