Description
有一个长度为 n n 的序列,以 fi f i 表示以 ai a i 结尾的最长严格上升子序列长度,先给出 fi f i 和 ai a i 的取值范围 [li,ri] [ l i , r i ] ,给出满足条件的序列 a1,...,an a 1 , . . . , a n ,保证有解
Input
第一行一整数 T T 表示用例组数,每组用例首先输入一整数表示序列长度,之后输入 n n 个整数,最后 n n 行每行输入两个整数表示 ai a i 的取值范围
(1≤n≤105,1≤fi≤n,0≤li≤ri≤2⋅109) ( 1 ≤ n ≤ 10 5 , 1 ≤ f i ≤ n , 0 ≤ l i ≤ r i ≤ 2 ⋅ 10 9 )
Output
输出满足条件的 a a 序列
Sample Input
4
6
1 2 3 2 4 3
0 5
2 4
3 3
1 2
3 5
1 5
5
1 2 1 3 1
100 200
200 300
200 400
400 500
100 500
7
1 2 3 1 1 4 2
0 3
0 3
0 3
0 3
0 3
0 3
0 3
2
1 1
1 2
2 3
Sample Output
1 2 3 2 5 3
200 300 200 500 200
0 1 2 0 0 3 1
2 2
Solution
若,说明 a1,...,ai−1 a 1 , . . . , a i − 1 均不小于 ai a i ,那么所有 fi=1 f i = 1 的位置的取值应该是一个不增的序列,贪心的让最后一个 fi=1 f i = 1 的位置取最小值 li l i ,之后从后往前把所有 fi=1 f i = 1 的位置都取最小值(两个限制:所给取值限制以及保证所有 fi=1 f i = 1 的位置取值序列不增的限制),这样一定对其他位置取值影响最小且合法
若 fi=x>1 f i = x > 1 ,说明存在 1≤j<i 1 ≤ j < i ,使得 fj=x−1 f j = x − 1 且 aj<ai a j < a i ,也即第 i i 个位置的取值要大于前个位置中 fj=x−1 f j = x − 1 的位置取值最小值,由于所有 fj=x−1 f j = x − 1 的位置取值不增,故第 i i 个位置的取值只要大于它前面第一个的位置的取值即可,同样的,所有 fi=x f i = x 的位置取值不增,从后往前让每个位置取最小值即可(三个限制:所给取值限制,保证所有 fi=x f i = x 的位置取值序列不增的限制,大于该位置前面第一个 fj=x−1 f j = x − 1 的位置取值的限制)
故对于每个位置 i i ,预处理表示第 i i 个位置前第一个满足的位置 j j ,把所有位置按取值升序排列,同样 f f <script type="math/tex" id="MathJax-Element-45">f</script>取值的位置按下标降序排列,以此序一个个按之前分析确定取值即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100005;
struct node
{
int l,r,f,id;
bool operator<(const node&b)const
{
if(f!=b.f)return f<b.f;
return id>b.id;
}
}a[maxn];
int T,n,ans[maxn],Next[maxn],pos[maxn];
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i].f),a[i].id=i;
for(int i=1;i<=n;i++)scanf("%d%d",&a[i].l,&a[i].r);
memset(Next,0,sizeof(Next));
for(int i=1;i<=n;i++)
{
pos[i]=Next[a[i].f-1];
Next[a[i].f]=i;
}
sort(a+1,a+n+1);
int L,F;
for(int i=1;i<=n;i++)
{
if(pos[a[i].id])a[i].l=max(a[i].l,ans[pos[a[i].id]]+1);
if(i==1||a[i].f!=a[i-1].f)
{
F=a[i].f,L=a[i].l;
ans[a[i].id]=L;
continue;
}
ans[a[i].id]=L=max(a[i].l,L);
}
for(int i=1;i<=n;i++)printf("%d%c",ans[i],i==n?'\n':' ');
}
return 0;
}