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 >> arr[i];
    }

    sort( arr, arr + n );

    for ( int i = 0; i < n; ++i )
    {
        ans += ( n-1-i ) * ( arr[n-1-i] - arr[i] );
    }
    cout << ans * 2 <<endl;

    return 0;
}
时间: 2024-10-25 18:31:38

Poj_2232 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

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 anim

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 杂题 - 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