Special Permutation CodeForces - 1352G(构造)

Rhoda ·
更新时间:2024-09-20
· 625 次阅读

思路:一开始想复杂了,直接搞的图论,TLE了。后来发现其实可以直接构造。前四个我们可以构造出2 3 1 4 的形式,如果n>=4的话,那么可以左右来回放置,这样就可以构造成功。只有n<=3的时候才不能构造成功。
代码如下:

#include using namespace std; int n; inline string fcs(int i) { string tt=""; while(i) { tt=(char)(i%10+'0')+tt; i/=10; } tt+=" "; return tt; } int main() { int T; cin >> T; while (T--) { int n; cin >> n; if (n <= 3) puts("-1"); else if (n == 4) puts("2 4 1 3"); else { string ans; ans+="2 4 1 3 "; int flag=1; for(int i=5;i<=n;i++) { if(flag) ans+=fcs(i); else ans=fcs(i)+ans; flag^=1; } cout<<ans<<endl; } } return 0; }

努力加油a啊,(o)/~

starlet_kiss 原创文章 723获赞 192访问量 7万+ 关注 私信 展开阅读全文
作者:starlet_kiss



permutation CodeForces

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章