codeforces B. Trouble Sort

在這裏插入圖片描述

題目

題意:

給你一個a,ba,b序列,現在你要將aa序列通過交換的方式排序,但是隻有bb不同的兩個才能進行交換,問你最後能不能有序。

思路:

我們可以發現有兩種情況:

  • 如果剛開始的時候就是有序的,那麼你不用任何操作就可以有序。
  • 然後就是看看是不是全部都是一樣的因爲只要有不一樣,我們就可以將不一樣的兩個當作媒介,最後肯定能夠交換成有序。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
#include <cctype>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {
    char c;
    int sgn;
    if (c = getchar(), c == EOF) return ;
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1:1;
    ret = (c == '-') ? 0:(c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return ;
}
int xxxxxxxxx = 1;
inline void outi(int x) {if (x > 9) outi(x / 10);putchar(x % 10 + '0');}
inline void outl(ll x) {if (x > 9) outl(x / 10);putchar(x % 10 + '0');}
inline void debug(ll x) {cout << xxxxxxxxx++ << " " << x << endl;}
inline void debugs(string s) {cout << s << endl;}
const int maxn = 510;
int a[maxn], b[maxn];
int main() {
    int t; read(t); while (t--) {
        int n; read(n);
        bool flag = true, flagn = false;
        for (int i = 1; i <= n; i++) {
            read(a[i]);
            if (a[i] < a[i - 1]) flag = false;
        }
        int cnt1 = 0, cnt0 = 0;
        for (int i = 1; i <= n; i++) {
            read(b[i]);
            if (b[i] == 0) cnt0++;
            else cnt1++;
            if (cnt1 && cnt0) flagn = true;
        }
        if (flagn || flag) printf("Yes\n");
        else printf("No\n");
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章