1545A - AquaMoon and Strange Sort - CodeForces Solution


sortings *1500

Please click on ads to support us..

Python Code:

T=int(input())
for i in range(T):
    n=int(input())
    arr=list(map(int,input().split()))
    l=[]
    r=[]
    for i in range(n):
        if i%2==0:
            l.append(arr[i])
        else:
            r.append(arr[i])
    l.sort()
    r.sort()
    arr.sort()
    z=[]
    for i in range(n):
        if i%2==0:
            z.append(l.pop(0))
        else:
            z.append(r.pop(0))
    if z==arr:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>
using namespace std;

// clang-format off
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define ll long long
#define int long long
#define ld long double
#define pb push_back
#define vi vector<long long>
#define vll vector<long long>
#define rep(i,a,b) for (ll i=a;i<b;i++)
#define repa(container) for(auto& x:container)
#define repn(i,a,b) for (ll i=a;i>=b;i--)
#define all(x) x.begin(), x.end()
#define mod 1000000007 // 10^9+7
#define epsilon 0.0000000000001

// debug templates 
#define debug(x)       cerr<< #x <<" : "<< x << endl;
#define debuga(A,N)    cerr<< #A <<" : [ ";for(int i = 0; i<N;i++) cerr<<A[i]<<" "; cerr<<"]\n";
#define debuga2(A,N,M) cerr<< #A << " : \n"; for(int i=0;i<N;i++){cerr<<"[";for(int j=0;j<M;++j) cerr<<A[i][j]<<" ";cerr<<"]\n";}
#define debugp(p)      cerr<< #p <<" : "<<"("<<(p).first<<","<<(p).second<<")\n";
#define debugv(v)      cerr<< #v <<" : "<<"[ ";for(int i = 0; i< (v).size(); i++) cerr<<v[i]<<" "; cerr<<"]\n";
#define debugv2(v)     cerr<< #v << " : \n"; for(int i=0;i<v.size();i++){cerr<<"[";for(int j=0;j<(v[0].size());++j) cerr<<v[i][j]<<" ";cerr<<"]\n";}
#define debugs(m)      cerr<< #m <<" : [ "; for(auto itr = m.begin(); itr!=m.end();itr++) cerr<<*itr<<" "; cerr<<"]\n";
#define debugm(m)      cerr<< #m <<" : [ "; for(auto itr = m.begin();itr!=m.end(); itr++) cerr<<"("<<itr->first<<","<<itr->second<<") ";cerr<<"]\n";
// clang-format on

const int xn = 2e5 + 10;
const int xm = -20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const ld eps = 1e-15;
const int base = 257;

int cnt[100001][2];

void solve() {
    int n;cin>>n;
    memset(cnt,0,sizeof(cnt));
    vi a(n);
    rep(i,0,n) {
        cin>>a[i];
        cnt[a[i]][i%2]++;
    }
    sort(all(a));
    rep(i,0,n) {
        cnt[a[i]][i%2]--;
    }
    bool flag=true;
    rep(i,0,n) {
        if(cnt[a[i]][0]!=0||cnt[a[i]][1]!=0) {
            flag=false;
            break;
        }
    }
    if(flag)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}

int32_t main() {
    IOS;
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST