Greedy Sequence
限制5000 ms 256 MB
You're given a permutation a of length n (1 ≤ n ≤ 105). For each i ∈ [1, n], construct a sequence si by the following rules:
1. si[1] = i;
2. The length of si is n, and for each j ∈ [2, n], si[j] ≤ si[j − 1];
3. First, we must choose all the possible elements of si from permutation a. If the indeof si[j] in permutation a is pos[j], for each j ≥ 2, ∣pos[j] − pos[j − 1]∣ ≤ k ( 1 ≤ k ≤ 105). And for each si, every element of si must occur in a at most once.
4. After we choose all possible elements for si, if the length of si is smaller than n, thevalue of every undetermined element of si is 0;
5. For each si, we must make its weight high enough. Consider two sequences C = [c1, c2, ...cn] and D = [d1, d2, ..., dn], we say the weigof C is higher than that of D if and only if there exists an integer k such that 1 ≤ k ≤ nci = di for all 1 ≤ i < k, and ck > dk . If for each i ∈ [1, n], ci = di, the weight of C is equal to the weight of D. For each i ∈ [1, n], print the number of non-zero elements of si separated by a space.It's guaranteed that there is only one possible answer.
Input
There are multiple test cases.The fifirst line contains one integer T(1 ≤ T ≤ 20), denoting the number of test cases.Each test case contains two lines, the fifirst line contains two integers n and k ( 1 ≤ n, k ≤ 105), the second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n)separated by a space, which is the permutation a.
Output
For each test case, print one line consists of n integers ∣s1∣, ∣s2∣, ..., ∣sn∣ separated by aspace. ∣si∣ is the number of non-zero elements of sequence si. There is no space at the end of the line.
Sample Input 1
2
3 1
3 2 1
7 2
3 1 4 6 2 5 7
Sample Output 1
1 2 3
1 1 2 3 2 3 3
题目大意:给你一个序列a,你可以从其中选取元素,然后要构建n个串,每个串的长度为n,构造的si串要满足以下条件,
1. si[1]=i . 2. si[j]<si[j-1] 3. |pos[j] -pos[j-1]|<=k 并且每个a中的元素只能用一次 ,如果不能从a中找到了,其余数字全部填上0
4. 两个串大小的定义时 前k项相等的前提(k和前面不是一个),Ck>Dk,则C大于D
要求输出n个串的非零字符数。
题目思路:每个串都可以有前面的传递推过来,要求每个串的值是最大的(很重要),当时做的时候以为是让字符数最多,结果一直模拟不出样例,因为忽略了最后一个条件(泪崩),因为每个串的首位一定是最大的,然后从第i-1开始找符合条件的,由它递推过来就ok
#include<bits/stdc++.h>
using namespace std;
const int maxn =1e5+10;
int pos[maxn];
int s[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
pos[x]=i;
}
for(int i=1;i<=n;i++)
s[i]=1;
for(int i=1;i<=n;i++)
{
for(int j=i-1;j>=1;j--)
if(abs(pos[i]-pos[j])<=k)
{
s[i]+=s[j];
break;
}
}
for(int i=1;i<=n;i++)
{
if(i==n)
printf("%d\n",s[i]);
else
printf("%d ",s[i]);
}
}
return 0;
}