题目链接:codeforces round #275 (div. 2) c - diverse permutation
题意:一串排列1~n。求一个序列其中相邻两项差的绝对值的个数(指绝对值不同的个数)为k个。求序列、
思路:1~k+1。构造序列前段,之后直接输出剩下的数。前面的构造可以根据,两项差的绝对值为1~k构造。
AC代码:
#include <stdio.h>#include <string.h>int ans[200010];bool vis[100010];int n,mark;int iabs(int a){ if(a<0) return -a; return a;}int main(){ int i,cnt,k; while(scanf("%d%d",&n,&k)!=EOF) { int x,y; memset(vis,0,sizeof vis); ans[0]=1; x=1,y=k+1; cnt=k; for(i=1; i<=k; i++,cnt--) { int temp=ans[i-1]+cnt; if(temp>k+1) temp=ans[i-1]-cnt; else if(vis[temp]) temp=ans[i-1]-cnt; ans[i]=temp; vis[temp]=true; } for(i=k+1; i<n; i++) ans[i]=i+1; for(i=0; i<n-1; i++) printf("%d ",ans[i]); printf("%d\n",ans[i]); } return 0;}
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号