Hdu5360Hiking greedy, hdu5360hiking greedy

Source: Internet
Author: User

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.

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.