題意:
請邀請n個小瘋子去瘋,ta們名字分別爲1…n;小瘋子脾氣都很怪,每個小瘋子都需要你滿足條件:只要在邀請ta之前,可以保證至少有 l 個人並且頂多就 r 個,ta纔去。(你後面再邀請到全體中國人去都和他的決定沒關係,小瘋子版口香糖 嘻嘻!)
要求給出一起瘋的最大人數,並且給出邀請的順序。思路:
先對每個小瘋子的下限做升序,然後再用優先隊列(stl中 priority_queue)對 這些小瘋子取最小上限用作隊列的比較。(前提是要滿足上下限條件哈!)
好了, 去邀請小瘋子們吧!複雜度:
時間複雜度:O(nlog(n))
空間複雜度:O(n)邀請小瘋子代碼
/* ***********************************************
Author :Ilovezilian
Created Time :2015/8/7 19:52:48
File Name :1008_1.cpp
************************************************ */
#include <bits/stdc++.h>
#define fi(i,n1,n) for(int i = n1; i <= n; i ++)
#define fd(i,n1,n2) for(int i = n1; i >= n2; i --)
#define INF 0x7fffffff
#define ll long long
using namespace std;
const int N = 100100, mod = 1e9+7;
int n, ans;
struct nod {
int l, r, o;
}p[N];
bool cmp(nod a, nod b) {return a.l < b.l;}
struct cmp1 { bool operator () (nod a, nod b){return a.r > b.r;}};
priority_queue<nod, vector<nod>, cmp1> q;
void judge()
{
int i = 0, m = 0;
ans = 0;
for(; i < n; i ++)
{
while(m < n && p[m].l <= i) q.push(p[m]), m ++;
while(!q.empty() && q.top().r < i) q.pop();
if(q.empty())
{
while(!q.empty()) q.pop();
return;
}
if(!q.empty()) ans ++, q.pop();
}
while(!q.empty()) q.pop();
return;
}
void solve()
{
scanf("%d", &n);
fi(i,0,n-1) p[i].o = i + 1;
fi(i,0,n-1) scanf("%d", &p[i].l);
fi(i,0,n-1) scanf("%d", &p[i].r);
sort(p, p + n, cmp);
judge();
printf("%d\n", ans);
if(ans == 0)
{
fi(i,0, n - 1) printf("%d%c", p[i].o, i != n - 1 ? ' ' : '\n');
return;
}
int i = 0, m = 0, cnt = 1;
for(; i < n; i ++)
{
while(m < n && p[m].l <= i) q.push(p[m]), m ++;
while(!q.empty() && q.top().r < i) printf("%d%c" ,q.top().o, cnt ++ != n ? ' ' : '\n'), q.pop();
if(!q.empty())
{
printf("%d%c" ,q.top().o, cnt ++ != n ? ' ' : '\n');
q.pop();
}
}
while(!q.empty())
{
printf("%d%c" ,q.top().o, cnt ++ != n ? ' ' : '\n');
q.pop();
}
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
int cas;
scanf("%d", &cas);
while(cas --) solve();
return 0;
}