
本文共 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������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
