博客
关于我
C. Crazy Diamond(思维+构造)
阅读量:238 次
发布时间:2019-03-01

本文共 2346 字,大约阅读时间需要 7 分钟。

    


思路:数字1和n在排序过程中具有特殊性,常被用作中转站进行值的交换。

对于小于等于n/2的数值,处理方式如下:首先将其与n交换位置,再将交换后的数值转移到1的位置。对于大于n/2+1的数值,处理方式与上述相似。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout << "#a# " << a << endl; using namespace std;const int maxn = "3e5+1000";typedef long long ll;typedef pair
P;inline LL read() { LL x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; }LL a[maxn], pos[maxn];vector

ans;void op(LL x, LL y) { ans.push_back({x, y}); swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); }int main(void) { cin.tie(0); std::ios::sync_with_stdio(false); LL n; cin >> n; for (LL i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } for (LL i = 2; i <= n/2; i++) { if (pos[i] <= n/2) { op(pos[i], n); op(n, i); } else { op(pos[i], 1); op(1, n); op(n, i); } } for (LL i = n/2+1; i <= n; i++) { if (pos[i] > n/2) { op(pos[i], n); op(n, i); } else { op(pos[i], 1); op(1, n); op(n, i); } } <#include

经过优化后的版本:

    


数字1和n在排序过程中具有特殊性,常被用作中转站进行值的交换。

对于小于等于n/2的数值,处理方式如下:首先将其与n交换位置,再将交换后的数值转移到1的位置。对于大于n/2+1的数值,处理方式与上述相似。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#define debug(a) cout << "debug: " << a << endl; using namespace std;const int maxn = 300000 + 1000;typedef long long ll;typedef pair
P;inline ll read() { ll x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; }ll a[maxn], pos[maxn];vector

ans;void op(ll x, ll y) { ans.push_back({x, y}); swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); }int main(void) { cin.tie(0); std::ios::sync_with_stdio(false); ll n; cin >> n; for (ll i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; } for (ll i = 2; i <= n/2; i++) { if (pos[i] <= n/2) { op(pos[i], n); op(n, i); } else { op(pos[i], 1); op(1, n); op(n, i); } } for (ll i = n/2+1; i <= n; i++) { if (pos[i] > n/2) { op(pos[i], n); op(n, i); } else { op(pos[i], 1); op(1, n); op(n, i); } }

转载地址:http://cict.baihongyu.com/

你可能感兴趣的文章
Objective-C实现KNN算法(附完整源码)
查看>>
Objective-C实现KNN算法(附完整源码)
查看>>
Objective-C实现knuth morris pratt(KMP)算法(附完整源码)
查看>>
Objective-C实现knuth-morris-pratt(KMP)算法(附完整源码)
查看>>
Objective-C实现Koch snowflake科赫雪花曲线算法(附完整源码)
查看>>
Objective-C实现koch snowflake科赫雪花算法(附完整源码)
查看>>
Objective-C实现KPCA(附完整源码)
查看>>
Objective-C实现KruskalMST最小生成树的算法(附完整源码)
查看>>
Objective-C实现kruskal克鲁斯卡尔算法(附完整源码)
查看>>
Objective-C实现kth order statistick阶统计量算法(附完整源码)
查看>>
Objective-C实现lamberts ellipsoidal distance朗伯椭球距离算法(附完整源码)
查看>>
Objective-C实现largest AdjacentNumber最大相邻数算法 (附完整源码)
查看>>
Objective-C实现largest subarray sum最大子数组和算法(附完整源码)
查看>>
Objective-C实现largestPrime最大素数的算法 (附完整源码)
查看>>
Objective-C实现lazy segment tree惰性段树算法(附完整源码)
查看>>
Objective-C实现LBP特征提取(附完整源码)
查看>>
Objective-C实现LDPC码(附完整源码)
查看>>
Objective-C实现least common multiple最小公倍数算法(附完整源码)
查看>>
Objective-C实现Lempel-Ziv压缩算法(附完整源码)
查看>>
Objective-C实现Length conversion长度转换算法(附完整源码)
查看>>