Codeforces Round #767 (Div. 2) ABCD problem solving

Posted by screamer141 on Mon, 24 Jan 2022 08:43:52 +0100

A. Download More RAM

Idea:
sign reach topic Check in question Check in question
Press mirror a from Small reach large Row order Sort by a from small to large Sort by a from small to large
only want k > = yes answer of b , Just + = b As long as k > = corresponding b, it is + = b As long as k > = corresponding b, it is + = b
transport Out k Namely can Output k Output k
Time complexity: O n l o g n Onlogn Onlogn

#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int> 
#define re register int
#define lb lower_bound
#define up upper_bound
#define int long long 
#define pb push_back
#define y second 
#define x first 
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;} 
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
 
int n , k ;
struct ai{
    int a , b ;
}q[N] ;
bool cmp(ai x , ai y)
{
    return x.a < y.a ;
}
 
signed main()
{
    cf
    {
        cin >> n >> k ;
        fer(i,1,n) sf(q[i].a) ;
        fer(i,1,n) sf(q[i].b) ;
        
        sort(q + 1 , q + 1 + n , cmp) ;
        
        fer(i,1,n)
        {
            if(k >= q[i].a)
                k += q[i].b ;
        }
        
        de(k) ;
    }
    return 0;
}

B. GCD Arrays

Idea:
read finish topic order The first one individual Think reach of Just yes g c d ( x , x + 1 ) = 1 After reading the title, the first thought is gcd(x,x+1)=1 After reading the title, the first thought is gcd(x,x+1)=1
display however this individual nature quality no yes special other shut key Obviously, this nature is not particularly critical Obviously, this nature is not particularly critical

stay Son fine Think Think , only want can can of g c d > 1 Namely can When you think about it, as long as GCD > 1 is possible When you think about it, as long as GCD > 1 is possible
2 also yes except 1 of Outside , [ l , r ] in because number most many of one individual number 2 is the number with the largest factor in [l,r] except 1 2 is the number with the largest factor in [l,r] except 1
place with g c d > 1 , have body yes many less , exercise do number most Small of one set yes 2 So GCD > 1. What is the specific number? The smallest operand must be 2 So GCD > 1. What is the specific number? The smallest operand must be 2

that Do you Unified meter one lower [ l , r ] in have many less individual odd number Then count the number of odd numbers in [l,r] Then count how many odd numbers there are in [l,r]
odd number yes one set want and another Outside one individual even number ride rise come An odd number must be multiplied by another even number An odd number must be multiplied by another even number
place with as fruit odd number Small to etc. to k , transport Out Y E S So if the odd number is less than or equal to k, the output is YES So if the odd number is less than or equal to k, the output is YES

stay special Special sentence break one lower a = b , and And a ! = 1 of feeling condition In a special judgment, a=b, and a= 1 In a special judgment, a=b, and a= 1
because by g c d ( a , a ) = a [ a ! = 1 ] Because GCD (a, a) = a [a! = 1] Because gcd(a,a)=a[a!=1]
this species feeling condition also yes Y E S This situation is also YES This is also YES
Time complexity: O t Ot Ot

#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int> 
#define re register int
#define lb lower_bound
#define up upper_bound
#define int long long 
#define pb push_back
#define y second 
#define x first 
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;} 
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
 
int get(int x) // Returns how many odd numbers are in 1-x
{
    return (x + 1) / 2 ;
}
 
signed main()
{
    cf
    {
        int a , b , k ;
        sf2(a , b) , sf(k) ;
        
        int s = get(b) - get(a - 1) ;
        if(s <= k || (a == b && a != 1)) puts("YES") ;
        else puts("NO") ;
        
    }
    return 0;
}

C. Meximum Array

Idea:
first before want Give Way word Canon order most large First, make the dictionary order maximum First of all, we should maximize the order of the dictionary
that Do you Ken set excellent before choose most large of Then you must give priority to the largest Then you must give priority to the largest

