Codeforces Round #375 (Div. 2) 个人题解

第一次参加Codeforces,这次算上终测一共过了4题,果然读英文题和中文题还是有很大差距的。

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

水题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);
}

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

水题2,求括号里单词个数和括号外单词最大长度,并且括号没有嵌套。

//
//  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;
}

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

题目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>

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.

水题4,不算边界上的连通图,统计图中所有连通图的个数并填到剩下k个,求最少填充几块,首先一遍dfs统计连通图个数以及位置和大小,一遍排序找出要填的位置,再一遍dfs填上即可。
//
//  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]);
    }
}


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值