469D - Two Sets - CodeForces Solution


2-sat data structures graph matchings greedy *2000

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <bits/stdc++.h>

// policy based datastructure
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define en '\n'
#define ff first
#define ss second
#define pb push_back
#define MP make_pair
#define mod 1000000007
#define mod2 998244353
#define fast                      \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
using namespace std;

typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef priority_queue<ll> pqi;
typedef priority_queue<ll, vi, greater<ll>> minpqi;
typedef pair<ll, ll> pii;
#define debarr(a, n)            \
    cout << #a << " : ";        \
    for (int i = 0; i < n; i++) \
        cerr << a[i] << " ";    \
    cerr << endl;
#define debmat(mat, row, col)         \
    cout << #mat << " :\n";           \
    for (int i = 0; i < row; i++)     \
    {                                 \
        for (int j = 0; j < col; j++) \
            cerr << mat[i][j] << " "; \
        cerr << endl;                 \
    }
#define pr(...) dbs(#__VA_ARGS__, __VA_ARGS__)
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p)
{
    return os << "(" << p.first << ", " << p.second << ")";
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &p)
{
    os << "[ ";
    for (auto &it : p)
        os << it << " ";
    return os << "]";
}
template <class T>
ostream &operator<<(ostream &os, const unordered_set<T> &p)
{
    os << "[ ";
    for (auto &it : p)
        os << it << " ";
    return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const unordered_map<S, T> &p)
{
    os << "[ ";
    for (auto &it : p)
        os << it << " ";
    return os << "]";
}
template <class T>
ostream &operator<<(ostream &os, const set<T> &p)
{
    os << "[ ";
    for (auto &it : p)
        os << it << " ";
    return os << "]";
}
template <class T>
ostream &operator<<(ostream &os, const multiset<T> &p)
{
    os << "[ ";
    for (auto &it : p)
        os << it << " ";
    return os << "]";
}
template <class S, class T>
ostream &operator<<(ostream &os, const map<S, T> &p)
{
    os << "[ ";
    for (auto &it : p)
        os << it << " ";
    return os << "]";
}
template <class T>
void dbs(string str, T t) { cerr << str << " : " << t << "\n"; }
template <class T, class... S>
void dbs(string str, T t, S... s)
{
    int idx = str.find(',');
    cerr << str.substr(0, idx) << " : " << t << ",";
    dbs(str.substr(idx + 1), s...);
}
template <class T>
void prc(T a, T b)
{
    cerr << "[";
    for (T i = a; i != b; ++i)
    {
        if (i != a)
            cerr << ", ";
        cerr << *i;
    }
    cerr << "]\n";
}
#define all(x) x.begin(), x.end()
#define present(c, key) (find(all(c), key) != c.end())
#define tr(c, it) for (auto it : c)
#define fr(i, s, e) for (ll i = s; i < e; i++)
#define revfr(i, s, e) for (ll i = s - 1; i >= e; i--)
#define getv(v, n)             \
    for (ll i = 0; i < n; i++) \
        cin >> v[i];

template <typename T>
ostream &operator<<(ostream &os, vector<T> &v)
{

    for (auto element : v)
    {
        os << element << ' ';
    }
    return os;
}
inline void add(ll &a, ll b)
{
    a += b;
    if (a >= mod)
        a -= mod;
}

inline void sub(ll &a, ll b)
{
    a -= b;
    if (a < 0)
        a += mod;
}

inline ll mul(ll a, ll b, ll p = mod)
{
    return (ll)((long long)a * b % p);
}

inline ll power(ll a, long long b, ll p = mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = mul(res, a, p);
        }
        a = mul(a, a, p);
        b >>= 1;
    }
    return res;
}
inline ll binpow(ll a, long long b)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = res * a;
        }
        a = a * a;
        b >>= 1;
    }
    return res;
}
inline ll inv(ll a, ll p = mod)
{
    a %= p;
    if (a < 0)
        a += p;
    ll b = p, u = 0, v = 1;
    while (a)
    {
        ll t = b / a;
        b -= t * a;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    if (u < 0)
        u += p;
    return u;
}
// inline ll lsb(ll x)
// {
//     return x&(~(x-1));
// }
// ll modexp2(ll a, ll b, ll c, ll p) // calculate (a^(b^c))%prime
// {
//     // By Fermat's Little Theorem (a^(p-1))%p = 1 if p is prime
//     ll x = power(b,c,p-1);  // so we first find remainder when b^c is divided by p-1
//     return (power(a,x,p));  // then just do (a^remainder)%p

// }
ll gcd(ll a, ll b)
{
    if (a == 0 or b == 0)
        return a ^ b;
    return gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
    return a * (b / gcd(a, b));
}
void fread()
{
    // #ifdef ONLINE_JUDGE
    freopen("./input.txt", "r", stdin);
    freopen("./output.txt", "w", stdout);
    // #endif
}

void solve()
{
    ll n,a,b;
    cin>>n>>a>>b;
    vi v(n);
    getv(v,n);
    map<ll,ll>sol;
    fr(i,0,n)sol[v[i]]=-1;
    vi ans(n,0);
    if(a==b)
    {
        fr(i,0,n)
        {
            if(sol[a-v[i]]==0)
            {
                cout<<"NO\n";
                return;
            }
        }
        cout<<"YES\n";
        fr(i,0,n)cout<<"0 ";
        cout<<en;
        return;
    }
    vi nxt;
    fr(i,0,n)
    {
        if(sol[v[i]]!=-1)continue;
        ll a1=a-v[i];
        ll b1=b-v[i];
        nxt.clear();
        nxt.pb(v[i]);
        ll done=0;
        while(!done)
        {
            if(sol[a1]==-1 and sol[b1]==-1)
            {
                nxt.pb(a1);
                nxt.pb(b1);
                swap(a1,b1);
                a1=(a-a1);
                b1=(b-b1);
            }
            else if(sol[a1]==-1)
            {
                nxt.pb(a1);
                done=1;
                fr(j,0,nxt.size())sol[nxt[j]]=1;
            }
            else if(sol[b1]==-1)
            {
                nxt.pb(b1);
                done=1;
                fr(j,0,nxt.size())sol[nxt[j]]=2;
            }
            else
            {
                cout<<"NO\n";
                return;
            }
        }
    }
    cout<<"YES\n";
    fr(i,0,n)cout<<(sol[v[i]]-1)<<" ";
    cout<<en;
}

using namespace std;

int main()
{
    fast
    // pre();
    // fread();
    ll _t = 1;
    // cin >> _t;
    fr(i, 1, _t + 1)
    {
        // cerr<<"i="<<i<<en;
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
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