Hdu5360Hiking greedy, hdu5360hiking greedy
// N people accept the invitation only when the number of people who have already accepted the invitation ranges from l [I], r [I] // how to set the invitation order so that the maximum number of people can be invited // sort the requests from small to large // then maintain a set, store the set object to the owner who meets the conditions on the left, // then, the greedy policy is to take the smallest person on the right that meets the conditions on the left. // after each invitation, delete the person on the right that does not meet the conditions # include <cstdio> # include <cstring> # include <iostream> # include <set> # include <algorithm> using namespace std; const int maxn = 100010; int a [maxn]; int t; int n; int vis [maxn]; struct node {int l, r, id; bool operator <(const struct node tmp) const {if (l = Tmp. l) return r <tmp. r; return l <tmp. l ;}} p [maxn]; multiset <pair <int, int> s; multiset <pair <int, int >>:: iterator it; int main () {// freopen ("in.txt", "r", stdin); scanf ("% d", & t); memset (vis, 0, sizeof (vis )); while (t --) {scanf ("% d", & n); for (int I = 1; I <= n; I ++) scanf ("% d ", & p [I]. l), p [I]. id = I; for (int I = 1; I <= n; I ++) scanf ("% d", & p [I]. r); sort (p + 1, p + 1 + n); int Ans = 0; int I = 1; s. clear (); while (1) {while (ans> = p [I]. l & I <= n) {s. insert (make_pair (p [I]. r, p [I]. id); I ++;} if (! S. size () break; while (s. size () {it = s. begin (); if (it-> first> = ans) break; s. erase (s. begin ();} if (s. size () {it = s. begin (); a [++ ans] = it-> second; vis [it-> second] = 1; s. erase (s. begin () ;}} printf ("% d \ n", ans); for (int I = 1; I <= n; I ++) if (! Vis [I]) a [++ ans] = I; else vis [I] = 0; for (int I = 1; I <= ans; I ++) printf ("% d % c", a [I], I = ans? '\ N': '');} return 0 ;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.