1472F - New Year's Puzzle - CodeForces Solution


brute force dp graph matchings greedy sortings *2100

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h> 
using namespace std;
#define ll                long long
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define nl                "\n"
#define all(x)            (x).begin(),(x).end()
#define sz(x)             (int)((x).size())
#define pii               pair<int, int>
#define pll               pair<long long, long long>
#define pdd               pair<double, double>
#define mp(x, y)          make_pair(x, y)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define lb(vec, item)      lower_bound((vec).begin(), (vec).end(), item)
#define ub(vec, item)      upper_bound((vec).begin(), (vec).end(), item)

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
 
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;

void run_case(){
    int n, m;
    cin >> n >> m;
    
    vector<pii> a(m);
    
    for(auto &x: a)
        cin >> x.first >> x.second;
    
    sort(all(a), [&](pii one, pii two) {
        return one.second < two.second;
    });
    
    vector<pii> v;
    
    auto check = [&]() {
        if(sz(v) & 1)
            return false;
        
        for(int i = sz(v) - 1; i >= 0; i -= 2) {
            int diff = abs(v[i].second - v[i - 1].second - 1);
            if(abs(v[i].first - v[i - 1].first) != diff % 2)
                return false;
        }  
        
        return true;
    };
    
    bool ans = true;
    
    for(int i = 0; i < m; ++i) {
        if(!v.empty() and a[i].second == v.back().second) {
            v.ppb();
            ans &= check();
            v.clear();
        } else {
            v.pb(a[i]);
        }
    }
    
    ans &= check();
    
    cout << (ans ? "YES\n" : "NO\n");
}
 
int main(){

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    ios_base::sync_with_stdio(false); 
    cin.tie(0); cout.tie(0);
    
    int tests;
    cin >> tests;
    while(tests--){
        run_case();
    }

}


Comments

Submit
0 Comments
More Questions

1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations
677C - Vanya and Label