题意: 现在要邀请N个人,每个人接受邀请的条件为,在邀请他时,已经接受邀请的人数需要在 [Li,Ri] 之间,求一个邀请顺序使得最后接受邀请的人数最多
分析:我们肯定需要按L排序,然后每次选择L小于等于当前受邀人数的人加入一个按R从小到大排序的优先队列,然后从这个队列中选则R最小的发出邀请,并标记为已邀请,受邀人数+1。最后将没有受邀的人排在最后即可
AC代码:
/*************************************************************************
> File Name: test.cpp
> Author: Akira
> Mail: qaq.febr2.qaq@gmail.com
************************************************************************/
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <set>
#include <list>
#include <ctime>
#include <climits>
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
using namespace std;
#define MaxN 100001
#define MaxM MaxN*10
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
const int mod = 1E9+7;
#define bug cout<<88888888<<endl;
int N;
struct Node
{
int L,R,id;
bool operator<(const Node & a) const
{
if(a.L == L) return a.R>R;
else return a.L>L;
}
}node[MaxN];
struct Right
{
int x;
int pos;
Right(int a = 0, int b = 0){x = a, pos = b;}
bool operator<(const Right & a) const
{
return a.x < x;
}
};
priority_queue<Right> Q;
int vis[MaxN];
int main()
{
//std::ios::sync_with_stdio(false);
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
for(int i=1;i<=N;i++)scanf("%d", &node[i].L);
for(int i=1;i<=N;i++)
{
scanf("%d", &node[i].R);
node[i].id = i;
}
sort(node+1,node+1+N);
CLR(vis);
int now = 0;
int _begin = 1;
vector<int>ans;
while(now<N)
{
for(;_begin<=N;_begin++)
{
if(node[_begin].L<=now)
{
Q.push(Right(node[_begin].R,node[_begin].id));
}
else break;
}
if(!Q.empty())
{
Right tmp = Q.top();Q.pop();
//cout <<now <<"--"<< tmp.x << endl;
if(tmp.x>=now)
{
//bug;
vis[tmp.pos] = 1;
ans.push_back(tmp.pos);
now++;
}
}
else break;
}
printf("%d\n", ans.size());
for(int i=1;i<=N;i++)
{
if(!vis[i]) ans.push_back(i);
}
for(int i=0;i<ans.size();i++)
printf("%d%c", ans[i], i==ans.size()-1 ? '\n' : ' ');
}
//system("pause");
}