E. Klever Permutation
链接:https://codeforces.com/problemset/problem/1927/E
思路:观察,可知每隔k个数据就是+1/-1,且间隔而分,思路如下:
然后按顺序打表就行
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>
typedef long long ll;
using namespace std;
const int N = 2e5+10;
ll a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t; cin >> t;
while (t--)
{
int ptr = 1;
int n, k; cin >> n >> k;
for (int i = 1; i <= k; i += 2)//注意这个i+=2
{
for (int j = 0; i + j * k <= n; j++)
{
a[i + j * k] = ptr;
ptr++;
}
}
ptr = n;//从n开始倒着标
for (int i = k; i > 0; i -= 2)//i-=2
{
for (int j = 0; i + j * k <= n; j++)
{
a[i + j * k] = ptr;
ptr--;
}
}
for (int i = 1; i <= n; i++)cout << a[i] << ' ';
cout << endl;
}
return 0;
}