146D - Lucky Number 2 - CodeForces Solution


brute force constructive algorithms implementation *1800

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define PG_Mazen ios_base::sync_with_stdio( 0 ), cin.tie( 0 ), cout.tie( 0 );
#define el '\n'
#define sp ' '
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define SZ(v) (v).size()
#define pb(n) push_back(n)
#define pf(n) push_front(n)
#define eb(n, m) emplace_back(n, m)
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define int long long
#define double long long double
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define pll pair<ll,ll>
#define F first
#define S second
#define loop int t; cin >> t; for(int i=1;i<=t;++i)
#define f0(i, st, n) for(int i=st;i<n;++i)
#define f1(i, st, n) for(int i=st;i<=n;++i)
#define fe(it, n) for(auto &it : n)
#define mms(arr, val) memset(arr, val, sizeof arr)
#define mmv(vec, val) memset(vec[0], val, sizeof(vec) * SZ(vec))
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

const int N = 1e5 + 5, MOD = 2019, oo = 0x3f3f3f3f;
const ll ooo = 0x3f3f3f3f3f3f3f3f;

void testCase() {
    int a1, a2, a3, a4;
    cin >> a1 >> a2 >> a3 >> a4;
    if (abs(a3 - a4) > 1 || a1 < max(a3, a4) || a2 < max(a3, a4) || (a1 + a2 <= a3 + a4)) {
        cout << "-1";
        return;
    }

    deque<char> dq;
    while (a3 + a4 > 1) {
        dq.pb('4');
        dq.pb('7');
        --a1, --a2, --a3, --a4;
    }
    if (a1)
        dq.pb('4'), --a1;
    else
        dq.pf('7'), --a2;
    if (a3) {
        if (dq.front() == '7' && a1)
            dq.pf('4'), --a1;
        else if(dq.front() == '4' && a2)
            dq.pb('7'), --a2;
        else {
            cout << "-1";
            return;
        }
    }
    if (a4) {
        if (dq.front() == '4' && a2)
            dq.pf('7'), --a2;
        else if(dq.front() == '7' && a1)
            dq.pb('4'), --a1;
        else {
            cout << "-1";
            return;
        }
    }
    if (a1) {
        if (dq.front() == '4')
            while (a1--) dq.pf('4');
        else {
            dq.pop_front();
            while (a1--) dq.pf('4');
            dq.pf('7');
        }
    }
    if (a2) {
        if (dq.back() == '7')
            while (a2--) dq.pb('7');
        else {
            dq.pop_back();
            while (a2--) dq.pb('7');
            dq.pb('4');
        }
    }
    fe(it, dq) cout << it;
}

int32_t main() {
    PG_Mazen
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    testCase();
}
	 	 		  				 				 		 	    			 	


Comments

Submit
0 Comments
More Questions

677A - Vanya and Fence
1621A - Stable Arrangement of Rooks
472A - Design Tutorial Learn from Math
1368A - C+=
450A - Jzzhu and Children
546A - Soldier and Bananas
32B - Borze
1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys