POJ 1838 Banana (query set), poj1838banana

Source: Internet
Author: User

POJ 1838 Banana (query set), poj1838banana

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
What are the uses and query sets of Peking University acm questions?

POJ 1611 The Suspects most basic query set

POJ 2524 basic Ubiquitous Religions query set
POJ 1182 extension of food chain and query set
Note: There is only one group of data;
Make full use of the conditions given by the question: there are three types of animals A, B, C, and the food chains of these three types of animals.
It constitutes an interesting ring. A eats B, B eats C, and C eats. That is to say, there are only three groups.
POJ 2492 A Bug's Life and expansion of query Sets
Method 1: depth-first Traversal
Each traversal record indicates whether the path is male or female. Only male-> female, female-> male meets the requirement. Otherwise, locate the homosexuality and end the program.
Method 2: Bipartite Graph Matching
Method 3: Expand the query set: it is very similar to 1182, but there are two groups, and 1182 is three groups, 1611 is not limited.
POJ 1861 Network = zju_1542 and query set + custom sorting + greedy search "minimal spanning tree"
The answer is not unique, but QSORT () and SORT () can be used on ZOJ, and only SORT () can be used on POJ...
POJ 1703 Find them, Catch them and query the expansion of the Set
This is very similar to POJ 2492 A Bug's Life, that is, the code will be AC after A slight modification!
Note: And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. There are only two groups.
POJ 2236 Wireless Network and query set Application
Note: 1. Check the Set; 2. The range of N can be 1001; 3. From the N + 1 line, the first input can be a string.
POJ 1988 Cube Stacking and good query set Application
1. Like Galaxy hero legend = NOI2002 Galaxy; 2. An array behind [x] is added to record the relative position of warship x in the column; 3. For detailed problem report, see Galaxy hero legend.

JOJ 1905 Freckles = POJ 2560 Minimum Spanning Tree

Method 1: Prim algorithm; Method 2: Query sets to implement the Kruskar Algorithm for the Minimum Spanning Tree

Poj1611 what is wrong with me? (Check set + water question, but I did it with STL)

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.