HDU 5360 Hiking (贪心+优先队列)

题目:

Hiking

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1681    Accepted Submission(s): 863
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 than li 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 than li 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 integer T, 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 of 1,2,,n denoting the invitation order. If there are multiple solutions, print any of them.
 

Sample Input
4 8 4 1 3 2 2 1 0 3 5 3 6 4 2 1 7 6 8 3 3 2 0 5 0 3 6 4 5 2 7 7 6 7 6 8 2 2 3 3 3 0 0 2 7 4 3 6 3 2 2 5 8 5 6 5 3 3 1 2 4 6 7 7 6 5 4 3 5
 

Sample Output
7 1 7 6 5 2 4 3 8 8 4 6 3 1 2 5 8 7 7 3 6 7 1 5 2 8 4 0 1 2 3 4 5 6 7 8
 


思路:

       贪心的策略很容易想到,在所有满足条件的soda里,优先选择r小的那个。所以,要按照 l 对所有的soda进行排序,然后将 l 符合条件的soda放进优先队列,这个优先队列是按照 r 排序的,然后再优先队列中邀请每个soda。

代码:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 1000000007
#define mem(a) memset(a,0,sizeof(a))

using namespace std;
const int maxn = 100000 + 5 , inf = 0x3f3f3f;

struct node{
    int l,r,p;
    //重载<运算符 用于优先队列的排序
    bool operator < (const node & a)const {
        return r > a.r ;
    }
};
node sodas[maxn];
vector<int>pos;//储存每次邀请的位置
//用于sort的排序
bool cmp(const node & a , const node & b ){
    if(b.l==a.l) return a.r<b.r;
    return a.l < b.l ;
}

int main(){
    int kase;
    scanf("%d",&kase);
    while(kase--){
        int n,cnt = 0;
        pos.clear();
        priority_queue<node,vector<node> >q;
        scanf("%d",&n);
        for(int i = 0 ; i < n ; i ++ ) scanf("%d",&sodas[i].l);
        for(int i = 0 ; i < n ; i ++ ) scanf("%d",&sodas[i].r);
        for(int i = 0 ; i < n ; i ++ ) sodas[i].p = i + 1;
        sort(sodas,sodas+n,cmp);//对soda按照 l 进行排序
        int i = 0;
        while(1){
            //i不断右移 找到符合条件的soda放入优先队列
            for(; i < n ; i ++ ){
                if(sodas[i].l<=cnt){
                    q.push(sodas[i]);
                }
                else break;
            }
            //对于补符合条件的soda 邀请了他们 但是他们并没有接受
            //直到找到一个符合条件的soda
            while(!q.empty()&&q.top().r<cnt){
                pos.push_back(q.top().p);
                q.pop();
            }
            //如果队列中没有符合条件的soda 退出
            if(q.size()==0) break;
            //如果有 就邀请他
            else{
                cnt++;
                pos.push_back(q.top().p);
                q.pop();
            }
        }
        //如何 i 没有到达 n 说明从 i 到 n-1 的soda都不符合条件 但是还需要邀请他们
        while(i<n){
            pos.push_back(sodas[i].p);
            i++;
        }
        //output
        printf("%d\n",cnt);
        for(int j = 0 ; j < pos.size() ; j ++ ){
            if(j) printf(" ");
            printf("%d",pos[j]);
        }
        printf("\n");
    }
}


posted @ 2018-05-02 23:08  dslybyme7  阅读(184)  评论(0编辑  收藏  举报