标签:sicily
Time Limit: 1 secs, Memory Limit: 256 MB
There are N boys and N girls at a dance party. We have measured their heights. Each boy will only dance with a girl and each girl will only dance with a boy. Everyone will dance with at most one partner. Each boy either wants to dance with a girl who is taller than him or with a girl who is shorter than him. Analogously, each girl either wants to dance with a boy who is taller than her or with a boy who is shorter than her. Boys and girls who are equally tall never want to dance with each other. Respecting everyone’s wishes, determine the maximum number of dancing pairs that can be achieved.
The first line contains an integer T (1<=T<=10), indicating the number of test cases.
Then, for each case, the first line contains the positive integer N (1<=N<=1000).
The second line contains N integers whose absolute values are between 1500 and 2500, inclusive. Their absolute values represent the height of each of the boys in millimetres. Positive height values indicate boys who want to dance with girls taller than themselves, while negative height values indicate boys who want to dance with girls shorter than themselves.
The third line contains N integers whose absolute values are between 1500 and 2500, inclusive. Their absolute values represent the height of each of the girls in millimetres. Positive height values indicate girls who want to dance with boys taller than themselves, while negative height values indicate girls who want to dance with boys shorter than themselves.
One line for each case, print the maximum number of dancing pairs.
2 1 -1800 1800 2 -1800 -2200 1900 1700
0 2
AcFast
#include <string.h> #include <vector> #include <queue> #include <stdio.h> #include <stack> #include <math.h> #include <algorithm> using namespace std; bool cmp(const int& a, const int& b) { return a > b; } int find_pairs(int f_l[], int f_h[], int length_f_l, int length_f_h) { int i = 0, j = 0, pairs = 0; while (i < length_f_l && j < length_f_h) {//注意这里,假如对方要找比他/她矮的,然后你的身高比他/她高,肯定你没戏了(别人已经是想找矮的人之中最高的了,而你又想找高的) if (-f_l[i] > f_h[j]) { pairs++; i++; j++; } else { j++; } } return pairs; } int main() { int b_find_l[1001], b_find_h[1001], g_find_l[1001], g_find_h[1001]; int case_num, n, i, j, k, temp, length_b_l, length_b_h, length_g_l, length_g_h; scanf("%d", &case_num); while (case_num--) { scanf("%d", &n); for (i = 0, j = 0, k = 0; i < n; i++) { scanf("%d", &temp); if (temp > 0) b_find_h[j++] = temp; else b_find_l[k++] = temp; } sort(b_find_l, b_find_l + k); length_b_l = k; sort(b_find_h, b_find_h + j, cmp); length_b_h = j; for (i = 0, j = 0, k = 0; i < n; i++) { scanf("%d", &temp); if (temp > 0) g_find_h[j++] = temp; else g_find_l[k++] = temp; } sort(g_find_l, g_find_l + k); length_g_l = k; sort(g_find_h, g_find_h + j, cmp); length_g_h = j; printf("%d\n", find_pairs(b_find_l, g_find_h, length_b_l, length_g_h) + find_pairs(g_find_l, b_find_h, length_g_l, length_b_h)); } return 0; }
标签:sicily
原文地址:http://blog.csdn.net/u012925008/article/details/44739755