## 【HDU-6235】Permutation

【HDU-6235】Permutation

Solution:

Special Judge，想出来如何构造后，会感叹竟然有这么简单的题，先在奇数位置上从1开始每次递增1排过去，奇数位置排完，接着排偶数位置，这样任意相间排列的数之差都是1，显然1是任何数的因数。

#include <iostream>

using namespace std;

int main(void) {
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int bound = (n + 1) / 2;
for (int i = 1; i <= bound; i++) {
if (i - 1) putchar(' ');
printf("%d", i);
putchar(' ');
if (i + bound <= n) printf("%d", i + bound);
}
putchar('\n');
}
return 0;
}


## 【HDU-6231】K-th Number【尺取法+二分】

【HDU-6231】K-th Number【尺取法+二分】

Solution:

#include <iostream>

using namespace std;

typedef long long LL;

const int LIM = 1e5 + 10;
int arr[LIM];
int n, k;
LL m;

bool deter(int v) {
int num = 0, lp = 0;
LL cnt = 0;
for (int i = 0; i < n; i++) {
if (arr[i] >= v) num++;
if (num == k) {
cnt += n - i;
while (arr[lp] < v) {
lp++;
cnt += n - i;
}
num--;
lp++;
}
}
return cnt >= m;
}

int main(void) {
int t;
scanf("%d", &t);
while (t--) {
int l = 1, r = 0;
scanf("%d%d%lld", &n, &k, &m);
/* input the data */
for (int i = 0; i < n; i++) {
scanf("%d", arr + i);
if (arr[i] > r) r = arr[i];
}
r++;
while (l < r) {
int mid = (l + r) >> 1;
if (deter(mid)) l = mid + 1;
else r = mid;
}
printf("%d\n", l - 1);
}
return 0;
}