思路:预处理一下点的顺序,然后询问的时候从Tag左边右边分别找k个点,只后双指针扫一遍就能得到最近的k个序列
#include <bits/stdc++.h>
using namespace std;
struct node{
#define MAXN 1005
int pos;
int id;
bool operator < (const node& A) const {
return pos < A.pos;
}
}tab[MAXN];
int index[MAXN], n, q; //n个点 q次询问
vector<int> ans;
bool find(vector<int>& T, int a, int k) {
T.clear();
int r = index[a]+k < n ? index[a]+k : n-1;
int l = index[a]-k < 0 ? 0 : index[a]-k;
int winSize = r - l + 1;
while (winSize > k + 1) {
if (tab[index[a]].pos - tab[l].pos > tab[r].pos - tab[index[a]].pos) l++;
else r--;
winSize--;
}
if (winSize <= k) return false;
while (l <= r) {
if (l != index[a]) T.push_back(tab[l].pos);
l++;
}
return true;
}
int main() {
int a, k;
scanf("%d", &n);
for (int i = 0; i < n; i++) { scanf("%d", &tab[i].pos); tab[i].id=i; } //read
sort(tab, tab+n);
for (int i = 0; i < n; i++) index[tab[i].id]=i; //hash
scanf("%d", &q);
while (q--) {
scanf("%d%d", &a, &k);
if (!find(ans, a-1, k))
printf("No\n");
else {
printf("Yes\n");
for (int i = 0; i < ans.size(); i++)
printf("%d%c", ans[i], " \n"[i+1==ans.size()]);
}
printf("\n");
}
return 0;
}
/*
input:
10
1 2 4 8 16 32 64 128 256 512
5
5 3
2 2
3 2
4 3
10 6
output:
2 4 8
1 4
1 2
1 2 4
8 16 32 64 128 256
input:
10
32 64 128 256 512 1 2 4 8 16
5
5 3
2 2
3 2
4 3
10 6
output:
64 128 256
16 32
32 64
32 64 128
1 2 4 8 32 64
*/