1 条题解

  • 0
    @ 2025-10-19 16:39:11

    经过思考发现,k>=2时总能实现,选择长度位2的区间进行反转,就是互换这两个数。和冒泡排序一样,总能使其非递减顺序排序

    然而当k == 1时,只有数组初始时非递减,才能满足题意。

    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    void solve(){
        int n,k; cin >>  n >> k;
        int a[n+10] = {0};
        for(int i = 1; i <= n; i++){
            cin >> a[i];
        }
        if(k==1){
            bool ok = false;
            for(int i = 2; i <= n; i++){
                if(a[i] != a[i - 1]){
                    ok = true;
                    break;
                }
            }
            if(ok == 0){
                cout << "YES" << endl;
            }else{
                cout << "NO" << endl;
            }
        }else{
            cout << "YES" << endl;
        }
    }
    signed main(){
        ios::sync_with_stdio(0);
        cin.tie(0),cout.tie(0);
        int T=1;
        cin >> T; 
        while(T--){
            solve();
            // cout << fixed << setprecision(6) << n << '\n';
        }
        return 0;
    }
    
    • @ 2025-10-19 16:48:56

      C++实现

      #include <bits/stdc++.h>
      using namespace std;
      #define int long long
      void solve(){
          int n,k; cin >>  n >> k;
          vector<int> a(n+1);
          for(int i = 1; i <= n; i++){
              cin >> a[i];
          }
          if(k==1){
              if(is_sorted(a.begin()+1,a.end())){
                  cout << "YES" << endl;
              }else{
                  cout << "NO" << endl;
              }
          }else{
              cout << "YES" << endl;
          }
      }
      signed main(){
          ios::sync_with_stdio(0);
          cin.tie(0),cout.tie(0);
          int T=1;
          cin >> T; 
          while(T--){
              solve();
              // cout << fixed << setprecision(6) << n << '\n';
          }
          return 0;
      }
      
  • 1

信息

ID
1150
时间
1000ms
内存
256MiB
难度
7
标签
递交数
57
已通过
12
上传者