ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #781 (Div. 2)题解

2022-04-11 18:03:44  阅读:168  来源: 互联网

标签:781 int 题解 节点 maxn test integer Div cases


A. GCD vs LCM

You are given a positive integer nn. You have to find 44 positive integers a,b,c,da,b,c,d such that

  • a+b+c+d=na+b+c+d=n, and
  • gcd(a,b)=lcm(c,d)gcd(a,b)=lcm⁡(c,d).

If there are several possible answers you can output any of them. It is possible to show that the answer always exists.

In this problem gcd(a,b)gcd(a,b) denotes the greatest common divisor of aa and bb, and lcm(c,d)lcm⁡(c,d) denotes the least common multiple of cc and dd.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.

Each test case contains a single line with integer nn (4≤n≤1094≤n≤109) — the sum of aa, bb, cc, and dd.

Output

For each test case output 44 positive integers aa, bb, cc, dd such that a+b+c+d=na+b+c+d=n and gcd(a,b)=lcm(c,d)gcd(a,b)=lcm⁡(c,d).

题目大意:给定一个n,求出四个数a,b,c,d满足a+b+c+d=n并且gcd(a,b)=gcd(c,d)。

思路:将一个数定义为n-3,剩下的三个数为1即可。

 

#include<bits/stdc++.h>
using namespace std;
int t,n;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n;
        cout<<n-3<<' '<<1<<' '<<1<<' '<<1<<'\n';
    }
    return 0;
}

B. Array Cloning Technique

You are given an array aa of nn integers. Initially there is only one copy of the given array.

You can do operations of two types:

  1. Choose any array and clone it. After that there is one more copy of the chosen array.
  2. Swap two elements from any two copies (maybe in the same copy) on any positions.

You need to find the minimal number of operations needed to obtain a copy where all elements are equal.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the length of the array aa.

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the elements of the array aa.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

Output

For each test case output a single integer — the minimal number of operations needed to create at least one copy where all elements are equal.

题目大意:给定一个排列,每次可以进行如下操作:1.复制该排列为一个新的排列。2.将原排列中的任意一个数与复制后的排列中任意一个数互换。

求最少经过几次操做可以实现原排列各个位置的值相同。

思路:每次复制后,取原排列中重复次数最多的数乘2到符合题意为止。

#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[100001];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n;
        int maxn=0;
        map<int,int>mp;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            mp[a[i]]++;
            maxn=max(maxn,mp[a[i]]);
        }
        if(maxn==n){
            cout<<'0'<<'\n';
            continue;
        }
        int ans=1;
        n-=maxn;
        if(n-maxn>=0)
            ans+=maxn;
        else
            ans+=n;
        n-=maxn;
        while(n>0)
        {
            ans++;
            maxn*=2;
            if(n-maxn>=0)
                ans+=maxn;
            else 
                ans+=n;
            n-=maxn;
        }
        cout<<ans<<'\n';
    }
    return 0;
}

 

C. Tree Infection 

A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a vertex vv (different from root) is the previous to vv vertex on the shortest path from the root to the vertex vv. Children of the vertex vv are all vertices for which vv is the parent.

You are given a rooted tree with nn vertices. The vertex 11 is the root. Initially, all vertices are healthy.

Each second you do two operations, the spreading operation and, after that, the injection operation:

  1. Spreading: for each vertex vv, if at least one child of vv is infected, you can spread the disease by infecting at most one other child of vv of your choice.
  2. Injection: you can choose any healthy vertex and infect it.

This process repeats each second until the whole tree is infected. You need to find the minimal number of seconds needed to infect the whole tree.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of the vertices in the given tree.

The second line of each test case contains n−1n−1 integers p2,p3,…,pnp2,p3,…,pn (1≤pi≤n1≤pi≤n), where pipi is the ancestor of the ii-th vertex in the tree.

It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of nn over all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case you should output a single integer — the minimal number of seconds needed to infect the whole tree.

题目大意:给定一棵树,每一秒可以感染一个节点,同时具有相同父亲节点的节点中如果存在已经被感染的节点的话,这几个节点中会有一个节点收到传播。

思路:每一部分具有相同父亲节点的节点都要花费一秒去感染,而对于每一堆有一个受到感染的节点来说其他节点全部感染所需要的最小时间是节点的子节点数 - 传播次序 + 1,知道这些后模拟一边即可。

#include<bits/stdc++.h>
using namespace std;
int t,n;
int a[200002];
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>t;
    while(t--)
    {
        cin>>n;
        memset(a,0,sizeof(a));
        int ans=1;
        for(int i=1;i<n;i++)
        {
            int k;
            cin>>k;
            a[k]++;
        }
        sort(a+1,a+1+n,cmp);
        priority_queue<int,vector<int>,less<int>>q;
        for(int i=1;i<=n;i++)
        {
            if(a[i]==0)
                break;
            ans++;
            q.push(a[i]-1+i);
        }
        while(ans<q.top())
        {
            int k=q.top()-1;
            ans++;
            q.pop();
            q.push(k);
        }
        cout<<ans<<'\n';
    }
    return 0;
}

 

标签:781,int,题解,节点,maxn,test,integer,Div,cases
来源: https://www.cnblogs.com/cbmango/p/16131210.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有