loading...

Daily Coding Challenge #69

wingkwong profile image Wing-Kam ・2 min read

About

This is a series of Daily Coding Challenge. Each day I show a few solutions written in C++. The questions are from coding practice/contest sites such as HackerRank, LeetCode, Codeforces, Atcoder and etc.


/*
Codeforces Round #656 (Div. 3) - A. Three Pairwise Maximums
https://codeforces.com/contest/1385/problem/A
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int t,x,y,z;
    cin >> t;
    while(t--){
        cin >> x >> y >> z;
        // x,y,z should have at least two pairs
        if(x==y&&y==z&&x==z) {
            cout << "YES\n";
            cout << x << " " << y << " " << z << "\n";
        }
        else if(x==y&&z<x&&z<y) {
            cout << "YES\n";
            cout << x << " " << 1 << " " << z << "\n";
        }
        else if(y==z&&x<y&&x<z) {
            cout << "YES\n";
            cout << x << " " << 1 << " " << z << "\n";
        }
        else if(x==z&&y<x&&y<z) {
            cout << "YES\n";
            cout << 1 << " " << x << " " << y << "\n";
        }
        else cout << "NO\n";
    }
    return 0;
} 

int sol2()  
{ 
    FAST_INP;
    int t,x,y,z;
    cin >> t;
    while(t--){
        vector<int> a(3);
        for(auto &x:a) cin >> x;
        sort(a.begin(),a.end());
        if(a[1]!=a[2]) {
            cout << "NO\n";
        } else {
            cout << "YES\n";
            cout << a[0] << " " << a[0] << " " << a[2] << "\n"; 
        }
    }
    return 0;
} 


/*
Codeforces Round #656 (Div. 3) - B. Restore the Permutation by Merger
https://codeforces.com/contest/1385/problem/B
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int n,t;
    cin >> t;
    while(t--){
        cin >> n;
        vector<int> s;
        int p;
        for(int i=0;i<2*n;i++){
            cin >> p;
            // use find to see if it exists
            if(find(s.begin(),s.end(),p) == s.end()) {
                s.push_back(p);
            }
        }
        for(auto x:s) cout << x << " ";
        cout << "\n";
    }
    return 0;
} 

int sol2()  
{ 
    FAST_INP;
    int n,t;
    cin >> t;
    while(t--){
        cin >> n;
        vector<int> s;
        unordered_map<int,int> m;
        int p;
        for(int i=0;i<2*n;i++){
            cin >> p;
            // use unordered_map to see if it exists
            if(!m[p]) {
                s.push_back(p);
                m[p]=1;
            }
        }
        for(auto x:s) cout << x << " ";
        cout << "\n";
    }
    return 0;
} 


/*
Codeforces Round #656 (Div. 3) - C. Make It Good
https://codeforces.com/contest/1385/problem/C
*/

#include <bits/stdc++.h>
using namespace std; 
#define FAST_INP  ios_base::sync_with_stdio(false);cin.tie(NULL)

int main()  
{ 
    FAST_INP;
    int n,t;
    cin >> t;
    while(t--){
        cin >> n;
        vector<int> v(n);
        for(auto &x:v) cin >>x;
        // a1<=a2<=...<=ax>=...>=ak-1>=ak-2<=ak2
        int k=n-1;
        while(k>0&&v[k-1]>=v[k]) k--;
        while(k>0&&v[k-1]<=v[k]) k--;
        cout << k << "\n";
    }
    return 0;
} 



The source code is available in corresponding repo below. Star and watch for timely updates!

GitHub logo wingkwong / leetcode

🏆 A Collection of my LeetCode Solutions with Explanations 🏆

GitHub logo wingkwong / hackerrank

🏆 A Collection of my HackerRank Solutions with Explanations 🏆

GitHub logo wingkwong / codeforces

🏆 A Collection of my Codeforces Solutions with Explanations 🏆

GitHub logo wingkwong / atcoder

🏆 A Collection of my AtCoder Solutions with Explanations 🏆

Posted on by:

wingkwong profile

Wing-Kam

@wingkwong

Consultant by day. Developer by night. AWS certified. Exploring #CloudNative currently.

Discussion

markdown guide