Codeforces Round #375 (Div. 2)

A. The New Year: Meeting Friends

http://codeforces.com/problemset/problem/723/A

题意: 给你三个点 找出一个点到这三个点的距离的绝对值之和最小

思路:这个点肯定在最大点和最小点之间 扫一遍就好了

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <stdlib.h>
 4 using namespace std;
 5 const int maxn=110;
 6 int dis[maxn];
 7 int x[maxn];
 8 int main()
 9 {
10     while(scanf("%d %d %d",&x[1],&x[2],&x[3])!=EOF)
11     {
12         int minn=0x3f3f3f3f;
13 
14         for(int i=min(x[1],min(x[2],x[3]));i<=max(x[1],max(x[2],x[3]));i++)
15         {
16             dis[i]=0;
17             for(int j=1;j<=3;j++)
18                 dis[i]+=abs(x[j]-i);
19                 minn=min(minn,dis[i]);
20         }
21         printf("%d
",minn);
22     }
23     return 0;
24 }
View Code

B. Text Document Analysis

http://codeforces.com/problemset/problem/723/B

题意:统计括号内的单词有多少个 括号外最长的单词的长度

思路:强行模拟就行

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 int n;
 8 const int MAXN=270;
 9 char str[MAXN];
10 bool judge(char s)
11 {
12     if(s>='A'&&s<='Z') return true;
13     if(s>='a'&&s<='z') return true;
14     return false;
15 }
16 int main()
17 {
18     while(scanf("%d",&n)!=EOF)
19     {
20         for(int i=1;i<=n;i++)
21              scanf(" %c",&str[i]);
22              bool flag=false;
23              int cnt=0;
24              int maxn=0;
25         for(int i=1;i<=n;i++)
26         {
27 
28             if(str[i]=='(')
29             {
30                 int pos=i;
31                 for(int j=pos+1;;j++)
32                 {
33                     if(str[j]==')')
34                     {
35                         i=j;
36                         break;
37                     }
38                     if(judge(str[j]))
39                     {
40                         int k=j;
41                         cnt++;
42                         while(judge(str[k])) k++;
43                         j=k-1;
44                     }
45                 }
46             }
47             else
48             {
49                 if(judge(str[i]))
50                 {
51                     int k=0;
52                     while(judge(str[k+i]))k++;
53                     i+=(k-1);
54                     maxn=max(maxn,k);
55 
56                 }
57             }
58         }
59         printf("%d %d
",maxn,cnt);
60     }
61     return 0;
62 }
View Code

C. Polycarp at the Radio

http://codeforces.com/problemset/problem/723/C

题意:给你n个数 让你把他全部换成1-m之间的数 出现次数最小的数的次数要在所有情况中是最大

思路:很明显这个最小出现次数一定是n/m 因为数据量很小 所以枚举每个1-m中间的数,有不符合情况的直接在n个数中找数替换就行了

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <map>
 8 using namespace std;
 9 const int maxn=2010;
10 int a[maxn];
11 int tmp[maxn];
12 int main()
13 {
14     int n,m;
15     while(scanf("%d %d",&n,&m)!=EOF)
16     {
17         memset(tmp,0,sizeof(tmp));
18         for(int i=1; i<=n; i++) scanf("%d",&a[i]);
19         for(int i=1; i<=n; i++)
20         {
21             if(a[i]<=m) tmp[a[i]]++;
22         }
23         int ans=n/m;
24         int cnt=0;
25        for(int i=1;i<=m;i++)
26        {
27            if(tmp[i]<ans)
28            {
29                for(int j=1;j<=n;j++)
30                {
31                    if(a[j]>m)
32                    {
33                        a[j]=i;
34                        tmp[i]++;
35                        cnt++;
36                    }
37                    else if(a[j]<=m)
38                    {
39                        if(tmp[a[j]]>ans)
40                        {
41                            cnt++;
42                            tmp[a[j]]--;
43                             tmp[i]++;
44                             a[j]=i;
45                        }
46                    }
47                    if(tmp[i]==ans) break;
48 
49                }
50            }
51        }
52         printf("%d %d
",ans,cnt);
53         for(int i=1;i<=n;i++)
54         {
55             printf("%d%c",a[i],i==n?'
':' ');
56         }
57 
58 
59     }
60     return 0;
61 }
View Code

D. Lakes in Berland

http://codeforces.com/problemset/problem/723/D

题意:有一副图,图有陆地和水,被陆地包围起来的叫湖,能连到外面的水叫海,水可以被填充,填充到最后要留下k个湖,问最少要填充多少水

思路:bfs处理出每个湖,然后排序从包含水量最少的开始填充

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <vector>
  7 #include <queue>
  8 using namespace std;
  9 const int maxn=60;
 10 int nex[5][2]= {{0,0},{0,1},{0,-1},{-1,0},{1,0}};
 11 char G[maxn][maxn];
 12 bool flag[maxn][maxn];
 13 bool vis[maxn][maxn];
 14 struct node
 15 {
 16     node(){}
 17     node(int a,int b,int c):x(a),y(b),num(c){}
 18     int x,y;
 19     int num;
 20 };
 21 bool cmp(const node&a,const node&b)
 22 {
 23     return a.num<b.num;
 24 }
 25 node ans[maxn*maxn];
 26 int n,m,k;
 27 int main()
 28 {
 29     while(scanf("%d %d %d",&n,&m,&k)!=EOF)
 30     {
 31         int tot=0;
 32         memset(flag,false,sizeof(flag));
 33         for(int i=1; i<=n; i++)
 34             for(int j=1; j<=m; j++)
 35                 scanf(" %c",&G[i][j]);
 36         for(int i=1; i<=n; i++)
 37             for(int j=1; j<=m; j++)
 38             {
 39                 node e;
 40                 if(!flag[i][j]&&G[i][j]=='.')
 41                 {
 42                     queue<node> q;
 43                     int cnt=1;
 44                     e.x=i,e.y=j;
 45                     flag[i][j]=true;
 46                     q.push(e);
 47                     bool flag1=true;
 48                     while(!q.empty())
 49                     {
 50                         int x=q.front().x;
 51                         int y=q.front().y;
 52                         q.pop();
 53                         for(int k=1; k<=4; k++)
 54                         {
 55                             int sx=x+nex[k][0];
 56                             int sy=y+nex[k][1];
 57                             e.x=sx,e.y=sy;
 58                             if(G[sx][sy]=='.'&&!flag[sx][sy])
 59                             {
 60                                 flag[sx][sy]=true;
 61                                 cnt++;
 62                                 q.push(e);
 63                             }
 64                             if(sx<1||sx>n||sy<1||sy>m)
 65                             {
 66                                 flag1=false;
 67 
 68                             }
 69                         }
 70 
 71                     }
 72 
 73                     if(flag1) ans[tot++]=node(i,j,cnt);
 74                 }
 75             }
 76           //  printf("%d
",tot);
 77             sort(ans,ans+tot,cmp);
 78      /*   for(int i=0;i<tot;i++)
 79         {
 80             printf("%d %d %d
",ans[i].x,ans[i].y,ans[i].num);
 81         }*/
 82         memset(flag,false,sizeof(flag));
 83         int sum=0;
 84         if(tot>k)
 85         {
 86             node e;
 87             for(int i=0;i<tot-k;i++)
 88             {
 89 
 90                 queue<node> q;
 91                 sum+=ans[i].num;
 92                 e.x=ans[i].x;
 93                 e.y=ans[i].y;
 94                 q.push(e);
 95                 while(!q.empty())
 96                 {
 97                     int x=q.front().x;
 98                     int y=q.front().y;
 99                     G[x][y]='*';
100                     q.pop();
101                     for(int k=1;k<=4;k++)
102                     {
103                         int sx=x+nex[k][0];
104                         int sy=y+nex[k][1];
105                         e.x=sx,e.y=sy;
106                         if(G[sx][sy]=='.')
107                         {
108                             q.push(e);
109                             G[sx][sy]='*';
110                         }
111                     }
112                 }
113 
114             }
115         }
116         printf("%d
",sum);
117         for(int i=1;i<=n;i++)
118         {
119             for(int j=1;j<=m;j++)
120             {
121                 printf("%c",G[i][j]);
122             }
123             printf("
");
124         }
125 
126     }
127 }
View Code
原文地址:https://www.cnblogs.com/as3asddd/p/5934131.html