开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第34天,点击查看活动详情
Good Bye 2022: 2023 is NEAR——B. Koxia and Permutation
Problem - B - Codeforces
Reve has two integers nn and kk.
Let pp be a permutation†† of length nn. Let cc be an array of length n−k+1n−k+1 such that
ci=max(pi,…,pi+k−1)+min(pi,…,pi+k−1).ci=max(pi,…,pi+k−1)+min(pi,…,pi+k−1).
Let the cost of the permutation pp be the maximum element of cc.
Koxia wants you to construct a permutation with the minimum possible cost.
†† A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, 2,3,1,5,4 is a permutation, but 1,2,2 is not a permutation (22 appears twice in the array), and 1,3,4 is also not a permutation (n=3n=3 but there is 44 in the array).
Input
Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤20001≤t≤2000) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105).
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.
Output
For each test case, output nn integers p1,p2,…,pnp1,p2,…,pn, which is a permutation with minimal cost. If there is more than one permutation with minimal cost, you may output any of them.
Example
input
3
5 3
5 1
6 6
output
5 1 2 3 4
1 2 3 4 5
3 2 4 1 6 5
问题解析
题目是说给你一个数字n,让你构造一个由1~n组成的数组,这个数组有一个长度为n-k+1的代价数组,代价数组的第i个元素是你数组的第i到第i+k个元素的最大值与最小值之和,现在让你输出一个数组,使得代价数组的最大值最小。
两个两个数交替输出即可,比如n为5,就是:5 1 4 2 3.
AC代码
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
#define endl '\n'
#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 998244353;
int a[N];
void solve()
{
int n, k;
cin >> n >> k;
if (n == k)
{
for (int i = 1; i <= n; i++)cout << i << " ";
cout << endl;
return;
}
int l = 1, r = n, idx = 1;
while (idx <= n)
{
a[idx++] = r--;
if (idx > n)break;
a[idx++] = l++;
}
for (int i = 1; i <= n; i++)cout << a[i] << " ";
cout << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
cin >> t;
while (t--)
{
solve();
}
return 0;
}