2015 Multi-University Training Contest 6_1008_Hiking(贪心)

来源:互联网 发布:蓝光主板端口说明 编辑:程序博客网 时间:2024/06/13 20:24

Hiking

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 730    Accepted Submission(s): 385
Special Judge


Problem Description
There are n soda conveniently labeled by 1,2,,n. beta, their best friends, wants to invite some soda to go hiking. The i-th soda will go hiking if the total number of soda that go hiking except him is no less thanli and no larger than ri. beta will 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 will agree or disagree according to the number he hears.

Note: beta will always tell the truth and soda will agree if and only if the number he hears is no less thanli and no larger than ri, otherwise he will disagree. Once soda agrees to go hiking he will not regret even if the final total number fails to meet some soda's will.

Help beta design an invitation order that the number of soda who agree to go hiking is maximum.
 

Input
There are multiple test cases. The first line of input contains an integerT, indicating the number of test cases. For each test case:

The first contains an integer n(1n105), the number of soda. The second line constains n integers l1,l2,,ln. The third line constains n integers r1,r2,,rn.(0lirin)
It is guaranteed that the total number of soda in the input doesn't exceed 1000000. The number of test cases in the input doesn't exceed 600.
 

Output
For each test case, output the maximum number of soda. Then in the second line output a permutation of1,2,,n denoting the invitation order. If there are 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 7 7 6 5 4 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
 
题意:在满足条件的情况下在n个人里选出尽可能多的人数。规则如下,当你对第i个人进行选择时,假设之前已选的总人数为sum,那么要想让它同意入队,必须满足l[i]<=sum&&sum<=r[i]。每个人所对应的区间已给出。

分析:贪心。对于满足当前条件的左区间,每次找其中最小的右区间即可。当然,每次只找一次。先以区间的左边界升序排序,然后对于右边界满足条件的人抛入优先队列(右边界小优先),直到不满足条件,则从队列里抛出一个人,那么这个人就是现在要选的人。一直循环下去。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5360
代码清单:
#include<set>#include<map>#include<cmath>#include<queue>#include<stack>#include<ctime>#include<cctype>#include<string>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const ll mod = 1e9 + 7;const int maxn = 1e5 + 5;struct People{    int l,r,id;    People(){}    People(int l,int r,int id){        this -> l = l;        this -> r = r;        this -> id = id;    }    friend bool operator<(People a,People b){        return a.r>b.r;    }}people[maxn];bool cmp(People a,People b){return a.l<b.l;}int T,n;int ans[maxn];bool vis[maxn];int main(){    scanf("%d",&T);    while(T--){        scanf("%d",&n);        for(int i=0;i<n;i++)            scanf("%d",&people[i].l);        for(int i=0;i<n;i++) {            scanf("%d",&people[i].r);            people[i].id = i;        }        sort(people,people+n,cmp);        priority_queue<People> q;        while(!q.empty()) q.pop();        memset(vis,false,sizeof(vis));        int ret=0;        int p = 0;        while (p < n) {            if (ret >= people[p].l) {                q.push(people[p++]);            }            else {                if (q.empty())                    break;                People t = q.top();                q.pop();                if (ret <= t.r) {                    ans[ret++] = t.id;                    vis[t.id] = true;                }            }        }        while (!q.empty()) {            People t = q.top();            q.pop();            if (ret <= t.r) {                ans[ret++] = t.id;                vis[t.id] = true;            }        }        printf("%d\n",ret);        bool fir = false;        for(int i=0;i<ret;i++){            if (!fir) {                printf("%d", ans[i]+1);                fir = true;            }            else                printf(" %d", ans[i] + 1);        }        for(int i=0;i<n;i++){            if(!vis[i]){                if (!fir) {                    printf("%d", i + 1);                    fir = true;                }                else                    printf(" %d", i + 1);            }        }        printf("\n");    }return 0;}


0 0
原创粉丝点击