1742F - Smaller - CodeForces Solution


greedy strings

Please click on ads to support us..

Python Code:

import sys
input = sys.stdin.readline

for t in range(int(input())):
    res="abcdefghijklmnopqrstuvwxyz"
    dct1={i:0 for i in res}
    dct2={i:0 for i in res}
    for i in range(int(input())):
        b=False
        a,b,c=input().split(" ")
        c=c[:-1]
        if a=='1':
            for i in c:
                dct1[i]+=1*(int(b))
        else:
            for i in c:
                dct2[i]+=1*(int(b))
                        mn=-1
        for j in dct1:
            if dct1[j]!=0:
                if mn!=-1:
                    mn=min(mn,j)
                else:
                    mn=j
        if mn==-1:
            print("YES")
            continue
        b=False
        if b:
            break
        for k in dct2:
            if dct2[k]!=0 and k>mn:
                print("YES")
                break
        else:
            mn1=-1
            for j in dct2:
                if dct2[j]!=0:
                    if mn1!=-1:
                        mn=min(mn1,j)
                    else:
                        mn1=j
            if mn1==-1:
                print("NO")
                continue
            else:
                bl1=True
                bl2=True
                for k in dct2:
                    if k!=mn1:
                        if dct2[k]!=0:
                            bl2=False
                for k in dct1:
                    if k!=mn:
                        if dct1[k]!=0:
                            bl1=False
                if bl2:
                    if dct1[mn]<dct2[mn1] and bl1:
                        print("YES")
                    else:
                        print("NO")
                else:
                    if dct1[mn]<dct2[mn1]:
                        print("YES")
                    else:
                        print("NO")

C++ Code:

// #LGM
/// -------------------------------Copyright @Sandeep kumar------------------------------------ ///
/// ------------------------------------OPTIMIZATIONS--------------------------------------- ///

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")

/// ------------------------------------HEADER-FILES---------------------------------------- ///
#include <bits/stdc++.h>

/// ------------------------------------NAMESPACES------------------------------------------ ///
using namespace std;

/// ------------------------------------DEFINING-MYWAY-------------------------------------- ///
typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef map<int, int> m32;
typedef map<ll, ll> m64;

double eps = 1e-12;

double pi = acos(-1);

#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define nl "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define pb push_back
#define INF 2e18
#define fast_cin()                    \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())

/// ----------------------------------------TRACING------------------------------------- ///

#ifndef ONLINE_JUDGE

template <typename T>
void __p(T a)
{
    cout << a;
}
template <typename T, typename F>
void __p(pair<T, F> a)
{
    cout << "{";
    __p(a.first);
    cout << ",";
    __p(a.second);
    cout << "}";
}
template <typename T>
void __p(std::vector<T> a)
{
    cout << "{";
    for (auto it = a.begin(); it < a.end(); it++)
        __p(*it), cout << ",}"[it + 1 == a.end()];
}
template <typename T>
void __p(std::set<T> a)
{
    cout << "{";
    for (auto it = a.begin(); it != a.end();)
    {
        __p(*it);
        cout << ",}"[++it == a.end()];
    }
}
template <typename T>
void __p(std::multiset<T> a)
{
    cout << "{";
    for (auto it = a.begin(); it != a.end();)
    {
        __p(*it);
        cout << ",}"[++it == a.end()];
    }
}
template <typename T, typename F>
void __p(std::map<T, F> a)
{
    cout << "{\n";
    for (auto it = a.begin(); it != a.end(); ++it)
    {
        __p(it->first);
        cout << ": ";
        __p(it->second);
        cout << "\n";
    }
    cout << "}\n";
}

template <typename T, typename... Arg>
void __p(T a1, Arg... a)
{
    __p(a1);
    __p(a...);
}
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1)
{
    cout << name << " : ";
    __p(arg1);
    cout << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args)
{
    int bracket = 0, i = 0;
    for (;; i++)
        if (names[i] == ',' && bracket == 0)
            break;
        else if (names[i] == '(')
            bracket++;
        else if (names[i] == ')')
            bracket--;
    const char *comma = names + i;
    cout.write(names, comma - names) << " : ";
    __p(arg1);
    cout << " | ";
    __f(comma + 1, args...);
}
#define trace(...) cout << "Line:" << __LINE__ << " ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
///-------------------------------------------segmentation tree by Sandeep kumar----------------------------------------///
class segment_tree
{
public:
    const ll N = 2000000;
    vector<ll> tree = vector<ll>(N, 0);
    vector<ll> tree1 = vector<ll>(N, 0);
    ll n;
    vector<ll> arr;
    segment_tree(ll n1, vector<ll> v1)
    {
        n = n1;
        for (auto it1 : v1)
        {
            arr.push_back(it1);
        }
    }
    // best_being_sandeep
    ~segment_tree(){};
    void build_seg(ll x, ll be, ll en);
    void update_point(ll x, ll be, ll en, ll k, ll val);
    void update_range(ll x, ll be, ll en, ll l, ll r, ll val);
    ll summation_range_lz(ll x, ll be, ll en, ll l, ll r);
    ll summation_point_lz(ll x, ll be, ll en, ll k);
};
/// ----------------------------------------- MATHS by Sandeep kumar ------------------------------------- ///
ll fact(ll n);

ll nCr(ll n, ll r)
{
    return fact(n) / (fact(r) * fact(n - r));
}

ll fact(ll n)
{
    if (n == 0)
        return 1;
    ll res = 1;
    for (ll i = 2; i <= n; i++)
        res = res * i;
    return res;
}
bool is_prime(ll n)
{
    if (n == 1)
        return false;
    if (n == 2 || n == 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;
    for (ll i = 5; i * i <= n; i += 6)
    {
        if (n % i == 0 || n % (i + 2) == 0)
        {
            return false;
        }
    }
    return true;
}
v64 prime_factor(ll n)
{

    v64 v;
    if (n <= 1)
        return v;
    while (n % 2 == 0)
    {
        v.push_back(2);
        n /= 2;
    }
    while (n % 3 == 0)
    {
        v.push_back(3);
        n /= 3;
    }
    for (ll i = 5; i <= n; i += 6)
    {
        while (n % i == 0)
        {
            v.push_back(i);
            n /= i;
        }
        while (n % (i + 2) == 0)
        {
            v.push_back(i + 2);
            n /= (i + 2);
        }
    }
    if (n > 3)
    {
        v.push_back(n);
    }
    return v;
}

ll binary_power(ll x, ll y, ll p)
{
    ll res = 1;
    while (y > 0)
    {
        if (y % 2 == 1)
        {
            res %= p;
            res = (res * x);
        }
        y = y >> 1;
        x %= p;
        x = (x * x);
        x %= p;
    }
    return res % p;
}

ll modulo_inv(ll a, ll m)
{
    return binary_power(a, m - 2, m);
}
/// ----------------------------------------------Sorting----------------------------------------------- ///

bool sortbysec(const pair<int, int> &a,
               const pair<int, int> &b)
{
    return (a.second < b.second);
}
void print(ll arr[], ll n)
{
    forn(i, n)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
}
/// ----------------------------------------------DATA STRUCTURES----------------------------------------------- ///

/// ----------------------------------------------CODE by Sandeep kumar----------------------------------------------- ///

void solve(int it)
{
    ll n;
    cin >> n;
    vector<ll> s(26, 0);
    vector<ll> t(26, 0);
    s[0] = 1;
    t[0] = 1;

    for (int i = 0; i < n; i++)
    {
        ll a;
        cin >> a;
        if (a == 1)
        {
            ll b;
            string s1;
            cin >> b >> s1;
            for (int j = 0; j < s1.size(); j++)
            {
                s[s1[j] - 'a']+=b;
            }
        }
        else
        {
            ll b;
            string s1;
            cin >> b >> s1;
            for (int j = 0; j < s1.size(); j++)
            {
                t[s1[j] - 'a']+=b;
            }
        }
        // trace(s,t);

        ll sum1 = 0;
        ll sum2 = 0;
       
        for (int j = 0; j < 26; j++)
        {
            sum1 += s[j];
            sum2 += t[j];
        }
        //  trace(sum1,sum2);

        if(sum2!=t[0])
        {
            cout<<"YES"<<endl;
        }
        else{
            if(sum2==t[0])
            {
               if(sum1!=s[0])
               {
                cout<<"NO"<<endl;
               }
               else{
                if(sum1>=sum2)
                {
                    cout<<"NO"<<endl;


                }
                else{
                    cout<<"YES"<<endl;
                }
               }
            }
        }
    }
}
signed main()
{
    fast_cin();
    // freopen ("input.txt" , "r" , stdin);
    // freopen ("output.txt", "w" , stdout);
    // manipulated_seive(MAX_SIZE);
    ll t;
    cin >> t;
    for (int it = 1; it <= t; it++)
    {
        solve(it);
    }
    cerr << "time taken : " << (float)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


Comments

Submit
0 Comments
More Questions

1478A - Nezzar and Colorful Balls
1581B - Diameter of Graph
404A - Valera and X
908A - New Year and Counting Cards
146A - Lucky Ticket
1594C - Make Them Equal
1676A - Lucky
1700B - Palindromic Numbers
702C - Cellular Network
1672C - Unequal Array
1706C - Qpwoeirut And The City
1697A - Parkway Walk
1505B - DMCA
478B - Random Teams
1705C - Mark and His Unfinished Essay
1401C - Mere Array
1613B - Absent Remainder
1536B - Prinzessin der Verurteilung
1699B - Almost Ternary Matrix
1545A - AquaMoon and Strange Sort
538B - Quasi Binary
424A - Squats
1703A - YES or YES
494A - Treasure
48B - Land Lot
835A - Key races
1622C - Set or Decrease
1682A - Palindromic Indices
903C - Boxes Packing
887A - Div 64