K Swap -- 归并排序
K Swap
https://atcoder.jp/contests/abc254/tasks/abc254_c
思路
Code
#include <bits/stdc++.h> #include <vector> #include <algorithm> #include <deque> #include <queue> #include <string> #include <set> using namespace std; /* https://atcoder.jp/contests/abc254/tasks/abc254_c */ vector<int> a(200004, 0); map<int, vector<int>> groups; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; /* group indexed from 0 to k-1 1 --> group 0 2 --> group 1 ... k --> group k-1 k+1 --> group 0 k+2 --> group 1 ... */ int group_index = (i - 1) % k; groups[group_index].push_back(a[i]); } for (int i = 0; i < k; i++) { vector<int> &one_group = groups[i]; sort(one_group.begin(), one_group.end()); // // for (int j = 0; j < one_group.size(); j++) { // cout << one_group[j] << " " ; // } // // cout << endl; } int prev = groups[0][0]; for (int i = 2; i <= n; i++) { int group_num = (i - 1) % k; int column = (i - 1) / k; int now = groups[group_num][column]; if (prev <= now) { prev = now; continue; } else { cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; }
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。