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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】