B - Moo Volume

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.

Input

* Line 1: N

* 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.

贴两种方法:

第一种:

就是找规律,这个特别难想。

先对a数组进行排序,然后求出相邻的差分

之后就根据n头牛,和第i头牛直接差分用的次数找到规律,直接计算。

第二种,比较简单

就是也要排序

然后第i头牛的音量就是第i-1头牛再加上(i-1-1)*d  (d是i和i-1的距离) 再减去 (n-i)*d

其实每头之间牛音量的不同就在于他们的距离,所以对他们的距离进行处理即可。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;//要找规律!!!
ll a[11000];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
    sort(a,a+n);
    ll sum=0;
    for(int i=1;i<n;i++)
    {
        sum+=(a[i]-a[i-1])*i*(n-i)*2;
    }
    cout<<sum<<endl;
    return 0;
}

  

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;typedef long long ll;ll a[11000],b[11000];int main(){    int n;    cin>>n;    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);    sort(a+1,a+n+1);    for(int i=2;i<=n;i++)    {        b[1]+=abs(a[i]-a[1]);    }    ll sum=b[1];    for(int i=2;i<=n;i++)    {        ll d=a[i]-a[i-1];        b[i]=b[i-1]+(i-1-1)*d-(n-i)*d;        sum+=b[i];    }    cout<<sum<<endl;    return 0;}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10357570.html

时间: 2024-10-13 01:28:09

B - Moo Volume的相关文章

[BZOJ1679][Usaco2005 Jan]Moo Volume 牛的呼声

1679: [Usaco2005 Jan]Moo Volume 牛的呼声 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1097  Solved: 571 [Submit][Status][Discuss] Description Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too

HDU 3466 Moo Volume

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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

BZOJ 1679 [Usaco2005 Jan]Moo Volume 牛的呼声

解法1: N^2的暴力程序,卡卡常数就过了. #include <cstdio> #include <algorithm> #include <cmath> int n; int a[10005]; long long tot; inline bool Read(int &ret){ char c; int flag = 1; if(c=getchar(),c==EOF) return 0; while(c!='-' && (c<'0'||

POJ 2231 Moo Volume

这题用的是贪心算法来算的,贪心策略如下: dist数组表示各个牛的位置距第一个牛的距离,当然之前要对牛的位置进行升序排序. 设a为第一头牛距各头牛的距离的总和,然后从第二头牛开始遍历,假设遍历到第i头牛时,那么标准为: t=t-(n-i)*d+i*d, 各变量的含义为:t一开始为a,然后每次就是该表达式运算之后的值: n为牛的总数: d为第i头牛和第i-1头牛之间的距离.这个公式画图其实很好理解的. 把所有的t值加起来就是最后的结果. #include<cstdio> #include<

贪心/poj 2231 Moo Volume

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; long long a[10010]; int main() { int n; scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%lld",&a[i]); sort(a+1,a+n+1); long long ans

Poj_2232 Moo Volume

题目链接:http://poj.org/problem?id=2231 思路: 先排序,再推导计算公式. 代码: #include <iostream> #include <algorithm> using namespace std; int main() { long long int ans = 0, arr[10010]; int n, m; cin >> n; for ( int i = 0; i < n; ++i ) { cin >> ar

poj 杂题 - 2231 Moo Volume

一开始以为是long long输入,后来发现用int就行,,,反正也是一个挺简单的题目,直接贴代码了. #include<stdio.h> #include<math.h> int cows[10005]={0}; int main(){ int n,i,j; long long res = 0,temp; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&cows[i]);

[BZOJ1657] [Usaco2006 Mar] Mooo 奶牛的歌声 (单调栈)

Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the range 1..2,000,000,000 nanometers (FJ really is a stickler for precision). Each cow moos at some volume v in th

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY