hdu5360| | Multi-school Joint 6th game 1008 Greedy

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=5360

Problem Descriptionthere is n Soda conveniently labeled by 1,2,... , N . Beta, their best friends, wants to invite some soda to go hiking. The i -th Soda would go hiking if the total number of soda that go hiking except him are no less than l i And no larger than r i . Beta would follow the rules below to invite soda one by one:
1. He selects a soda not invited before;
2. He tells soda the number of soda who agree to go hiking by now;
3. Soda'll agree or disagree according to the number he hears.

Note:beta'll always tell the truth and soda would agree if and only if the number he hears are no less than l i And no larger than r i , otherwise he'll disagree. Once Soda agrees to go hiking he won't regret even if the final total number fails to meet some soda ' s would.

Help Beta design a invitation order the number of soda who agree to go hiking is maximum.

Inputthere is multiple test cases. The first line of input contains an integer T , indicating the number of test cases. For each test case:

The first contains an integer n (1≤n≤5 ) The number of soda. The second line Constains n Integers l 1 , l 2 ,... .., l n . The third line Constains n Integers r 1 , r 2 ,... .., r n . (0≤ l i ≤ r i ≤N)
It is guaranteed, the total number of soda in the input doesn ' t exceed 1000000. The number of test cases in the input doesn ' t exceed 600.

Outputfor each test case, output the maximum number of soda. Then on the second line output a permutation of 1,2,... , N Denoting the invitation order. If There is multiple solutions, print any of them.

Sample Input
484 1 3 2 2 1 0 35 3 6 4 2 1 7 683 3 2 0 5 0 3 64 5 2 7 7 6 7 682 2 3 3 3 0 0 27 4 3 6 3 2 2 585 6 5 3 3 1 2 46 3 5

Sample Output
71 7 6 5 2 4 3 884 6 3 1 2 5 8 773 6 7 1 5 2 8 401 2 3 4 5 6 7 8

/**hdu5360| | Multi-school Joint 6th field 1008 greedy topic: XXX to invite n people to play, for the first person if the number of people who have been invited (Li,ri), he will go. What is the order of invitations to invite the most people? Problem-solving idea: It looks like a problem with the Asian race in Shanghai last year. My idea is that the n person is incremented by Li and then traversed from the go, and the currently invited X-Men, the minimum RI (set maintenance) is taken from all the li>=x. Complexity O (NLOGN) */#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream>#    include<set>using namespace Std;const int maxn=100005;struct note{int l,r,id;    BOOL Operator < (const note &other) const {return l<other.l;    }}a[maxn];int n,num[maxn],flag[maxn];set<pair<int,int> >st;int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);            for (int i=0;i<n;i++) {scanf ("%d", &AMP;A[I].L);        a[i].id=i+1;        } for (int i=0;i<n;i++) {scanf ("%d", &AMP;A[I].R);        } sort (a,a+n);        int x=0;        memset (flag,0,sizeof (flag));        for (int i=0;;)     {while (x>=a[i].l&&i<n) {           St.insert (Make_pair (a[i].r,a[i].id));            i++;            } while (((*st.begin ()). First<x) &&st.size () >0) st.erase (St.begin ());            if (St.size () ==0) break;            int cnt= (*st.begin ()). Second;            num[x++]=cnt;            Flag[cnt-1]=1;        St.erase (St.begin ());        } printf ("%d\n", X);            for (int i=0;i<n;i++) {if (!flag[i]) {num[x++]=i+1; }} for (int i=0;i<n;i++) {printf (i==n-1? ")        %d\n ":"%d ", num[i]); }} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdu5360| | Multi-school Joint 6th game 1008 Greedy

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.