zoukankan      html  css  js  c++  java
  • POJ 1838 Banana (并查集)

    Banana

    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 2032   Accepted: 755

    Description

    Consider a tropical forrest, represented as a matrix. The cell from the right top corner of the matrix has the coordinates (1,1), and the coordinates of the other cells are determinated by the row and the column on which the cell is. In some cells of the matrix are placed banana trees; a cell can contain no more than a banana tree. More banana trees which are neighbours on horizontal or vertical form a region of banana trees. In this kind of region, monkey CEKILI is moving easily, with her well-known agility, from a banana tree to another. 
    CEKILI is eager and the bananas from a single region are not enough for her. Tarzan wants to help his friend. For that, he may connect exactly k banana tree regions knoting more lianas and so CEKILI could move from a region to another using lianas. Obviously, Tarzan must choose the regions so that the total number of banana trees from those k regions must be maximum. 

    Detemine maximum number of banana trees which Tarzan can obtain connecting exactly k regions. 

    Input

    The input has the following structure: 
    Nr K 
    x(1) y(1) 
    y(2) y(2) 
    ... 
    x(Nr) y(Nr) 
    Nr is the number of banana trees. K is the number of zones which can be connected. x(i) is the row of the i-th banana tree, while y(i) is the column of the i-th banana tree. 
    There are Constraints: 
    • 1 <= Nr <= 16000; 
    • 1 <= x(i), y(i) <= 10000; 
    • In the tests used for grading k will never be bigger than the number of regions; 
    • Two positions are horizontally neighbours if they are on the same row and consecutive columns, respectively vertically neighbours if they are on the same column and on consecutive rows. 

    Output

    The output will contain on the first line the maximum number of banana trees that can be obtained by connecting the k regions.

    Sample Input

    10 3
    7 10
    1 1
    101 1
    2 2
    102 1
    7 11
    200 202
    2 1
    3 2
    103 1
    

    Sample Output

    9

    Source

     
    题目的意思是有一只猴子想吃香蕉,但是它只能在相邻的树上移动,这里相邻的意思是:横坐标相等时,纵坐标相差1。纵坐标相等时,横坐标相差1。如果两棵树相邻则可以合并到一个区域里面。
    问给定n棵树和k个区域,k个区域里面最多有多少香蕉树。
     
    看到区域,就想到了并查集。模拟样例之后发现,这里合并挺简单,就是给每个给定的坐标编号,如果符合合并的条件就利用编号将这两棵树合并起来。
     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<stdlib.h>
     5 #include<algorithm>
     6 using namespace std;
     7 const int MAXN=16000+10;
     8 struct node
     9 {
    10     int x,y;
    11     int index;
    12 }a[MAXN];
    13 int p[MAXN],num[MAXN];
    14 int n,k;
    15 int cmp1(const node &A,const node &B)
    16 {
    17     if(A.x!=B.x)
    18         return A.x<B.x;
    19     else
    20         return A.y<B.y;
    21 }
    22 
    23 int cmp2(const node &A,const node &B)
    24 {
    25     if(A.y!=B.y)
    26         return A.y<B.y;
    27     else
    28         return A.x<B.x;
    29 }
    30 
    31 int cmp3(const int a,const int b)
    32 {
    33     return a>b;
    34 }
    35 int findfa(int x)
    36 {
    37     return p[x]==x?x:p[x]=findfa(p[x]);
    38 }
    39 
    40 int main()
    41 {
    42     //freopen("in.txt","r",stdin);
    43     while(scanf("%d %d",&n,&k)!=EOF)
    44     {
    45         for(int i=0;i<n;i++)
    46         {
    47             scanf("%d %d",&a[i].x,&a[i].y);
    48             a[i].index=i;
    49         }
    50 
    51         for(int i=0;i<n;i++)
    52         {
    53             p[i]=i;
    54             num[i]=1;
    55         }
    56         sort(a,a+n,cmp1);
    57         for(int i=0;i<n-1;i++)
    58         {
    59             if(a[i].x==a[i+1].x && a[i+1].y-a[i].y==1)
    60             {
    61                 int xx=findfa(a[i].index);
    62                 int yy=findfa(a[i+1].index);
    63                 if(xx==yy)
    64                     continue;
    65                 else
    66                 {
    67                     p[yy]=xx;
    68                     num[xx]+=num[yy];
    69                     num[yy]=0;
    70                 }
    71             }
    72         }
    73 
    74         sort(a,a+n,cmp2);
    75         for(int i=0;i<n-1;i++)
    76         {
    77             if(a[i+1].x-a[i].x==1 && a[i+1].y==a[i].y)
    78             {
    79                 int xx=findfa(a[i].index);
    80                 int yy=findfa(a[i+1].index);
    81                 if(xx==yy)
    82                     continue;
    83                 else
    84                 {
    85                     p[yy]=xx;
    86                     num[xx]+=num[yy];
    87                     num[yy]=0;
    88                 }
    89             }
    90         }
    91 
    92         sort(num,num+n);
    93         int ans=0;
    94         for(int i=n-1;i>=n-k;i--)
    95             ans+=num[i];
    96         printf("%d
    ",ans);
    97     }
    98     return 0;
    99 }
    View Code
  • 相关阅读:
    安装MySQL-python时报错
    人的成功平台很重要
    开源运维工具
    遗忘Windows Server 2008R2密码的处理方法
    操作系统下载和操作系统更新失败解决
    说说对SQL 语句优化有哪些方法?
    Git彻底删除历史提交记录的方法
    MSSQL备份脚本
    .NET Core Data Access
    各种数据库默认端口总结
  • 原文地址:https://www.cnblogs.com/clliff/p/3957549.html
Copyright © 2011-2022 走看看