SSLOJ 1145 蛋糕店
Description–
最近小G新开了一家蛋糕店。开业第一天,一共来个n位顾客。由于小G非常懒,他每次只会接待一位顾客。每个顾客都想尽快的买到蛋糕,所以没有第一个买到蛋糕的顾客都会有一个愤怒值。最终排在第i个位置的顾客x的愤怒值为i*a[x]。小G想要所有顾客的愤怒值之和最小。求最小的愤怒值之和。
Input–
第一行为一个整数n,表示顾客数。
第二行输入n个整数a[1]…a[n] ,含义见题面
Output–
一行一个整数ans,表示最小的愤怒值之和。
Sample Input–
5
8 5 8 4 6
Sample Output–
51
说明–
样例解释
Ans=81+62+53+44=51
数据规模
对于30%的数据,1 ≤ n ≤ 10。
对于60%的数据,1 ≤ n ≤ 1000。
对于100%的数据,1 ≤ n ≤ 1000000。
解题思路–
要用快读。。。。。。不然就会超时!!!
纯模拟hehehehehehehehehehehehehe~~~
代码–
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
long long t;
long long a[1000005],s,n,x;
int read()
{
x=0;
char c=getchar();
while (c<'0' || c>'9') c=getchar();
while (c>='0' && c<='9')
{
x=x*10+(c-'0');
c=getchar();
}
return x;
}
int main()
{
n=read();//快读
for (int i=1;i<=n;++i)
a[i]=read();//快读
sort(a+1,a+n+1);//快排
for (int i=n-1;i>=1;--i)
{
t=a[i]*(n-i);
s+=t;
}
printf("%lld",s);
return 0;
}