【POJ 1838 --- Banana】

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

  • 解题思路

分别通过对x,y的排序得到满足相邻的树的坐标。运用并查集标记每一块区域。最后得到每个区域中树的个数,奖其中最多的几个相连。即可解题。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
#define MAXN 16010
struct Node
{int x,y;int num;
}arr[MAXN];
int b[MAXN],result[MAXN];bool compareX(Node &a,Node &b)
{if(a.x != b.x){return a.x < b.x;}return a.y < b.y;
}bool compareY(Node &a,Node &b)
{if(a.y!=b.y){return a.y<b.y;}return a.x < b.x;
}bool compare(int a,int b)
{return a > b;
}
//查找
int seek(int x)
{if(x==b[x])return x;return b[x]=seek(b[x]);
}
//设置连接
int setting(int x,int y)
{x=seek(x);y=seek(y);if(x==y)return 0;elseb[x]=y;return 0;
}int main()
{int n,m,ans=0;scanf("%d%d",&n,&m);for(int i=0;i<n;i++){b[i]=i;scanf("%d%d",&arr[i].x,&arr[i].y);arr[i].num = i+1;}sort(arr,arr+n,compareX);for(int i=0;i<n-1;i++){if(arr[i].x==arr[i+1].x && arr[i+1].y-arr[i].y==1){setting(arr[i].num,arr[i+1].num);}}sort(arr,arr+n,compareY);for(int i=0;i<n-1;i++){if(arr[i].y==arr[i+1].y && arr[i+1].x-arr[i].x==1){setting(arr[i].num,arr[i+1].num);}}//将每个树分别标记到每块区域中for(int i=0;i<n;i++){b[i]=seek(i);}//得到每块区域树的个数for(int i=0;i<n;i++){result[b[i]]++;}sort(result,result+n+1,compare);//获取树的数量最多的m个区域中树的总数for(int i=0;i<m;i++){ans += result[i];}printf("%d\n",ans);return 0;
}

【POJ 1838 --- Banana】相关推荐

  1. 【POJ No. 1840】公式 Eqs

    [POJ No. 1840]公式 Eqs POJ题目地址 [题意] 考虑方程a1 x1^3 +a2 x2^3 +a3 x3^3=0,系数是在[-50,50]区间的整数,xi ∈[-50,50],xi ...

  2. 【POJ No. 3764】 最长xor 路径 The xor-longest Path

    [POJ No. 3764] 最长xor 路径 The xor-longest Path 北大OJ 题目地址 [题意] 在边权树中,路径p的xor长度被定义为路径p上边权的 ,⊕是xor运算符,表示异 ...

  3. 【POJ No. 1986】 距离查询 Distance Queries

    [POJ No. 1986] 距离查询 Distance Queries 北大OJ 题目地址 [题意] 约翰有N 个农场,标记为1-N .有M 条垂直和水平的道路连接农场,每条道路的长度各不相同.每个 ...

  4. 【POJ No. 3275】奶牛排序 Ranking the Cows

    [POJ No. 3275]奶牛排序 Ranking the Cows 官方题目地址 [题意] 约翰想按照奶牛的产奶能力给它们排序. 已知有N (1≤N ≤1 000)头奶牛,而且知道这些奶牛的M ( ...

  5. 【POJ No. 2431】 丛林探险 Expedition

    [POJ No. 2431] 丛林探险 Expedition 北大OJ 题目地址 [题意] 一群人开着一辆卡车冒险进入丛林深处,卡车油箱坏了,每走1米就会漏1升油,他们需要到最近的城镇(距离不超过10 ...

  6. 【POJ No. 3104】 烘干衣服 Drying

    [POJ No. 3104] 烘干衣服 Drying POJ题目地址 [题意] 可以使用散热器烘干衣服.但散热器很小,所以它一次只能容纳一件衣服. 简有n 件衣服,每件衣服在洗涤过程中都带有ai 的水 ...

  7. 【POJ No. 2778】DNA 序列 DNA Sequence

    [POJ No. 2778]DNA 序列 DNA Sequence 北大OJ 题目地址 [题意] DNA序列是一个只包含A.C.T和G的序列.分析DNA序列片段非常有用,若动物的DNA序列包含片段AT ...

  8. 【POJ No. 1019】数字序列 Number Sequence

    [POJ No. 1019]数字序列 Number Sequence 北大OJ 题目地址 [题意] 给出单个正整数i ,编写程序以找到位于数字组S 1 , S 2 , -, Sk 序列中第i 位上的数 ...

  9. 【POJ No. 1256】 字谜 Anagram

    [POJ No. 1256] 字谜 Anagram 官方题目地址 [题意] 写程序从一组给定的字母中生成所有可能的单词.例如,给定单词"abc",应该输出单词"abc&q ...

  10. 【POJ No. 3258】 跳房子游戏 River Hopscotch

    [POJ No. 3258] 跳房子游戏 River Hopscotch POJ题目地址 [题意] 跳房子游戏指从河中的一块石头跳到另一块石头,这发生在一条又长又直的河流中,从一块石头开始,到另一块石 ...

最新文章

  1. Spring Cloud Gateway CORS 方案看这篇就够了
  2. 解决mysql“Access denied for user‘root‘@‘IP地址‘“问题
  3. python手机版安卓-当python遇到Android手机 那么,万物皆可盘
  4. 零代码实战 SharePoint 2013 BCS:三、实干篇
  5. MySQL与会计报表_会计报表的18项必须审核的数据
  6. swoole不是php,Swoole
  7. VTK:PolyData之MultiBlockMergeFilter
  8. 中南大学夏令营集训营
  9. Uboot分析(三)
  10. ASP注入漏洞基础教程(二)
  11. 预测数据时数据类型是object导致报错TypeError: unsupported operand type(s) for -: ‘str‘ and ‘float‘
  12. tomcat启动问题,卡在 preparing launch delegate 100% 的解决方法
  13. 给BERT加一个loss就能稳定提升?斯坦福+Facebook最新力作!
  14. mysql服务器系统优化_mysql服务器系统优化
  15. html多行文本框_HTML的七大标签怎么运用?
  16. php $GLOBALS 超全局变量的理解
  17. C++ vector理解
  18. 安卓一键清理内存_雨点清理下载-雨点清理v1.6手机下载
  19. 实现HTML的简单压缩
  20. 基于NET Core 的Nuget包制作、发布和运用流程

热门文章

  1. 【Axure原型分享】会议管理原型模板
  2. win7录屏_win7/win10电脑屏幕录像工具哪款比较好?--QVE屏幕录制
  3. Unity粒子特效系列-闪星星的宝箱
  4. 浅谈JSP编程的网店管理系统构造
  5. windows10如何卸载edge浏览器 1809版本
  6. SpringBoot---Tomcat日志配置
  7. idea中设置字体大小
  8. 【C++STL库】stack常用函数学习
  9. javaweb day14
  10. ug齿条插件_NX9.0齿轮齿条运动仿真—齿轮工具箱巧用及渐开线制作