Codeforces Round #665 (Div. 2)

Codeforces Round #665 (Div. 2)

A - Distance and Axis

#include 
using namespace std;
typedef long long ll;
const int MAX = 2e5 + 10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;

void solve() {
    int n,k;
    cin>>n>>k;
    k>n?cout<<k-n<<endl:cout<<((n+k)%2?1:0)<<endl;
}

int main() {
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

B - Ternary Sequence

#include 
using namespace std;
typedef long long ll;
const int MAX = 3e5 + 10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;


void solve() {
    int x,y,z,a,b,c;
    cin>>x>>y>>z>>a>>b>>c;
    int t=min(z,b);
    z-=t;
    if (c>x+z)t-=c-x-z;
    cout<<2*t<<endl;
}

int main() {
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

C - Mere Array

#include 
using namespace std;
typedef long long ll;
const int MAX = 1e5 + 10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;


void solve() {
    int n,i,a[MAX],b[MAX],flag=1;
    cin>>n;
    for (i = 1; i <= n; ++i) {
        cin>>a[i],b[i]=a[i];
    }
    sort(b+1,b+1+n);
    for (int j = 1; j <=n; ++j) {
        if (b[j]%b[1]!=0&&b[j]!=a[j]) flag=0;
    }
    cout<<(flag?"YES":"NO")<<endl;
}

int main() {
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

D - Maximum Distributed Tree

#include 
using namespace std;
typedef long long ll;
const int MAX = 1e5 + 10;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;

ll n,m,start;
vector<vector<ll>>to(MAX);
deque<ll>k;
vector<ll>sun;
deque<ll>edgenum;

void dfs(ll now,ll pre){
    for(auto v:to[now])
        if (v!=pre) dfs(v,now),sun[now]+=sun[v];
    sun[now]++;
    if (now!=start) edgenum.push_back((n-sun[now])*sun[now]);
}

void solve() {
    cin>>n;
    for(ll i=1;i<=n;i++)to[i].clear();
    for(ll i=1;i<n;i++){
        ll u,v;
        cin>>u>>v;
        to[u].push_back(v);
        to[v].push_back(u);
    }
    cin>>m;
    k.resize(m);
    for(ll i=0;i<m;i++) cin>>k[i];
    sort(k.begin(),k.end());
    for(ll i=1;i<=n;i++) if(to[i].size()==1) start=i;

    sun.clear(),sun.resize(n+1,0),edgenum.clear();
    dfs(start,-1);
    sort(edgenum.begin(),edgenum.end());
    while (m>edgenum.size())
        k[m-2]=(k[m-2]*k[m-1]%mod),k.pop_back(),m--;
    while (m<edgenum.size()) k.push_front(1),m++;

    ll ans=0;
    for(ll i=0;i<m;i++)
        ans=(ans+edgenum[i]%mod*k[i]%mod)%mod;
    cout<<ans<<endl;
}

int main() {
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

你可能感兴趣的:(C++,acm,cf,算法,acm竞赛,c++)