Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: mickyta1
- TASK: sort3
- LANG: C++
- */
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1000 + 5;
- deque<int> ones;
- vector<int> lft, sortedLft;
- int arr[N], sorted[N];
- int main(){
- freopen("sort3.in", "r", stdin);
- freopen("sort3.out", "w", stdout);
- int n;
- scanf("%d", &n);
- for(int i = 1; i <= n; ++i){
- scanf("%d", &arr[i]);
- sorted[i] = arr[i];
- }
- sort(sorted + 1, sorted + n + 1);
- for(int i = 1; i <= n; ++i){
- if(arr[i] != sorted[i]){
- lft.push_back(arr[i]);
- sortedLft.push_back(arr[i]);
- }
- }
- sort(sortedLft.begin(), sortedLft.end());
- int cnt = 0;
- for(int i = 0; i < lft.size(); ++i){
- if(lft[i] == 1){
- ones.push_back(i);
- }
- }
- int i;
- for(i = 0; i < lft.size() && sortedLft[i] == 1; ++i){
- if(lft[i] == 2){
- swap(lft[i], lft[ones.front()]);
- ones.pop_front();
- } else if(lft[i] == 3){
- swap(lft[i], lft[ones.back()]);
- ones.pop_back();
- }
- ++cnt;
- }
- for(; i < lft.size() && sortedLft[i] == 2; ++i){
- if(lft[i] == 3){
- ++cnt;
- }
- }
- cout << cnt << '\n';
- fclose(stdin);
- fclose(stdout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement