蛋糕店

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SSLGZ_yyc/article/details/82714355

题目描述

  最近小G新开了一家蛋糕店。开业第一天,一共来个n位顾客。由于小G非常懒,他每次只会接待一位顾客。每个顾客都想尽快的买到蛋糕,所以没有第一个买到蛋糕的顾客都会有一个愤怒值。最终排在第i个位置的顾客x的愤怒值为i*a[x]。小G想要所有顾客的愤怒值之和最小。求最小的愤怒值之和。

输入

第一行为一个整数n,表示顾客数。
第二行输入n个整数a[1]..a[n] ,含义见题面
输出

一行一个整数ans,表示最小的愤怒值之和。
输入样例

5
8 5 8 4 6
输出样例

51
说明

【样例解释】
Ans=8*1+6*2+5*3+4*4=51

【数据规模和约定】
对于30%的数据,1 ≤ n ≤ 10。
对于60%的数据,1 ≤ n ≤ 1000。
对于100%的数据,1 ≤ n ≤ 1000000。
.
.
.
.
.

分析

很明显,这是一道贪心
我们让a值越大的人排得越前就行了
.
.
.
.
.

程序:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
long long a[1000001];

int read()
{
    int w=0;
    char ch=getchar();
    while (ch<'0'||ch>'9') ch=getchar();
    while (ch>='0'&&ch<='9')
    {
      w=w*10+(ch-'0');
      ch=getchar();
    }
    return w;
}


bool cmp(int a,int b)
{
    return a>b;
}

int main()
{
    n=read();
    for (int i=1;i<=n;i++)
        a[i]=read();
    sort(a+1,a+n+1,cmp);
    long long tj=0;
    for (int i=1;i<=n;i++)
        tj+=(i-1)*a[i];
    printf("%lld",tj);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/SSLGZ_yyc/article/details/82714355