HDU 5360 Hiking(STL优先队列)

Hiking

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0
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
 

/***********************************************************************************/

题意:给定n个人,现在要邀请这些人去远足,但每个人同意邀请的条件是当前已经同意去远足的人数c必须满足c>=l[i]&&c<=r[i](l[i]、r[i]为第i个人同意去远足的条件界限,分别表示要求当前已经同意去远足的人数至少l[i]个人,至多r[i]个人)问你邀请的顺序是什么才能使尽可能多的人去远足,若有多个最优解,输出任意的一个。

举个例子(样例1)

刚开始,去远足的人数为0,那么第7个人是符合条件的,因为l[7]=0,表示当前去远足的人至少已经有0个,他才会同意一起去远足,所以第7个人会去远足;接着,现在已经有一个人同意去远足了(即第7个人),此时第2个人和第6个人都是满足条件的,而你先邀请哪个人便是此题最优解的策略。

放上出题人的题解报告

HDU 5360 Hiking(STL优先队列)_第1张图片

解题思路:首先将每个人按照l[i]从小到大排序,然后按照人数递增一个个进行判断,比如当人数c=0时,将所有l[i]=0的放进优先队列,取r最小的且>=c的数即为当前要邀请的人的序号,然后c自增为1,再把l[i]=1的人放进队列,与之前的人一起判断,取满足条件的具有最小的r的人的序号即为下一个所求的人,具体看代码

#pragma comment (linker,"/stack:102400000,102400000")
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const double PI=acos(-1.0);
#define able puts("0000000000000000000000")
template T_T f_max(T_T a,T_T b)
{
    return a>b?a:b;
}
template T_T f_min(T_T a,T_T b)
{
    return a T_T f_abs(T_T a)
{
    return a>0?a:-a;
}
template T_T gcd(T_T a,T_T b)
{
    return b?gcd(b,a%b):a;
}
template void swap(T_T *a,T_T *b)
{
    T_T c;
    c=a;
    a=b;
    b=c;
}
const int N = 100005;
const int inf = 1000000000;
const int mod = 1000000007;
struct node
{
    int l,r,id;
    node(){}
    node(int _l,int _r,int _id):l(_l),r(_r),id(_id){}
    bool operator < (const node &a) const
    {
       return r>a.r;//最小值优先
    }
}s[N];
int v[N];
bool cmp(node x,node y)
{
    if(x.l!=y.l)
        return x.l q;
        c=0;
        if(s[0].l!=0)
        {
            printf("%d\n",c);
            for(i=1;i<=n;i++)
                printf("%d%c",i,i==n?'\n':' ');
            continue;
        }
        q.push(s[0]);i=1;k1=0;k2=n-1;
        while(!q.empty())
        {
            for(;i=c)
                {//printf("q:%d %d\n",q.top().id,q.top().r);
                    v[k1++]=q.top().id;
                    c++;
                    q.pop();
                    break;
                }
                else
                    v[k2--]=q.top().id;
                q.pop();
            }
            for(;i
菜鸟成长记

你可能感兴趣的:(HDU,OJ,多校练习赛,Training,Contest)