Codeforces Round #541 (Div. 2) C.Birthday
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #541 (Div. 2) C.Birthday
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
鏈接:https://codeforces.com/contest/1131/problem/C
題意:
求給的n個數,相鄰差值最小的排列方式。1-n相鄰。
思路:
sort后隔一個取一個,取到底后再反著取剩下的。
代碼:
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 100 + 10; int a[MAXN];int main() {int n;cin >> n;for (int i = 1;i <= n;i++)cin >> a[i];sort(a + 1, a + 1 + n);vector<int> res;vector<int> tmp;for (int i = 1;i <= n;i += 2)res.push_back(a[i]);for (int i = 2;i <= n;i += 2)tmp.push_back(a[i]);reverse(tmp.begin(), tmp.end());for (auto x : tmp)res.push_back(x);for (auto x : res)cout << x << ' ';return 0; }
轉載于:https://www.cnblogs.com/YDDDD/p/10425073.html
總結
以上是生活随笔為你收集整理的Codeforces Round #541 (Div. 2) C.Birthday的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在VUE中利用MQTT协议实现即时通讯
- 下一篇: 云计算时代,数据中心架构三层到大二层的演