Codeforces Round #775

题目链接

Codeforces Round #775

B. Game of Ball Passing

Daniel is watching a football team playing a game during their training session. They want to improve their passing skills during that session.
The game involves \(n\) players, making multiple passes towards each other. Unfortunately, since the balls were moving too fast, after the session Daniel is unable to know how many balls were involved during the game. The only thing he knows is the number of passes delivered by each player during all the session.
Find the minimum possible amount of balls that were involved in the game.

Input

There are several test cases in the input data. The first line contains a single integer \(t\left(1 \leq t \leq 5 \cdot 10^{4}\right)-\) the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer \(n\left(2 \leq n \leq 10^{5}\right)-\) the number of players.
The second line of the test case contains a sequence of integers \(a_{1}, a_{2}, \ldots, a_{n}\left(0 \leq a_{i} \leq 10^{9}\right)\), where \(a_{i}\) is the number of passes delivered by the \(i\)-th player.
It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^{5}\).

Output

For each test case print a single integer - the answer to the problem.

Example

input

4
4
2 3 3 2
3
1 5 2
2
0 0
4
1000000000 1000000000 1000000000 1000000000

output

1
2
0
1

解题思路

思维

考虑传球最多的次数 \(mx\),类似于填数:\(——mx——mx——mx—— \dots ——mx\),如果 \(mx\leq sum-mx\),则一定可以将 \(——\)填满,即只需要一个球,反之,尽量填数:\(mx——mx——mx—— \dots ——mx\),填不了则还要形成一条链,共 \(2mx-sum\) 条链,另外考虑全为 \(0\) 的特殊情况

  • 时间复杂度:\(O(T)\)

代码

// Problem: B. Game of Ball Passing
// Contest: Codeforces - Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)
// URL: https://codeforces.com/contest/1649/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}


int main()
{
    int t,n;
    for(cin>>t;t;t--)
    {
    	cin>>n;
    	LL mx=0,s=0;
    	while(n--)
    	{
    		LL x;
    		cin>>x;
    		chkMax(mx,x);
    		s+=x;
    	}
    	cout<<(mx?max(1ll,2*mx-s):0)<<'\n';
    }
    return 0;
}

C. Weird Sum

Egor has a table of size \(n \times m\), with lines numbered from 1 to \(n\) and columns numbered from 1 to \(m\). Each cell has a color that can be presented as an integer from 1 to \(10^{5}\).

Let us denote the cell that lies in the intersection of the \(r\)-th row and the \(c\)-th column as \((r, c)\). We define the manhattan distance between two cells \(\left(r_{1}, c_{1}\right)\) and \(\left(r_{2}, c_{2}\right)\) as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table \(3 \times 4\) the manhattan distance between \((1,2)\) and \((3,3)\) is 3 , one of the shortest paths is the following: \((1,2) \rightarrow(2,2) \rightarrow(2,3) \rightarrow(3,3)\).

Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.

Input

The first line contains two integers \(n\) and \(m(1 \leq n \leq m, n \cdot m \leq 100000)\) - number of rows and columns in the table.
Each of next \(n\) lines describes a row of the table. The \(i\)-th line contains \(m\) integers \(c_{i 1}, c_{i 2}, \ldots, c_{i m}\left(1 \leq c_{i j} \leq 100000\right)-\) colors of cells in the \(i\)-th row.

Output

Print one integer - the the sum of manhattan distances between each pair of cells of the same color.

input

2 3
1 2 3
3 2 1

output

7

input

3 4
1 1 2 2
2 1 1 2
2 2 1 1

output

76

input

4 4
1 1 2 3
2 1 1 2
3 1 2 1
1 1 2 1

output

129

解题思路

思维

考虑每个点的贡献,且 \(x,y\) 是独立的,互不影响,两者可以单独考虑,将颜色分开,对于一种颜色,以横坐标为例:如果前面有 \(i\) 个,后面有 \(j\) 个,则其贡献为 \((i-j)\times x\),当然可以掉优化排序

  • 时间复杂度:\(O(nm)\)

代码

  • 未优化
// Problem: C. Weird Sum
// Contest: Codeforces - Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)
// URL: https://codeforces.com/contest/1649/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=100005;
vector<int> x[N],y[N];
int n,m;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=m;j++)
    	{
    		int w;
    		cin>>w;
    		x[w].pb(i);
    		y[w].pb(j);
    	}
    LL res=0;
    for(int i=1;i<N;i++)
    {
    	int s=x[i].size();
    	if(s)
    	{
    		sort(x[i].begin(),x[i].end());
    		sort(y[i].begin(),y[i].end());
    		for(int j=0;j<s;j++)res+=1ll*(2*j+1-s)*(x[i][j]+y[i][j]);
    	}
    }
    cout<<res;
    return 0;
}
  • 优化
// Problem: C. Weird Sum
// Contest: Codeforces - Codeforces Round #775 (Div. 2, based on Moscow Open Olympiad in Informatics)
// URL: https://codeforces.com/contest/1649/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1e5+5;
LL l1[N],l2[N],r1[N],r2[N],n,m,x;
int main()
{
	LL res=0;
    cin>>n>>m;
    vector<vector<int>> a(n+1);
    for(int i=1;i<=n;i++)a[i].resize(m+1);
    for(int i=1;i<=n;i++)
    	for(int j=1;j<=m;j++)
    	{
    		cin>>x;
    		a[i][j]=x;
    		res+=1ll*l1[x]*i-r1[x];
    		r1[x]+=i;
    		l1[x]++;
    	}
    for(int j=1;j<=m;j++)
    	for(int i=1;i<=n;i++)
    	{
    		x=a[i][j];
    		res+=1ll*l2[x]*j-r2[x];
    		r2[x]+=j;
    		l2[x]++;
    	}
    cout<<res;
    return 0;
}
posted @ 2022-03-07 15:20  zyy2001  阅读(185)  评论(0编辑  收藏  举报