Poj 1838 banana (query set)

Source: Internet
Author: User

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 in no more than a banana tree. more banana trees which are neighbors 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 cocould move from a region to another using lianas. obviusly, 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 neighbors if they are on the same row and consecutive columns, respectively vertically neighbors if they are on the same column and on consecutive rows.

Output

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

Sample Input

10 37 101 1101 12 2102 17 11200 2022 13 2103 1

Sample output

9


In a set, only when the abscissa is equal, the ordinate difference is 1, or the ordinate is equal, the abscissa difference is 1. You only need to merge the conditions where the horizontal coordinates are equal and the vertical coordinates are equal. Use the query set.


#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <set>#include <stack>#include <cctype>#include <algorithm>#define lson o<<1, l, m#define rson o<<1|1, m+1, rusing namespace std;typedef long long LL;const int mod = 99999997;const int MAX = 0x3f3f3f3f;const int maxn = 16005;int n, k, f[maxn], rank[maxn];struct C {    int x, y, id;} in[maxn];int Find(int x) {    return x == f[x] ? x : f[x] = Find(f[x]);}bool cmp0 (C a, C b) {    if(a.x != b.x) return a.x < b.x;    return a.y < b.y;}bool cmp1 (C a, C b) {    if(a.y != b.y) return a.y < b.y;    return a.x < b.x;}bool cmp2 (int a, int b) {    return a > b;}void Union (int p, int q) {    int i = Find(p), j = Find(q);    if(i == j) return;    rank[i] += rank[j];    f[j] = i;    rank[j] = 0;}int main(){    cin >> n >> k;    for(int i = 0; i < n; i++) {        in[i].id = i;        scanf("%d%d", &in[i].x, &in[i].y);    }    for(int i = 0; i < n; i++) rank[i] = 1, f[i] = i;    sort(in, in+n, cmp0);    for(int i = 0; i < n-1; i++) {        C cur = in[i], next = in[i+1];        if(cur.x == next.x && next.y-cur.y == 1)            Union(cur.id, next.id);    }    sort(in, in+n, cmp1);    for(int i = 0; i < n-1; i++) {        C cur = in[i], next = in[i+1];        if(cur.y == next.y && next.x-cur.x == 1)            Union(cur.id, next.id);    }    sort(rank, rank+n, cmp2);    int sum = 0;    for(int i = 0; i < k; i++) sum += rank[i];    cout << sum << endl;    return 0;}


Zookeeper

Poj 1838 banana (query set)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.