第一次参加Codeforces,这次算上终测一共过了4题,果然读英文题和中文题还是有很大差距的。
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.
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.
Print one integer — the minimum total distance the friends need to travel in order to meet together.
7 1 4
6
30 20 10
20
水题1,求3个人聚到一人家里走的最短距离,最大值减最小值就好了。
//
// main.cpp
// A. The New Year: Meeting Friends
//
// Created by teddywang on 2016/10/3.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
long long int a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
long long int maxn=max(a,b);
maxn=max(maxn,c);
long long int minn=min(a,b);
minn=min(minn,c);
printf("%lld\n",maxn-minn);
}
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).
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.
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).
37 _Hello_Vasya(and_Petya)__bye_(and_OK)
5 4
37 _a_(_b___c)__de_f(g_)__h__i(j_k_l)m__
2 6
27 (LoooonG)__shOrt__(LoooonG)
5 2
5 (___)
0 0
//
// main.cpp
// B. Text Document Analysis
//
// Created by teddywang on 2016/10/3.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
char s[1000];
int main()
{
scanf("%d",&n);
scanf("%s",s);
int len=strlen(s);
int flag=0;
int a=0,b=0;
int m=0,l=0;
for(int i=0;i<len;i++)
{
if(s[i]=='(')
{
if(m!=0)
a=max(a,m);
m=0;i++;
while(s[i]!=')')
{
if(s[i]=='_')
{
if(l!=0)
b++;
l=0;
}
else l++;
i++;
}
if(s[i]==')')
{
if(l!=0)
b++;
l=0;
}
}
else
{
if(s[i]=='_')
{
if(m!=0) a=max(m,a);
m=0;
}
else m++;
}
}
if(m!=0)
{
a=max(m,a);
}
cout<<a<<" "<<b<<endl;
}
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.
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.
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.
4 2 1 2 3 2
2 1 1 2 1 2
7 3 1 3 2 2 2 2 1
2 1 1 3 3 2 2 2 1
4 4 1000000000 100 7 1000000000
1 4 1 2 3 4
题目3,题目比较长且比较绕,不过花点时间理清就发现是贪心的水题。题意是有很多乐团来参演,给出n首歌演唱的乐团,现在要把所有的乐团改为1到m个乐团,并且所有乐团参演次数的最小值要最大化。明显是求平均数,最大值就是这个平均数,然后把少于平均数的部分填上就行,这里用大与m或者出现次数多于平均数的部分替换。注意只要最小值为平均数即可,不需要把所有不在m内的数都替换掉。
<span style="font-size:14px;">//
// main.cpp
// C. Polycarp at the Radio
//
// Created by teddywang on 2016/10/3.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll a[2010],cnt[2010];
int main()
{
ll n,m;
scanf("%lld%lld",&n,&m);
memset(cnt,0,sizeof(cnt));
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++)
{
scanf("%lld",&a[i]);
if(a[i]<=m&&a[i]) cnt[a[i]]++;
}
ll ans=n/m,num=0,pos=1;
for(int i=0;i<n;i++)
{
if(a[i]>m||cnt[a[i]]>ans)
{
while(cnt[pos]>=ans&&pos<=m) pos++;
if(pos>m) break;
if(a[i]<=m)
cnt[a[i]]--;
num++;
cnt[pos]++;
a[i]=pos;
}
}
printf("%lld %lld\n",ans,num);
printf("%lld",a[0]);
for(int i=1;i<n;i++)
printf(" %lld",a[i]);
printf("\n");
}</span><span style="font-size: 0.9em;">
</span>
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.
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.
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.
5 4 1 **** *..* **** **.* ..**
1 **** *..* **** **** ..**
3 3 0 *** *.* ***
1 *** *** ***
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.
//
// main.cpp
// D. Lakes in Berland
//
// Created by teddywang on 2016/10/3.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char maze[55][55];
int vis[55][55];
int m,n,k;
int len,cnt,flag;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct sizess{
int sum;
int xx,yy;
friend bool operator < (sizess a,sizess b)
{
return a.sum<b.sum;
}
}sizes[3003];
int judge(int x,int y)
{
if(x==0||x==n-1||y==0||y==m-1) return 0;
else return 1;
}
int ju(int x,int y)
{
if(x<0||x>n-1||y<0||y>m-1) return 0;
if(maze[x][y]=='*') return 0;
if(vis[x][y]) return 0;
return 1;
}
void dfs(int x,int y)
{
if(judge(x,y)==0) flag=1;
for(int i=0;i<4;i++)
{
int bx=x+dx[i];
int by=y+dy[i];
if(ju(bx,by)==0) continue;
vis[bx][by]=1;
cnt++;
dfs(bx,by);
}
}
void dfss(int x,int y)
{
for(int i=0;i<4;i++)
{
int bx=x+dx[i];
int by=y+dy[i];
if(x<0||x>n-1||y<0||y>m-1) continue;
if(maze[bx][by]=='*') continue;
if(vis[bx][by]==0) continue;
maze[bx][by]='*';
dfss(bx,by);
}
}
int main()
{
cin>>n>>m>>k;
memset(sizes,0,sizeof(sizes));
memset(vis,0,sizeof(vis));
len=0;
for(int i=0;i<n;i++)
{
scanf("%s",maze[i]);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(maze[i][j]=='.'&&!vis[i][j])
{
vis[i][j]=1;
cnt=1;
flag=0;
dfs(i,j);
if(flag==0)
{
//cout<<i<<" "<<j<<endl;
sizes[len].xx=i;
sizes[len].yy=j;
sizes[len++].sum=cnt;
}
}
}
}
sort(sizes,sizes+len);
int l=len-k;
int p=0;
for(int i=0;i<l;i++)
{
p+=sizes[i].sum;
maze[sizes[i].xx][sizes[i].yy]='*';
dfss(sizes[i].xx,sizes[i].yy);
}
cout<<p<<endl;
for(int i=0;i<n;i++)
{
printf("%s\n",maze[i]);
}
}