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(1t5104) the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer n(2n105) the number of players.
The second line of the test case contains a sequence of integers a1,a2,,an(0ai109), where ai 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 105.

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,类似于填数:mxmxmxmx,如果 mxsummx,则一定可以将 填满,即只需要一个球,反之,尽量填数:mxmxmxmx,填不了则还要形成一条链,共 2mxsum 条链,另外考虑全为 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×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 105.

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 (r1,c1) and (r2,c2) 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×4 the manhattan distance between (1,2) and (3,3) is 3 , one of the shortest paths is the following: (1,2)(2,2)(2,3)(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(1nm,nm100000) - 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 ci1,ci2,,cim(1cij100000) 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 个,则其贡献为 (ij)×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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15976097.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(191)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示