F. Greedy Sequence(The Preliminary Contest for ICPC Asia Nanjing 2019)

原题

You’re given a permutation a of length n ( 1 ≤ n ≤ 1 0 5 1\le n\le 10^5 1n105). For each i ∈ [ 1 , n ] i \in [1,n] i[1,n], construct a sequence s i s_i si​ by the following rules:
1. s i [ 1 ] = i s_i[1]=i si[1]=i
2.The length of s i s_i si is n, and for each j ∈ [ 2 , n ] , s i [ j ] ≤ s i [ j − 1 ] j \in [2, n], s_i[j] \le s_i[j-1] j[2,n],si[j]si[j1];
3.First, we must choose all the possible elements of s i s_i si​ from permutation a. If the index of s i [ j ] s_i[j] si[j] in permutation a is p o s [ j ] pos[j] pos[j], for each j ≥ 2 j \ge 2 j2, ∣ p o s [ j ] − p o s [ j − 1 ] ∣ ≤ k |pos[j]-pos[j-1]|\le k pos[j]pos[j1]k( 1 ≤ k ≤ 1 0 5 1 \le k \le 10^5 1k105). And for each s i s_i si​, every element of s i s_i si must occur in a at most once.
4.After we choose all possible elements for s i s_i si​, if the length of s i s_i si​ is smaller than nnn, the value of every undetermined element of s i s_i si​ is 0;
5.For each s i s_i si​, we must make its weight high enough.
Consider two sequences C=[c1,c2,…cn] and D = [ d 1 , d 2 , . . . , d n ] D=[d_1, d_2, ..., d_n] D=[d1,d2,...,dn], we say the weight of C is higher than that of D if and only if there exists an integer k such that 1 ≤ k ≤ n 1 \le k \le n 1kn, c i = d i c_i=d_i ci=di​ for all 1 ≤ i &lt; k 1 \le i &lt; k 1i<k, and c k &gt; d k c_k &gt; d_k ck>dk​.

If for each i ∈ [ 1 , n ] i \in [1,n] i[1,n], c i = d i c_i=d_i ci=di, the weight of C is equal to the weight of D.

For each i ∈ [ 1 , n ] i \in [1,n] i[1,n], print the number of non-zero elements of s i s_i si​ separated by a space.
It’s guaranteed that there is only one possible answer.

Input:
There are multiple test cases.
The first line contains one integer T ( 1 ≤ T ≤ 20 ) T(1 \le T \le 20) T(1T20), denoting the number of test cases.
Each test case contains two lines, the first line contains two integers n and k ( 1 ≤ n , k ≤ 1 0 5 1 \le n,k \le 10^5 1n,k105), the second line contains nnn distinct integers a 1 , a 2 , . . . , a n a_1, a_2, ..., a_n a1,a2,...,an( 1 ≤ a i ≤ n 1 \le a_i \le n 1ain) separated by a space, which is the permutation a.

Output
For each test case, print one line consists of n integers ∣ s 1 ∣ , ∣ s 2 ∣ , . . . , ∣ s n ∣ |s_1|, |s_2|, ..., |s_n| s1,s2,...,sn separated by a space. ∣ s i ∣ |s_i| si is the number of non-zero elements of sequence s i s_i si​.There is no space at the end of the line.

思路

分析知 ∣ s i ∣ |s_i| si i i i a i a_i ai周围的k个元素中 ≤ a i \le a_i ai的元素的最大一个有关,利用set lower_bound二分查找(STL 中的set使用红黑树实现,插入、查询、删除的时间复杂度为 O ( l o g n ) \mathcal{O}(log n) O(logn)

代码实现

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+3;
int t,n,k;
int a[MAX];
int pre[MAX],res[MAX];
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&k);
		set<int> s;
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		int L=0, R=min(k, n);
		for(int i=L;i<=R;i++){
			s.insert(a[i]);
		}
		for(int i=0;i<n;i++){
			if(L<i-k)s.erase(a[L++]);
			if(R>=i+k)s.insert(a[++R]);
			set<int>::iterator it=s.lower_bound(a[i]);
			if(it==s.begin())pre[a[i]]=0;
			else pre[a[i]]=*(--it);
		}
		res[0]=0;
		for(int i=1;i<=n;i++){
			res[i]=res[pre[i]]+1;
		}
		for(int i=1;i<=n;i++){
			printf("%d",res[i]);
		}
		printf("\n");
	}
}	


参考链接:
https://blog.csdn.net/weixin_43667611/article/details/100527679
https://blog.csdn.net/qq_41157137/article/details/100185548

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值