COFO::Round #665 ( div 2 )

Problem A : Distance and Axis

Point

Design

Big O(time)

Big O(memory)

Code

// https://beenpow.github.io/
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define r_rep(i,a,b) for(int i=a;i>b;i--)
#define vi vector<int>
#define vpi vector<pair<int, int> >
typedef long long ll;
const int MAXN = 1000000 + 5, inf = 1e9;
using namespace std;
void process(){
    int n, k;
    cin >> n >> k;
    if(n < k) cout << k - n << endl;
    else{
        if(n%2 == k%2) cout << "0" << endl;
        else cout << "1" << endl;
    }
}
int main(){
    int tc; cin >> tc;
    while (tc--)
        process();
    return 0;
}

Problem B : Termary Sequence

Point

Design

Big O(time)

Big O(memory)

Code

// https://beenpow.github.io/
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define r_rep(i,a,b) for(int i=a;i>b;i--)
#define vi vector<int>
#define vpi vector<pair<int, int> >
typedef long long ll;
const int MAXN = 1000000 + 5, inf = 1e8;
using namespace std;


void process(){
    int a0, a1, a2; cin >> a0 >> a1 >> a2;
    int b0, b1, b2; cin >> b0 >> b1 >> b2;
    int gap0_2 = min(a0, b2);
    a0 -= gap0_2, b2 -= gap0_2;
    if(b2 == 0){
        cout << 2 * 1 * min(a2, b1) << endl;
        return;
    }
    int gap2_2 = min(a2, b2);
    a2 -= gap2_2, b2 -= gap2_2;
    if(b2 == 0){
        cout << 2 * 1 * min(a2, b1) << endl;
        return;
    }
    cout << -1 * 1 * 2 * min(a1, b2) << endl;
}
int main(){
    int tc; cin >> tc;
    while (tc--)
        process();
    return 0;
}

Problem C : Mere Array

Point

Design

Big O(time)

Big O(memory)

Code

// https://beenpow.github.io/
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define r_rep(i,a,b) for(int i=a;i>b;i--)
#define vi vector<int>
#define vpi vector<pair<int, int> >
typedef long long ll;
const int MAXN = 1000000 + 5, inf = 1e9;
using namespace std;

int n;
vi a, b;
vi ans;
void process(){
    cin >> n;
    a.resize(n);
    
    int mn = inf;
    rep(i, 0, n){
        cin >> a[i];
        mn = min(mn, a[i]);
    }
    
    b = a;
    sort(all(b));
    
    rep(i, 0, n) if(a[i] != b[i]){
        if(a[i] % mn != 0 || a[i] < mn){
            cout << "NO" << endl;
            return;
        }
    }
    
    cout << "YES" << endl;
}
int main(){
    int tc; cin >> tc;
    while (tc--)
        process();
    return 0;
}

Problem D : Maximum Distributed Tree

Point

Design

Big O(time)

Big O(memory)

Code

// https://beenpow.github.io/
#include<bits/stdc++.h>
#define endl '\n'
#define pb push_back
#define f first
#define s second
#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define r_rep(i,a,b) for(int i=a;i>b;i--)
#define vi vector<int>
#define vll vector<ll>
#define vpi vector<pair<int, int> >
typedef long long ll;
const int MAXN = 100000 + 5, inf = 1e9;
#define MOD 1000000007
using namespace std;

int n, m;
vi adj[MAXN];

int sz[MAXN];
vll v;
void dfs(int here, int prev){
    sz[here] = 1;
    for(auto there : adj[here]){
        if(there == prev) continue;
        dfs(there, here);
        sz[here] += sz[there];
    }
    if(here != 1) v.pb( 1LL * sz[here] * (n - sz[here]));
}

void process(){
    cin >> n;
    
    // init
    rep(i, 0, n+1) adj[i].clear(), sz[i] = 0;
    //w.clear();
    v.clear();
    
    rep(i, 0, n-1){
        int u, v; cin >> u >> v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    cin >> m;
    vll w (m, 0);
    rep(i, 0, m) cin >> w[i];
    
    
    dfs(1, 0);
    
    sort(v.rbegin(), v.rend());
    sort(w.rbegin(), w.rend());
    
    ll ans = 0;
    int cnt = max(1, m - (n-1) + 1); // n-1 >= m ,  n-1 < m
    int j = 0;
    ll val = v[0] % MOD;
    while(cnt > 0){
        val *= w[j++];
        val %= MOD;
        cnt--;
    }
    
    ans += val; //  the largest part
    ans %= MOD;
    
    rep(i, 1, v.size()){
        if(j < w.size()) ans += v[i] * w[j++];
        else ans += v[i];
        ans %= MOD;
    }
    cout << ans << endl;
}
int main(){
    int tc; cin >> tc;
    while (tc--)
        process();
    return 0;
}