This question is another way to listen and learn to eat school brother brother speak, I feel pretty good ^ _ ^, such as if the younger brother often hear a lot of talk about the title can harvest it, probably next year is expected written, ha ha ~
Problem:
N-plane to the individual, the position (x, y) given tuple, m plane there door, a position (x, y) given. Now the agreement can only enter every door a man, and man can only move left and down (to x-1 y-1 and moving), I ask how many people are up to the door?
Solution:
The door and the x value by descending order, the enumeration door. For the current door enumerated i, is greater than the door [i] y into the value of all .x set, the minimum is found greater than or equal door [i] .y, and assign it to the gate i, and then from set excluded, then the door enumeration i + 1.
code show as below:
#include <the iostream> #include <algorithm> #include < SET > the using namespace STD; / * Problem: to the plane of the n-individual, the position (x, y) tuple given plane there door m , from the location (x, y) given. Now the agreement can only enter every door a man, and man can only move left and down (to x-1 y-1 and mobile), will at most how many people the door? Solution: The door and the x value by descending order, the enumeration door. For the current door enumerated i, is greater than the door [i] y into the value of all .x set, the minimum is found greater than or equal door [i] .y, and assign it to the gate i, and then from set excluded, then the door enumeration i + 1. * / Const int N = 1E3 + . 5 ; typedef pair < int , int > Tuple; BOOL CMP ( const Tuple A, const Tuple B) { return a.first > b.first; } multiset<int> s; multiset<int>::iterator it; int main() { int n, m; cin >> n >> m; Tuple people[N], door[N]; for (int i = 0; i < n; i++) { cin >> people[i].first >> people[i].second; } sort(people, people + n, cmp); for (int i = 0; i < m; i++) { cin >> door[i].first >> door[i].second; } sort(door, door + m, cmp); int ans = 0; int k = 0; for (int i = 0; i < m; i++) { while (k < n) { if (people[k].first >= door[i].first) { s.insert(people[k].second); k++; } } if (s.count(door[i].second) > 0) { ans++; s.erase(s.find(door[i].second)); } else { it = s.upper_bound(door[i].second); if (it != s.end()) { ans++; s.erase(it); } } } std::cout << "Answer = " << ans << endl; return 0; } /* 2 2 5 3 6 5 3 4 4 2 */