cf665 div2 abcd

传送门

A Distance and Axis standard input/output 1 s, 256 MB Submit Add to favourites x17891

  当n大于等于k时, 假设可以在n内部找到一个这样的点 位置是x 那么 n-x-x=k 也就是 n-k=2x  n与k奇偶性相同则不需移动 否则需要移动1即可 ,  如果k 大于k的话 那么移动k-n即可 k-0=k
B Ternary Sequence standard input/output 2 s, 256 MB Submit Add to favourites x15102

  直接模拟即可,取 2*min(z1,y2)  尽可能将y1和z2变最小
C Mere Array standard input/output 2 s, 256 MB Submit Add to favourites x13454

  因数为m的数可以相互交换,需要得到一个非降序序列   将数组排序后与原数组对比 如果两数组不同的元素不能整除m 则无解
D Maximum Distributed Tree standard input/output 2 s, 256 MB Submit Add to favourites x5441

  需要最大化贡献,每条路的经过次数是固定的 所以可以dfs先把每条路的经过次数算出来,然后从大到小分配p数组即可 经过次数最多的路径分配最大的边权, 而所有路边权的乘积需要为k  则需要考虑如果n-1 大于 m 则剩下的路权为1 ; 如果n-1小于m 则把多出来的p全累乘到p[n-2]上面

 (太难了,写好dfs以后 想直接把小于n-1的p数组填充1完整再排序 然后直接用 _for(i,0,n-1) vec[i]*p[i]  但是这个操作一直wa 而且写起来麻烦没有必要。。。放弃这个操作,真就写代码十分钟 debug十小时)

 

#include <bits/stdc++.h>

using namespace std;

#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)
#define ll long long
#define all(v) (v).begin(), (v).end()

 

 

void taskA() {
    int t; cin >> t;
    while(t--) {
        int n,k; cin >> n >> k;
        if(k > n) cout << k-n << "\n";
        else {
            if(k%2 == n%2) cout << "0\n";
            else cout << "1\n";
        }
    }
    return;
}
View A Code

 

void mis(int& x, int& y) {
    int m = min(x,y);
    x-=m, y-=m;
    return;
}
void taskB() {
    int t; cin >> t;
    while(t--) {
        int x0,x1,x2,y0,y1,y2;
        cin >> x0 >> x1 >> x2 >> y0 >> y1 >> y2;
        mis(x0, y2); mis(y0, x1);
        
        int ans = 2*min(x2,y1);
        mis(x2, y1);
        ans -= 2*min(x1, y2);
        cout << ans << "\n";
    }
    return;
}
View B Code

 

void taskC() {
    int t; cin >> t;
    while(t--) {
        int n; cin >> n;
        vector<int> a(n), b(n);
        int mi = 1e9;
        _for(i,0,n) cin >> a[i], b[i] = a[i], mi = min(mi, a[i]);
        sort(all(b));
        int k = 0;
        _for(i,0,n) { 
            if(a[i] != b[i] and a[i]%mi)
                k = 1;
        }
        cout << (k ? "NO" : "YES") << "\n";
    } return;
}
View C Code

 

const int N = 1e5+10;
const int Mod = 1e9+7;
int sz[N], n;
vector<ll> vec;
vector<int> adj[N];
void dfs(int u, int f) {
    sz[u] = 1;
    for(int v : adj[u]) {
        if(v != f) 
            dfs(v, u), sz[u] += sz[v];
    }   
    if(f) vec.push_back(1LL*sz[u]*(n-sz[u]));
    return;
}

void taskD() {
    int t; cin >> t;
    while(t--) {
        cin >> n;
        vec.clear();
        _rep(i,1,n) adj[i].clear();
        _for(i,1,n) {
            int x, y; cin >> x >> y;
            adj[x].push_back(y);
            adj[y].push_back(x);
        }
        dfs(1, 0);
        sort(all(vec));
        n--;
        int m; cin >> m;
        vector<ll> a(m);
        _for(i,0,m) cin >> a[i];
        sort(all(a));
        while(m > n) a[m-2] = a[m-1]*a[m-2]%Mod, m--;
        
        
        //_for(i,0,n) cout << vec[i] << " "; cout << "vec\n";
        //_for(i,0,n) cout << a[i] << " "; cout << "a\n";
        int ans = 0;
        for(int i = n-1; i >= 0; i--) 
            if(m < 1) ans = (ans+vec[i]%Mod)%Mod;
            else ans = (ans+vec[i]*a[m-1]%Mod)%Mod, m--;
        cout << ans << "\n";
    } 
    return;
}
View D Code

 

int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
   
    taskD();
    return 0;
}

 

posted @ 2020-09-13 04:48  163467  阅读(127)  评论(0编辑  收藏  举报