Description
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. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.
FJ's N cows (1 <= N <= 10,000) all graze at various locations on a long one-dimensional pasture. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.
Input
* Line 1: N
* Lines 2..N+1: The location of each cow (in the range 0..1,000,000,000).
* Lines 2..N+1: The location of each cow (in the range 0..1,000,000,000).
Output
There are five cows at locations 1, 5, 3, 2, and 4.
Sample Input
5 1 5 3 2 4
Sample Output
40
Hint
INPUT DETAILS:
There are five cows at locations 1, 5, 3, 2, and 4.
OUTPUT DETAILS:
Cow at 1 contributes 1+2+3+4=10, cow at 5 contributes 4+3+2+1=10, cow at 3 contributes 2+1+1+2=6, cow at 2 contributes 1+1+2+3=7, and cow at 4 contributes 3+2+1+1=7. The total volume is (10+10+6+7+7) = 40.
There are five cows at locations 1, 5, 3, 2, and 4.
OUTPUT DETAILS:
Cow at 1 contributes 1+2+3+4=10, cow at 5 contributes 4+3+2+1=10, cow at 3 contributes 2+1+1+2=6, cow at 2 contributes 1+1+2+3=7, and cow at 4 contributes 3+2+1+1=7. The total volume is (10+10+6+7+7) = 40.
/************************************************************************************************************ 题意:直线上有N头牛,求N头牛的距离之和 例如 1 3 5 则ans = (3-1)+(5-1)+|1-3|+(5-3)+|1-5|+|3-5| 思路:此题直接暴力在POJ上1000MS低空飘过 在AOJ上会TLE 暴力做法不多说 直接循环相加 比较叼的方法是求出每段出现的次数加起来 这我不太好说明 可以在草稿纸上写写 比如 1 3 5 7 9 先算吼声向右传的 首先 1吼一声 然后 1 3段记为d(1,3)出现了一次 d(3,5)出现了一次 d(5,7)出现了一次,以此类推 然后2吼一声 d(3,5)又出现了一次 此时为2次 以此类推 然后3吼一声 继续 推 能发现ans += (a[i] - a[i-1]) * i * (n-i)的规律 下面先附上暴力渣代码 最后是比较好的代码 ************************************************************************************************************/ #include <cstdio> #include <algorithm> #include <cmath> using namespace std; int a[10000+10]; int main() { int n; scanf("%d", &n); for(int i = 0 ; i < n ; i ++) scanf("%d", &a[i]); sort(a , a + n); __int64 ans = 0; for(int i = 0 ; i < n ; i ++) { for(int j = 0 ; j < n ; j ++) ans += abs(a[j] - a[i]); } printf("%I64d ", ans); return 0; } #include <cstdio> #include <algorithm> using namespace std; int a[10000+10]; int main() { int n; scanf("%d", &n); for(int i = 0 ; i < n ; i ++) scanf("%d", &a[i]); sort(a, a + n); __int64 ans = 0; for(int i = 1 ; i < n ; i ++) ans += (__int64)(a[i] - a[i-1]) * i * (n - i); printf("%I64d ", ans * 2); }