Codeforces Round #722 (Div. 2) 20210525

Posted 晁棠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #722 (Div. 2) 20210525相关的知识,希望对你有一定的参考价值。

A. Eshag Loves Big Arrays

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Eshag has an array a consisting of n integers.

Eshag can perform the following operation any number of times: choose some subsequence of a and delete every element from it which is strictly larger than AVG, where AVG is the average of the numbers in the chosen subsequence.

For example, if a=[1,4,3,2,4] and Eshag applies the operation to the subsequence containing a1, a2, a4 and a5, then he will delete those of these 4 elements which are larger than a1+a2+a4+a54=114, so after the operation, the array a will become a=[1,3,2].

Your task is to find the maximum number of elements Eshag can delete from the array a by applying the operation described above some number (maybe, zero) times.

A sequence b is a subsequence of an array c if b can be obtained from c by deletion of several (possibly, zero or all) elements.

Input
The first line contains an integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains an integer n (1≤n≤100) — the length of the array a.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤100) — the elements of the array a.

Output
For each test case print a single integer — the maximum number of elements Eshag can delete from the array a.

Example
inputCopy
3
6
1 1 1 2 2 3
6
9 9 9 9 9 9
6
6 4 1 1 4 1
outputCopy
3
0
3
Note
Consider the first test case.

Initially a=[1,1,1,2,2,3].

In the first operation, Eshag can choose the subsequence containing a1, a5 and a6, their average is equal to a1+a5+a63=63=2. So a6 will be deleted.

After this a=[1,1,1,2,2].

In the second operation, Eshag can choose the subsequence containing the whole array a, the average of all its elements is equal to 75. So a4 and a5 will be deleted.

After this a=[1,1,1].

In the second test case, Eshag can’t delete any element.

思路

可以任意选子串,那么只选两个数,一个为最小的数,另一个为其他数,每次相加求平均值,绝对能够去掉两者间大的那个数,即每次最少能删一次。所以直接统计非最小的数有多少个就是最多的删除次数。

#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <cmath>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <queue>
#include <list>
#include <stack>

using namespace std;

int T;

void ready()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin>>T;
}

int n;
int a[105];

void work()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	  cin>>a[i];
	sort(a+1,a+n+1);
	int j=1;
	while(a[j]==a[j+1] && j+1<=n)
	  j++;
	cout<<n-j<<'\\n';
}

int main()
{
    ready();
    while(T--)
      work();
     return 0;
}

B. Sifid and Strange Subsequences

A sequence (b1,b2,…,bk) is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it’s strange if for every pair (i,j) with 1≤i<j≤k, we have |ai−aj|≥MAX, where MAX is the largest element of the sequence. In particular, any sequence of length at most 1 is strange.

For example, the sequences (−2021,−1,−1,−1) and (−1,0,1) are strange, but (3,0,1) is not, because |0−1|<3.

Sifid has an array a of n integers. Sifid likes everything big, so among all the strange subsequences of a, he wants to find the length of the longest one. Can you help him?

A sequence c is a subsequence of an array d if c can be obtained from d by deletion of several (possibly, zero or all) elements.

Input
The first line contains an integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.

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

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

It is guaranteed that the sum of n over all test cases doesn’t exceed 105.

Output
For each test case output a single integer — the length of the longest strange subsequence of a.

Example
inputCopy
6
4
-1 -2 0 0
7
-3 4 -2 0 -4 6 1
5
0 5 -3 2 -5
3
2 3 1
4
-3 0 2 0
6
-3 -2 -1 1 1 1
outputCopy
4
5
4
1
3
4
Note
In the first test case, one of the longest strange subsequences is (a1,a2,a3,a4)
In the second test case, one of the longest strange subsequences is (a1,a3,a4,a5,a7).

In the third test case, one of the longest strange subsequences is (a1,a3,a4,a5).

In the fourth test case, one of the longest strange subsequences is (a2).

In the fifth test case, one of the longest strange subsequences is (a1,a2,a4).

题解
排序后,从小到大找,每次增加多一个数进集合内。保留子串内两者相减的绝对值的最小值,一旦出现了一个元素比它大,则表示后续的都会比它大,后续的就不用再进行操作了。

#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <cmath>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <queue>
#include <list>
#include <stack>

using namespace std;

int T;

void ready()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin>>T;
}

int n;
int a[100005];
int d,ans;

