gpt4 book ai didi

c - "Tournament"的更好算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:58:54 25 4
gpt4 key购买 nike

<分区>

我有一个问题。几天以来我一直在为 ZCO 做准备,我遇到了一个非常简单的问题,我一直无法在 3 秒的时间限制内解决。问题来了:

N teams participate in a league cricket tournament on Mars, where each pair of distinct teams plays each other exactly once. Thus, there are a total of (N × (N­1))/2 matches. An expert has assigned a strength to each team, a positive integer. Strangely, the Martian crowds love one­sided matches and the advertising revenue earned from a match is the absolute value of the difference between the strengths of the two matches. Given the strengths of the N teams, find the total advertising revenue earned from all the matches.

For example, suppose N is 4 and the team strengths for teams 1, 2, 3, and 4 are 3, 10, 3, and 5 respectively. Then the advertising revenues from the 6 matches are as follows:

7, 0, 2, 7, 5, 2

Thus the total advertising revenue is 23.

Sample input 4 3 10 3 5 Sample output 23 Test data In all subtasks, the strength of each team is an integer between 1 and 1,000 inclusive.

Subtask 1 (30 marks) : 2 ≤ N ≤ 1,000.
Subtask 2 (70 marks) : 2 ≤ N ≤ 200,000.

Limits

Time limit : 3s

Memory limit: 64 MB

因此,我选择了一种简单的算法来扫描并找到每个团队与之前所有其他团队对应的广告收入。这相当于未能通过子任务 2 的 O(n^2)。我认为不可能对此进行改进,有人可以帮助我吗?

附言虽然没有帮助,但这是我当前的 C 代码:

#include <stdio.h>

int main(void)
{
long long int n, i, j;
scanf("%lld", &n);
long long int A[n], strength = 0;
for(i = 0;i < n;i++)
{
scanf("%lld", &A[i]);
for(j = i;j >= 0;j--)
{
strength += A[i] > A[j] ? A[i] - A[j] : A[j] - A[i];
}
}
printf("%lld\n", strength);
return 0;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com