I Men can with hair present answer case of The first one individual number yes whole individual order column in noodles of M E X We can find that the first number of the answer is MEX in the whole sequence We can find that the first number of the answer is MEX in the whole sequence
that Do you I Men first before eliminate except of front k individual number So we first eliminate the first k numbers So we first eliminate the first k numbers

want protect card this k individual number of M E X = whole individual order column of M E X Ensure that the MEX of this k number is equal to the MEX of the whole sequence Ensure that the MEX of this k number is equal to the MEX of the whole sequence
and And protect card k most Small , this kind you can with choose more many of number And keep k minimum, so you can choose more numbers And keep k minimum, so you can choose more numbers

Delete fall of after of lower one individual number yes Leftover lower one whole individual order column of M E X The next number after deletion is the MEX of the whole sequence The next number after deletion is the MEX of the whole sequence
model Draft upper State too Course Namely can Just simulate the above process Just simulate the above process

have body can with see generation code fine section See the code details for details See the code details for details
Time complexity: O n On On

#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int> 
#define re register int
#define lb lower_bound
#define up upper_bound
#define int long long 
#define pb push_back
#define y second 
#define x first 
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;} 
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
 
int n ;
int a[N] ;
int st[N] ;
int s[N] ;
 
signed main()
{
    cf
    {
        cin >> n ;
        fer(i,0,n) st[i] = 0 ;
        fer(i,1,n) sf(a[i]) , st[a[i]] ++ ;
        int k = 0 ;
        while(st[k]) k ++ ;
        // k is the MEX of the whole sequence
        vector<int> v ;
        // v array record answer
        
        int t = 0 ; 
        // t indicates how many numbers appear from 0 to k-1
        fer(i,0,k) s[i] = 0 ;
        for(int i = 1 ; i <= n ; i ++)
        {
            if(a[i] <= k - 1)
            {
                if(!s[a[i]])
                {
                    s[a[i]] ++ ;
                    t ++ ;
                }
                else s[a[i]] ++ ;
            }
            
            if(t == k) // If the number of consecutive occurrences = k
            {
                v.pb(k) ;
                fer(i,0,k - 1) st[i] -= s[i] ;
                // Subtract the number of occurrences of these numbers from the st array
                t = 0 ; 
                k = 0 ;
                while(st[k]) k ++ ;
                // Rediscover the MEX of the entire sequence
                fer(i,0,k) s[i] = 0 ;
            }
        }
        
        // Output answer
        de(sz(v)) ;
        for(auto i : v) cout << i << " " ;
        puts("") ;
        
    }
    return 0;
}

D. Peculiar Movie Preferences

Idea:
as fruit Out present Yes with one individual word mother group become of word symbol strand one set by Y E S If a string composed of the same letter appears, it must be YES If a string composed of the same letter appears, it must be YES

stay Test Worry about long degree by 2 of word symbol strand Consider a string of length 2 Consider a string of length 2
x y and y x with Time Out present say bright can with If xy and yx appear at the same time, it can be explained If xy and yx appear at the same time, it can be explained

stay Test Worry about long degree by 3 of word symbol strand Consider a string of length 3 Consider a string of length 3
x y z and z y x with Time Out present say bright can with If xyz and zyx appear at the same time, it can be explained If xyz and zyx appear at the same time, it can be explained

stay Test Worry about long degree by 2 and 3 of Spell meet Considering splices of lengths 2 and 3 Considering splices of lengths 2 and 3
as fruit Out present Yes x y z y x [ x y z + y x ] If xyzyx[xyz+yx] appears If xyzyx[xyz+yx] appears
x y z stay front noodles y x stay after noodles xyz is in the front and yx is in the back xyz is in the front and yx is in the back

or person yes x y z y x [ x y + z y x ] Or xyzyx[xy+zyx] Or xyzyx[xy+zyx]
x y stay front noodles , z y x stay after noodles xy is in the front, zyx is in the back xy is in the front, zyx is in the back
this 2 species feeling condition all can with Both cases are OK Both cases are OK
his he feeling condition all by no can can Other situations are impossible Other situations are impossible

