• Educational Codeforces Round 35


    Educational Codeforces Round 35 

    A. Nearest Minimums

    找出最小的位置,相邻相减取最小

    view code
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast,no-stack-protector")
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define endl "
    "
    #define LL long long int
    #define vi vector<int>
    #define vl vector<LL>
    #define all(V) V.begin(),V.end()
    #define sci(x) scanf("%d",&x)
    #define scl(x) scanf("%I64d",&x)
    #define scs(s) scanf("%s",s)
    #define pii pair<int,int>
    #define pll pair<LL,LL>
    #ifndef ONLINE_JUDGE
    #define cout cerr
    #endif
    #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
    #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
    #define debug(x)  cerr << #x << " = " << x << endl
    function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
    template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
    template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
    const int MAXN = 2e5+7;
    
    void solve(){
        int n; sci(n);
        vi A(n); for(int &x : A) sci(x);
        int x = *min_element(all(A));
        vi pos; for(int i = 0; i < n; i++) if(A[i]==x) pos << i;
        int ret = INF;
        for(int i = 1; i < pos.size(); i++) cmin(ret,pos[i]-pos[i-1]);
        cout << ret << endl;
    }
    int main(){
        #ifndef ONLINE_JUDGE
        freopen("Local.in","r",stdin);
        freopen("ans.out","w",stdout);
        #endif
        solve();
        return 0;
    }
    

    B. Two Cakes

    二分答案判断一下是否满足

    view code
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast,no-stack-protector")
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define endl "
    "
    #define LL long long int
    #define vi vector<int>
    #define vl vector<LL>
    #define all(V) V.begin(),V.end()
    #define sci(x) scanf("%d",&x)
    #define scl(x) scanf("%I64d",&x)
    #define scs(s) scanf("%s",s)
    #define pii pair<int,int>
    #define pll pair<LL,LL>
    #ifndef ONLINE_JUDGE
    #define cout cerr
    #endif
    #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
    #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
    #define debug(x)  cerr << #x << " = " << x << endl
    function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
    template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
    template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
    const int MAXN = 2e5+7;
    
    void solve(){
        int n, a, b;
        sci(n); sci(a); sci(b);
        int l = 1, r = min(a,b);
        while(l<=r){
            int mid = (l + r) >> 1;
            if(a / mid + b / mid >= n) l = mid + 1;
            else r = mid - 1;
        }
        cout << r << endl;
    }
    int main(){
        #ifndef ONLINE_JUDGE
        freopen("Local.in","r",stdin);
        freopen("ans.out","w",stdout);
        #endif
        solve();
        return 0;
    }
    

    C. Three Garlands

    可以发现满足条件的只有四种:

    1. 1 x x
    2. 2 2 x
    3. 3 3 3
    4. 2 4 4

    x为任意数

    view code
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast,no-stack-protector")
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define endl "
    "
    #define LL long long int
    #define vi vector<int>
    #define vl vector<LL>
    #define all(V) V.begin(),V.end()
    #define sci(x) scanf("%d",&x)
    #define scl(x) scanf("%I64d",&x)
    #define scs(s) scanf("%s",s)
    #define pii pair<int,int>
    #define pll pair<LL,LL>
    #ifndef ONLINE_JUDGE
    #define cout cerr
    #endif
    #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
    #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
    #define debug(x)  cerr << #x << " = " << x << endl
    function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
    template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
    template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
    const int MAXN = 2e5+7;
    
    void solve(){
        vi A(3);
        for(int &x : A) sci(x);
        sort(all(A));
        if(A[0]==1 or (A[0]==A[1] and A[0]==2) or (A[0]==A[1] and A[1]==A[2] and A[0]==3) or (A[0]==2 and A[1]==A[2] and A[1]==4)) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    int main(){
        #ifndef ONLINE_JUDGE
        freopen("Local.in","r",stdin);
        freopen("ans.out","w",stdout);
        #endif
        solve();
        return 0;
    }
    

    D. Inversion Counting

    假设原来([l,r])区间内的逆序对数量为(x),假设区间长度为(L),那么翻转之后变成了(frac{Lcdot (L+1)}{2}-x)

    那么如果(frac{Lcdot (L+1)}{2})是偶数,奇偶性不变,否则奇偶性改变

    view code
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast,no-stack-protector")
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define endl "
    "
    #define LL long long int
    #define vi vector<int>
    #define vl vector<LL>
    #define all(V) V.begin(),V.end()
    #define sci(x) scanf("%d",&x)
    #define scl(x) scanf("%I64d",&x)
    #define scs(s) scanf("%s",s)
    #define pii pair<int,int>
    #define pll pair<LL,LL>
    #ifndef ONLINE_JUDGE
    #define cout cerr
    #endif
    #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
    #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
    #define debug(x)  cerr << #x << " = " << x << endl
    function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
    template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
    template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
    const int MAXN = 2e5+7;
    
    void solve(){
        int n; sci(n);
        vi A(n); for(int &x : A) sci(x);
        int even = true;
        for(int i = 1; i < n; i++) for(int j = 0; j < i; j++) if(A[j]>A[i]) even ^= 1;
        int m; sci(m);
        while(m--){
            int l, r; sci(l); sci(r);
            int x = r - l + 1;
            if((x*(x-1)/2)&1) even ^= 1;
            cout << (even ? "even" : "odd") << endl;
        }
    }
    int main(){
        #ifndef ONLINE_JUDGE
        freopen("Local.in","r",stdin);
        freopen("ans.out","w",stdout);
        #endif
        solve();
        return 0;
    }
    

    E. Stack Sorting

    先跑给定了的,显然如果遇到最小值就把最小值从栈里拿出来

    如果栈内存在一个元素比栈顶小,输出(-1)

    现在考虑剩下的值,显然每次把小于等于栈顶的全部取出是最优的,由于要最大字典序,那就逆序输出即可

    view code
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast,no-stack-protector")
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define endl "
    "
    #define LL long long int
    #define vi vector<int>
    #define vl vector<LL>
    #define all(V) V.begin(),V.end()
    #define sci(x) scanf("%d",&x)
    #define scl(x) scanf("%I64d",&x)
    #define scs(s) scanf("%s",s)
    #define pii pair<int,int>
    #define pll pair<LL,LL>
    #ifndef ONLINE_JUDGE
    #define cout cerr
    #endif
    #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
    #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
    #define debug(x)  cerr << #x << " = " << x << endl
    function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
    template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
    template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
    const int MAXN = 2e5+7;
    
    void solve(){
        int n, k;
        sci(n); sci(k);
        vi A(k);
        for(int &x : A) sci(x);
        int cur = 1;
        stack<int> stk;
        for(int i = 0; i < k; i++){
            if(A[i]==cur){
                cur++;
                while(!stk.empty() and stk.top()==cur){
                    cur++;
                    stk.pop();
                }
            }else{
                if(!stk.empty() and stk.top()<A[i]){
                    cout << -1 << endl;
                    return;
                }else stk.push(A[i]);
            }
        }
        while(cur<=n){
            if(stk.empty()){
                for(int i = n; i >= cur; i--) A << i;
                break;
            }
            for(int i = stk.top() - 1; i >= cur; i--) A << i;
            cur = stk.top() + 1;
            stk.pop();
        }
        for(int x : A) cout << x << ' '; cout << endl;
    }
    int main(){
        #ifndef ONLINE_JUDGE
        freopen("Local.in","r",stdin);
        freopen("ans.out","w",stdout);
        #endif
        solve();
        return 0;
    }
    

    F. Tree Destruction

    考虑倒着来做,相当于确定一个起始点,然后不断加点进去

    那么显然先找出直径,然后把其他点补上,距离取各个点到直径两端点的最大值即可

    view code
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast,no-stack-protector")
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define endl "
    "
    #define LL long long int
    #define vi vector<int>
    #define vl vector<LL>
    #define all(V) V.begin(),V.end()
    #define sci(x) scanf("%d",&x)
    #define scl(x) scanf("%I64d",&x)
    #define scs(s) scanf("%s",s)
    #define pii pair<int,int>
    #define pll pair<LL,LL>
    #ifndef ONLINE_JUDGE
    #define cout cerr
    #endif
    #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
    #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
    #define debug(x)  cerr << #x << " = " << x << endl
    function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
    template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
    template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
    const int MAXN = 2e5+7;
    int n, dis[MAXN], par[MAXN];
    LL ret;
    vi G[MAXN];
    vector<pair<pii,int> > vec;
    void dfs(int u, int fa){
        dis[u] = dis[fa] + 1;
        par[u] = fa;
        for(int v : G[u]) if(v!=fa) dfs(v,u);
    }
    
    void dfs(int u, int rt, int rrt, int lca){
        int dis1 = dis[u];
        int dis2 = dis[u] + dis[rrt] - 2 * dis[lca];
        if(dis1>dis2) vec << make_pair(make_pair(u,rt),u);
        else vec << make_pair(make_pair(u,rrt),u);
        ret += max(dis1,dis2);
        for(int v : G[u]) if(v!=par[u]) dfs(v,rt,rrt,lca);
    }
    
    void solve(){
        sci(n);
        for(int i = 1; i < n; i++){
            int u, v; sci(u); sci(v);
            G[u] << v; G[v] << u;
        }
        dis[0] = -1;
        dfs(1,0);
        int rt = 1;
        for(int i = 1; i <= n; i++) if(dis[i]>dis[rt]) rt = i;
        dfs(rt,0);
        int anotherrt = rt;
        for(int i = 1; i <= n; i++) if(dis[i]>dis[anotherrt]) anotherrt = i;
        vector<bool> vis(n+1,false);
        int u = anotherrt;
        while(par[u]){
            vis[u] = true;
            vec << make_pair(make_pair(anotherrt,par[u]),par[u]);
            ret += dis[anotherrt] - dis[par[u]];
            u = par[u];
        }
        vis[rt] = true;
        u = anotherrt;
        while(u){
            for(int v : G[u]) if(!vis[v]) dfs(v,rt,anotherrt,u);
            u = par[u];
        }
        cout << ret << endl;
        for(auto p = vec.rbegin(); p != vec.rend(); p++) cout << p->first.first << ' ' << p->first.second << ' ' << p->second << endl;
    }
    int main(){
        #ifndef ONLINE_JUDGE
        freopen("Local.in","r",stdin);
        freopen("ans.out","w",stdout);
        #endif
        solve();
        return 0;
    }
    

    G. Mass Change Queries

    考虑线段树,每个节点存一下每个值的映射,每次修改考虑把原来映射为(x)的点,映射到(y)

    (pushdown)操作就把左右儿子的映射全部更新一遍

    view code
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast,no-stack-protector")
    #include<bits/stdc++.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define endl "
    "
    #define LL long long int
    #define vi vector<int>
    #define vl vector<LL>
    #define all(V) V.begin(),V.end()
    #define sci(x) scanf("%d",&x)
    #define scl(x) scanf("%I64d",&x)
    #define scs(s) scanf("%s",s)
    #define pii pair<int,int>
    #define pll pair<LL,LL>
    #ifndef ONLINE_JUDGE
    #define cout cerr
    #endif
    #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
    #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
    #define debug(x)  cerr << #x << " = " << x << endl
    function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
    template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
    template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
    const int MAXN = (1 << 18) + 7;
    const int MAXV = 101;
    struct SegmentTree{
        int l[MAXN<<1], r[MAXN<<1];
        int f[MAXN<<1][MAXV];
        #define ls(rt) rt << 1
        #define rs(rt) rt << 1 | 1
        void build(int L, int R, int rt = 1){
            l[rt] = L; r[rt] = R;
            for(int i = 1; i < MAXV; i++) f[rt][i] = i;
            if(L + 1 == R) return;
            int mid = (L + R) >> 1;
            build(L,mid,ls(rt)); build(mid,R,rs(rt));
        }
        void pushdown(int rt){
            for(int i = 1; i < MAXV; i++){
                f[ls(rt)][i] = f[rt][f[ls(rt)][i]];
                f[rs(rt)][i] = f[rt][f[rs(rt)][i]];
            }
            for(int i = 1; i < MAXV; i++) f[rt][i] = i;
        }
        void modify(int L, int R, int x, int y, int rt = 1){
            if(L>=r[rt] or l[rt]>=R) return;
            if(L<=l[rt] and r[rt]<=R){
                for(int i = 1; i < MAXV; i++) if(f[rt][i]==x) f[rt][i] = y;
                return;
            }
            pushdown(rt);
            modify(L,R,x,y,ls(rt)); modify(L,R,x,y,rs(rt));
        }
        void print(vi &A, int rt = 1){
            if(l[rt]+1==r[rt]){
                cout << f[rt][A[l[rt]]] << ' ';
                return;
            }
            pushdown(rt);
            print(A,ls(rt)); print(A,rs(rt));
        }
    }ST;
    void solve(){
        int n; sci(n);
        vi A(n); for(int &x : A) sci(x);
        ST.build(0,n);
        int m; sci(m);
        while(m--){
            int l, r, x, y;
            sci(l); sci(r); sci(x); sci(y);
            l--; r--;
            if(x==y) continue;
            ST.modify(l,r+1,x,y);
        }
        ST.print(A);
    }
    int main(){
        #ifndef ONLINE_JUDGE
        freopen("Local.in","r",stdin);
        freopen("ans.out","w",stdout);
        #endif
        solve();
        return 0;
    }
    
  • 相关阅读:
    利用qq设置个性化的域名邮箱
    中小企业为什么要上HR系统
    JAVA设计模式之:命令模式
    hdoj 1251 统计难题 【字典树】
    哈希表(散列)HashTable实现
    HDU 1711 Number Sequence(字符串匹配)
    回溯法之求n个集合的幂集
    微软Azure云计算服务主导全球
    jQuery选择器
    MyEclipse激活步骤
  • 原文地址:https://www.cnblogs.com/kikokiko/p/13586567.html
Copyright © 2020-2023  润新知