• Codeforces Round #636 (Div. 3)


    传送门

    ABC略。。

    D. Constant Palindrome Sum

    题意:
    给出(n)个数,(n)为偶数,每个数在范围([1,k])内。
    现在可以改变任一个数的值,范围也要在([1,k])之内。
    问最少改变多少次,使得(a_i+a_{n-i+1}=x)对于所有的(i)都满足。

    思路:
    当前数为(x,y)时,那么考虑他们所有可能的情况,会发现对答案的贡献为区间的形式。所以我们每对数直接差分维护一下对答案的贡献就行。

    Code
    /*
     * Author:  heyuhhh
     * Created Time:  2020/4/22 11:50:35
     */
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <set>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <assert.h>
    #define MP make_pair
    #define fi first
    #define se second
    #define pb push_back
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
    #define INF 0x3f3f3f3f
    #define Local
    #ifdef Local
      #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
      void err() { std::cout << std::endl; }
      template<typename T, typename...Args>
      void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
      template <template<typename...> class T, typename t, typename... A> 
      void err(const T <t> &arg, const A&... args) {
      for (auto &v : arg) std::cout << v << ' '; err(args...); }
    #else
      #define dbg(...)
    #endif
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    //head
    const int N = 2e5 + 5;
     
    int n, k;
    int a[N], cnt[N << 1];
     
    void run() {
        cin >> n >> k;
        for(int i = 1; i <= n; i++) cin >> a[i];
        for(int i = 1; i <= 2 * k; i++) cnt[i] = 0;
        for(int i = 1; i <= n / 2; i++) {
            int x = a[i], y = a[n - i + 1];
            if(x > y) swap(x, y);
            cnt[2] += 2;
            --cnt[x + 1];
            ++cnt[k + 1 + y];
            --cnt[x + y];
            ++cnt[x + y + 1];
        }
        for(int i = 2; i <= 2 * k; i++) cnt[i] += cnt[i - 1];
        int ans = INF;
        for(int i = 2; i <= 2 * k; i++) {
            ans = min(ans, cnt[i]);   
        }
        cout << ans << '
    ';
    }
     
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cout << fixed << setprecision(20);
        int T; cin >> T; while(T--)
        run();
        return 0;
    }
    

    E. Weights Distributing

    题意:
    给出一张(n)个点(m)条边的图,现在给出序列(w_{1,2,...,m})表示边权集合。
    现在给定三个点(a,b,c),要从(a ightarrow b ightarrow c)。现在问如果给图中的边分配边权,这条路径的最小长度为多少。

    思路:
    最直接的想法就是找到一条路径,总长度最小,并且重复的边尽可能多,但是代码实现十分困难。
    其实就可以考虑暴力点的做法:枚举中间点(x),那么规定路径为(a ightarrow x ightarrow b ightarrow x ightarrow c),显然枚举所有的(x)我们必然能找到最优的走法。之后据此贪心选边然后统计答案即可。
    求距离的时候我们要以(a,b,c)三个点作为起点跑一次最短路。
    详见代码:

    Code
    /*
     * Author:  heyuhhh
     * Created Time:  2020/4/21 23:23:10
     */
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <set>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <assert.h>
    #define MP make_pair
    #define fi first
    #define se second
    #define pb push_back
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
    #define INF 0x3f3f3f3f
    #define Local
    #ifdef Local
      #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
      void err() { std::cout << std::endl; }
      template<typename T, typename...Args>
      void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
      template <template<typename...> class T, typename t, typename... A> 
      void err(const T <t> &arg, const A&... args) {
      for (auto &v : arg) std::cout << v << ' '; err(args...); }
    #else
      #define dbg(...)
    #endif
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    //head
    const int N = 2e5 + 5, M = 2e5 + 5;
     
    int n, m, a, b, c;
    int p[N];
    struct Edge{
        int v, w, next;   
    }e[M << 1];
    int dis[N], dis2[N], dis3[N];
    struct Dijkstra{
        struct node{
            ll d, u;
            bool operator < (const node &A) const {
                return d > A.d;
            }   
        };
        int head[N], tot;
        bool vis[N];
        void init() {
            memset(head, -1, sizeof(head)); tot = 0;   
        }
        void adde(int u, int v, int w) {
            e[tot].v = v; e[tot].w = w; e[tot].next = head[u]; head[u] = tot++;   
        }
        void dij(int s) {
            priority_queue <node> q;
            memset(dis, INF, sizeof(dis));
            memset(vis, 0, sizeof(vis));
            dis[s] = 0;
            q.push(node{0, s});
            while(!q.empty()) {
                node cur = q.top(); q.pop();
                int u = cur.u, d = cur.d;
                if(vis[u]) continue;
                vis[u] = 1;
                for(int i = head[u]; i != -1; i = e[i].next) {
                    int v = e[i].v;
                    if(dis[v] > dis[u] + e[i].w) {
                        dis[v] = dis[u] + e[i].w;
                        q.push(node{dis[v], v});   
                    }
                }   
            }
        }
    }solver;
    ll sum[N];
    void run() {
        cin >> n >> m >> a >> b >> c;
        for(int i = 1; i <= m; i++) {
            cin >> p[i];   
        }
        sort(p + 1, p + m + 1);
        for(int i = 1; i <= m; i++) {
            sum[i] = sum[i - 1] + p[i];
        }
        solver.init();
        for(int i = 1; i <= m; i++) {
            int u, v; cin >> u >> v;
            solver.adde(u, v, 1);
            solver.adde(v, u, 1);
        }
        solver.dij(c);
        for(int i = 1; i <= n; i++) dis3[i] = dis[i];
        solver.dij(b);
        for(int i = 1; i <= n; i++) dis2[i] = dis[i];
        solver.dij(a);
        ll ans = 1e18;
        for(int i = 1; i <= n; i++) {
            int len = dis[i] + dis2[i] + dis3[i];
            if(len <= m) ans = min(ans, sum[len] + sum[dis2[i]]);
        }
        cout << ans << '
    ';
    }
     
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cout << fixed << setprecision(20);
        int T; cin >> T;
        while(T--) run();
        return 0;
    }
    

    F. Restore the Permutation by Sorted Segments

    题意:
    现有一排列未知,现在有(n-1)个序列,每个序列选择([2,n])中一点为右端点(不重复),选择一点为左端点构成一段长度大于(1)的区间。之后将每个区间中的数按照从小到大的顺序给出。序列给出的顺序为乱序。
    通过这些求出原排列。

    思路:
    考虑从前往后构造出原排列。
    假设我们知道了第一个位置的元素,那么据此可以求出第二个位置的元素,以此类推就可以求出所有的元素。
    已知第(i)个元素,求第(i+1)个元素的方法:我们考虑所有的序列,如果一个序列中未出现的数的个数为(1)并且已出现的数所在位置为连续的一段:([x,i]),我们此时就可以选定其作为第(i+1)个数。
    这样做的正确性是显然的,如果我们不选这个元素而选其它的元素,那么显然这个元素和序列中其它元素不在一个序列中。
    那么我们直接枚举第一个元素,然后依次找后面的元素即可。如果找到合法的就直接输出。

    PS.其实这个题最容易的想法是从后往前找,因为每次最后一个位置的数只会出现一次,但最前面的数也可能出现一次。这样的话就需要多考虑一些情况来判断当前是否合法,码量会很大。但是如果注意到了这样一个性质就比较容易想到从第一个开始找,就是每次我们开头都是一个长度为(2)的区间,删去第一个后很容易找到下一个,下一个也是一个长度为(2)的区间的开头...然后以此类推。从后往前的话就不具备这样的性质。
    貌似多说了一点。。

    Code
    /*
     * Author:  heyuhhh
     * Created Time:  2020/4/22 10:00:01
     */
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <set>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <assert.h>
    #define MP make_pair
    #define fi first
    #define se second
    #define pb push_back
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
    #define INF 0x3f3f3f3f
    #define Local
    #ifdef Local
      #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
      void err() { std::cout << std::endl; }
      template<typename T, typename...Args>
      void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
      template <template<typename...> class T, typename t, typename... A> 
      void err(const T <t> &arg, const A&... args) {
      for (auto &v : arg) std::cout << v << ' '; err(args...); }
    #else
      #define dbg(...)
    #endif
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    //head
    const int N = 200 + 5;
    
    int n;
    vector <vector<int>> a;
    int res[N], pos[N];
    
    bool chk() {
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j < n; j++) {
                int r = sz(a[j]), x = -1;
                for (auto it : a[j]) {
                    if (!pos[it]) x = it;
                    else if(pos[it] > i - sz(a[j])) --r;
                }
                if (r == 1 && x != -1) {
                    res[i] = x;
                    pos[x] = i;
                    break;
                }
            }
            if (!res[i]) return false;
        }
        return true;
    }
    
    void run() {
        cin >> n;
        a.resize(n + 1);
        for (int i = 1; i < n; i++) {
            int k; cin >> k;
            a[i].resize(k);
            for (int j = 0; j < k; j++) {
                cin >> a[i][j];
            }   
        }
        for (int i = 1; i <= n; i++) {
            memset(res, 0, sizeof(res));
            memset(pos, 0, sizeof(pos));
            res[1] = i;
            pos[i] = 1;
            if (chk()) break;
        }
        for (int i = 1; i <= n; i++) {
            cout << res[i] << " 
    "[i == n];   
        }
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cout << fixed << setprecision(20);
        int T; cin >> T; while(T--)
        run();
        return 0;
    }
    
  • 相关阅读:
    脏数据或者场景考虑不全面引发的生产问题
    框架那些事
    RMI远程方法调用和rpc远程过程调用
    如何提高开发效率
    什么是RPC
    TCP/IP协议和HTTP协议
    apache常见错误:VC运行库(找不到 VCRUNTIME140.dll)
    Apache报错:无法使用可靠的服务器域名
    Apache2.4 下载和安装
    Navicat Premium 15.0.17 破解激活(DFoX 注册机)
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/12753964.html
Copyright © 2020-2023  润新知