1381B - Unmerge - CodeForces Solution


dp *1800

Please click on ads to support us..

Python Code:

import sys
 
input = sys.stdin.readline
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ls = []
    cnt = 1
    mx = a[0]
    for i in range(1, 2 * n):
        if a[i] > mx:
            mx = a[i]
            ls.append(cnt)
            cnt = 1
        else:
            cnt += 1
    if cnt:
        ls.append(cnt)
    dp = 1
    for i in ls:
        dp |= dp << i
    if dp & 1 << n:
        print("YES")
    else:
        print("NO")

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define f(i, a, b) for (ll i = a; i < b; i++)
#define fr(i, a, b) for (ll i = b - 1; i >= a; i--)
#define fa(x) for (auto ele : x)

#define all(x) (x).begin(), (x).end()
#define ll long long
#define db double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ppiii pair<pair<int, int>, int>

#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vs vector<string>
#define v(ele) vector<ele>

#define sei set<int>
#define sel set<ll>
#define ses set<string>
#define sec set<char>
#define se(ele) set<ele>

#define mii map<int, int>
#define mll map<ll, ll>
#define mipii map<int, pair<int, int>>
#define mcpii map<char, pair<int, int>>
#define mcpci map<char, pair<char, int>>
#define mpiii map<pair<char, int>, int>
#define mic map<int, char>
#define mci map<char, int>
#define mcsei map<char, sei>
#define mlsel map<ll, sel>
#define mcvi map<char, vi>
#define mci map<char, int>
#define msi map<string, int>
#define misei map<int, sei>
#define mlvl map<ll, vl>
#define mivi map<int, vi>
#define mlpll map<ll, pll>
#define mplll map<pll, ll>
#define m(e1, e2) map<e1, e2>

#define qi queue<int>
#define ql queue<ll>
#define qpii queue<pair<int, int>>

const ll mod = 1e9 + 7;
const ll INF = 1e16;
const ll NEG_INF = LONG_LONG_MIN;

const ll N = 1e3 + 5;
// const int N = 51;

void YN(bool possible)
{
    if (possible)
    {
        cout << "YES\n";
    }
    else
    {
        cout << "NO\n";
    }
}
void solve()
{
    int n;
    cin >> n;
    int arr[2 * n];
    f(i, 0, 2 * n)
    {
        cin >> arr[i];
    }
    vi v;
    int cnt = 1, rep = arr[0];
    f(i, 1, 2 * n)
    {
        if (rep < arr[i])
        {
            v.push_back(cnt);
            rep = arr[i];
            cnt = 1;
        }
        else
        {
            cnt++;
        }
    }
    v.push_back(cnt);
    int len = v.size();
    bool dp[n + 1]{0};
    dp[0] = 1;
    f(i, 0, len)
    {
        int val = v[i];
        // dp[val] = 1;
        fr(j, 0, n - val + 1)
        {
            if (dp[j])
            {
                dp[j + val] = 1;
            }
        }
    }
    YN(dp[n]);
    return;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1713C - Build Permutation
1699A - The Third Three Number Problem
1617B - GCD Problem
841A - Generous Kefa
1690B - Array Decrements
1692C - Where's the Bishop
104A - Blackjack
1438A - Specific Tastes of Andre
1711C - Color the Picture
1194C - From S To T
110B - Lucky String
1114A - Got Any Grapes
224B - Array
125B - Simple XML
567B - Berland National Library
431B - Shower Line
282C - XOR and OR
1582B - Luntik and Subsequences
609A - Флеш-карты
1207A - There Are Two Types Of Burgers
371C - Hamburgers
343B - Alternating Current
758B - Blown Garland
1681B - Card Trick
1592A - Gamer Hemose
493D - Vasya and Chess
1485A - Add and Divide
337B - Routine Problem
1392D - Omkar and Bed Wars
76E - Points