奶牛音量问题
发布日期:2021-05-15 01:06:39 浏览次数:27 分类:精选文章

本文共 1874 字,大约阅读时间需要 6 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������d = a[j] - a[j-1]���������������������j*(n - j)*2���������������������������������������������������������

Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too much noise.

FJ���s N cows (1 ��� N ��� 10,000) all graze at various locations on a long one-dimensional pasture. Every pair of cows carries on a conversation simultaneously, resulting in N*(N-1) MOOing sessions. Each MOO's volume is equal to the distance between the two cows for the other to hear it. The task is to compute the total volume of all MOOs generated simultaneously.

Sample Input:513524

���������������������������������������������������������������������������������������������������d������������������j*(N-j)*2���������j������������������������������j������������������������������������������������������������������������������������������������

���������������

  #include 
#include
#include
int main() {
int N;
int a[10010];
std::cin >> N;
for (int i = 0; i < N; ++i) {
std::cin >> a[i];
}
std::sort(a, a + N);
long long total = 0;
for (int j = 1; j < N; ++j) {
int distance = a[j] - a[j-1];
total += distance * j * (N - j) * 2;
}
std::cout << total << std::endl;
}

���������������������������������������������������������������������O(N log N)������������N������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:每周总结
下一篇:欢乐的跳

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年05月02日 10时31分11秒