First Direct O (n²) violence. The result is a ... How weak is the usaco data = =
First sort, and then you can think of a few yy ... specifically see Code
-----------------------------------------------------------------------------------------
#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>#define REP (i, n) for (int i = 0; i < n; i++)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const INT MAXN = 10000 + 5;int h[MAXN];int main () {//freopen ("test.in", "R", stdin);int n;cin >> N;Rep (i, N)scanf ("%d", H + i);sort (H, H + N);A long long ans = 0;Rep (i, n-1)ans + = 1LL * (i + 1) * (N-i-1) * (h[i + 1]-h[i]);cout << ans * 2 << "\ n";return 0;}
-----------------------------------------------------------------------------------------
1679: [Usaco2005 jan]moo Volume cow's cries Time Limit:1 Sec Memory Limit:MB
Submit:798 Solved:385
[Submit] [Status] [Discuss] Description
Farmer John had received a noise complaint from he neighbor, Farmer Bob, stating that he cows are making too much noise. FJ ' s n cows (1 <= n <=) all graze at various locations on a long one-dimensional pasture. The cows is very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow are simultaneously mooing at all of the N-1 other Cows). When cow I MOOs at Cow J, the volume of this MOO must is equal to the distance between I and J, in order for J to be able To hear the MOO in all. FJ compute the total volume of sound being generated by all n (N-1) simultaneous mooing sessions.
John's neighbour, Bob, accused John's cattle of being too good to shout. John's N (1≤n≤10000) cows graze in different places on one-dimensional pastures. They are gossip cows, each chatting with the other N-1 cows at the same time. In a conversation, it takes two cows to roar in the same volume as their distance, so there are N (N-1)/2 voices on the pasture. Please calculate the and of these volumes. Input
* Line 1:n * Lines 2..n+1:the location of each cow (in the range 0..1,000,000,000).
Enter N in line 1th, and then enter n integers to indicate where a cow is located.
Output
* Line 1: A single integer With the total volume of the MOOs.
An integer that represents the total volume. Sample Input 5
1
5
3
2
4
INPUT DETAILS:
There is five cows at locations 1, 5, 3, 2, and 4.
Sample Output 40
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) =.HINT
Source
Silver
Bzoj 1679: [Usaco2005 jan]moo Volume Bull's Cry ()