1311B - WeirdSort - CodeForces Solution


dfs and similar sortings *1200

Please click on ads to support us..

Python Code:

t = int(input())

for _ in range(t):
    n, m = map(int, input().split())

    l = [False] * n

    arr = list(map(int, input().split()))
    pi = list(map(int, input().split()))

    for p in pi:
        l[p-1] = True

    f = True
    i = 0

    while i < n:
        if not l[i]:
            i += 1
            continue
        idx = i
        while idx < n and l[idx]:
            idx += 1
        arr[i:idx+1] = sorted(arr[i:idx+1])
        i = idx

    for i in range(len(arr)-1):
        if arr[i] > arr[i+1]:
            f = False
            break

    print("YES" if f else "NO")








C++ Code:

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



#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) { cerr << t; }
void _print(int t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(lld t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(ull t) { cerr << t; }

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}"; }
template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; }
template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; }

void solve()
{
    int n;
    cin >> n;
    int m;
    cin >> m;
    vector<int> arr(n);
    vector<int> pos(m);
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    for (int i = 0;i < m;i++)
    {
        cin >> pos[i];
        pos[i]--;
    }
    sort(all(pos));
    vector<int> sarray = arr;
    sort(all(sarray));
    vector<pair<int, int> > positions;

    for (int ind = 0;ind < m;ind++)
    {
        int i = ind;
        int j = ind + 1;
        while (j < m && pos[j] - pos[j - 1] == 1)
        {
            j++;
        }
        j--;
        debug(pos[i]);
        debug(pos[j]);

        if (i == j)
        {
            sort(arr.begin() + pos[i], arr.begin() + pos[i] + 1 + 1);
            debug(arr);
        }
        else
        {
            sort(arr.begin() + pos[i], arr.begin() + pos[j] + 1 + 1);
        }
    }
    // sort(arr.begin(), arr.begin() + 2);
    debug(arr);
    debug(sarray);
    debug(pos);
    int flag = 0;
    for (int i = 0; i < arr.size(); i++)
    {
        if (arr[i] != sarray[i])
        {
            flag = 1;
            break;
        }
    }
    if (flag == 1)
    {
        cout << "NO\n";
        return;
    }
    else
    {
        cout << "YES\n";
    }




}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t << ": ";

        solve();
    }
}


Comments

Submit
0 Comments
More Questions

590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array