by What Do you no Beg theory 2 individual long degree by 3 and 3 individual long degree by 2 of Spell meet Why not discuss 2 splices of length 3 and 3 splices of length 2 Why not discuss 2 splices of length 3 and 3 splices of length 2
because by as fruit Out present Yes this kind of feeling condition , say bright one set Out present Yes long degree by 2 and 3 of Spell meet Because if this happens, there must be splices with lengths of 2 and 3 Because if this happens, there must be splices with lengths of 2 and 3
Time complexity: O n l o g n Onlogn Onlogn

#include <bits/stdc++.h>
#define fer(i,a,b) for(re i = a ; i <= b ; ++ i)
#define der(i,a,b) for(re i = a ; i >= b ; -- i)
#define cf int _; cin>> _; while(_--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define sf(x) scanf("%lld",&x)
#define pll pair<int,int> 
#define re register int
#define lb lower_bound
#define up upper_bound
#define int long long 
#define pb push_back
#define y second 
#define x first 
using namespace std;
inline void sf2(int &a , int &b) { sf(a) , sf(b) ;}
inline void sf3(int n , int a[]) {fer(i,1,n) sf(a[i]);} ;
inline void de(auto x) {cout << x << "\n" ;}
inline void de2(auto a , auto b) {cout << a << " " << b << "\n" ;}
inline void de3(int n , auto a[]){fer(i,1,n) cout << a[i] << " " ;puts("");}
inline void mo(int &a , int b) {a = (a % b + b) % b ;}
inline int gcd(int a,int b){return b ? gcd(b , a % b) : a ;}
inline int qpow(int a,int b,int c){int res=1%c;a%=c;while(b>0){if(b&1)res=res*a%c;a=a*a%c;b>>=1;}return res;}
inline int qadd(int a,int b,int c){int res=0;a%=c;while(b>0){if(b&1)res=(res+a)%c;a=(a+a)%c;b>>=1;}return res;} 
const int inf = 0x3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f3f ;
const int N = 1e6 + 10 , M = 3010 , mod = 1e9 + 7 ;
const double eps = 1e-7 , pi = acos(-1.0) ;
 
int n ;
string q[N] ;
 
signed main()
{
    cf
    {
        cin >> n ;
        int f1 = 0 ;
        map<string,int> mp , p;
        fer(i,1,n) 
        {
            cin >> q[i] , mp[q[i]] ++ ;    
            if(sz(q[i]) == 1) f1 = 1 ;
            else if(sz(q[i]) == 2) 
            {
                if(q[i][0] == q[i][1]) 
                    f1 = 1 ;
            }
            else 
            {
                if(q[i][0] == q[i][1] && q[i][0] == q[i][2])
                    f1 = 1 ;
            }
            // If a string composed of the same letter appears, it must be YES
        }
        
        fer(i,1,n)
        {
            p[q[i]] ++ ;
            if(sz(q[i]) == 2)
            {
                string t = q[i] ;
                reverse(all(t)) ;
                if(mp[t]) f1 = 1 ;
                // If xy and yx appear at the same time, it can be explained
            }
            else if(sz(q[i]) == 3)
            {
                string t1 = "" ;
                string t2 = "" ;
                
                string t = q[i] ;
                reverse(all(t)) ;
                if(mp[t]) f1 = 1 ;
                // xyz and zyx
                
                fer(j,1,2) t1 = t1 + q[i][j] ;
                fer(j,0,1) t2 = t2 + q[i][j] ;
                
                reverse(all(t1)) ;
                reverse(all(t2)) ;
                if(p[t1] || mp[t2]) f1 = 1 ;
                // If xyzyx[xyz+yx] appears
                // Or xyzyx[xy+zyx] can also explain
            }
            mp[q[i]] -- ;
        }
        
        if(f1) puts("YES") ;
        else puts("NO") ;
    }
    return 0;
}

Topics: Algorithm data structure Dynamic Programming