void work()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	  cin>>a[i];
	sort(a+1,a+n+1);
	ans=1;
	d=2000000001;
	for(int i=2;i<=n;i++)
	{
		d=min(d,abs(a[i]-a[i-1]));
		if(d>=a[i])
		  ans++;
		else
		  break;
	}
	cout<<ans<<'\\n';
}

int main()
{
    ready();
    while(T--)
      work();
     return 0;
}

C. Parsa’s Humongous Tree

Parsa has a humongous tree on n vertices.

On each vertex v he has written two integers lv and rv.

To make Parsa’s tree look even more majestic, Nima wants to assign a number av (lv≤av≤rv) to each vertex v such that the beauty of Parsa’s tree is maximized.

Nima’s sense of the beauty is rather bizarre. He defines the beauty of the tree as the sum of |au−av| over all edges (u,v) of the tree.

Since Parsa’s tree is too large, Nima can’t maximize its beauty on his own. Your task is to find the maximum possible beauty for Parsa’s tree.

Input
The first line contains an integer t (1≤t≤250) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (2≤n≤105) — the number of vertices in Parsa’s tree.

The i-th of the following n lines contains two integers li and ri (1≤li≤ri≤109).

Each of the next n−1 lines contains two integers u and v (1≤u,v≤n,u≠v) meaning that there is an edge between the vertices u and v in Parsa’s tree.

It is guaranteed that the given graph is a tree.

It is guaranteed that the sum of n over all test cases doesn’t exceed 2⋅105.

Output
For each test case print the maximum possible beauty for Parsa’s tree.

Example
inputCopy
3
2
1 6
3 8
1 2
3
1 3
4 6
7 9
1 2
2 3
6
3 14
12 20
12 19
2 12
10 17
3 17
3 2
6 5
1 5
2 6
4 6
outputCopy
7
8
62
Note
The trees in the example:

In the first test case, one possible assignment is a={1,8} which results in |1−8|=7.

In the second test case, one of the possible assignments is a={1,5,9} which results in a beauty of |1−5|+|5−9|=8

题解
树形DP
状态转移式:
dp[u][0]+=max(dp[v][0]+llabs(l[u]-l[v]),dp[v][1]+llabs(l[u]-r[v]));
dp[u][1]+=max(dp[v][0]+llabs(r[u]-l[v]),dp[v][1]+llabs(r[u]-r[v]));

dp[i][0]为第i个点选择最小值去和其他点相连
dp[i][1]为第i个点选择最大值去和其他点相连

通过DFS遍历树,返回时再进行DP。

咩哥说树题基本不会出现dfs下去爆栈的情况

#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <stdlib.h>
#include <cstdlib>
#include <math.h>
#include <cmath>
#include <string>
#include <string.h>
#include <cstring>
#include <vector>
#include <queue>
#include <list>
#include <stack>

using namespace std;

int T;
vector<int> m[100005];
void ready()
{
    ios::sync_with_stdio(false),cin.tie(0);
    cin>>T;
}

int n; 
int d,ans;
long long l[100005],r[100005],dp[100005][2];
bool vis[100005];

void dfs(int u)
{
	for(int i=0;i<m[u].size();i++)
	{
		int v=m[u][i];
		if(!vis[v])
		{
			vis[v]=true;
			dfs(v);
			dp[u][0]+=max(dp[v][0]+llabs(l[u]-l[v]),dp[v][1]+llabs(l[u]-r[v]));
			dp[u][1]+=max(dp[v][0]+llabs(r[u]-l[v]),dp[v][1]+llabs(r[u]-r[v]));
		}
	}
}

void work()
{
	cin>>n;
	memset(dp,0,sizeof(dp));
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)
	{
		cin>>l[i]>>r[i];
		m[i].clear();
	}
	for(int i=1;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		m[u].push_back(v);
		m[v].push_back(u);
	}
	vis[1]=true;
	dfs(1);
	cout<<max(dp[1][0],dp[1][1])<<'\\n';
}

int main()
{
    ready();
    while(T--)
      work();
     return 0;
}

以上是关于Codeforces Round #722 (Div. 2) 20210525的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #722 (Div. 2) 20210525

Codeforces Round #722 (Div. 2)Codeforces-1529 ABC

Codeforces Round #722 (Div. 2)Codeforces-1529 ABCD

Codeforces Round #722 (Div. 2)Codeforces-1529 ABCD

Codeforces Round #722 (Div. 2) 部分题解

Codeforces Round #722 (Div. 2) A~D 题解