CF1234A Equalize Prices
洛谷 CF1234A Equalize Prices Again
题目描述
You are both a shop keeper and a shop assistant at a small nearby shop. You have nn goods, the ii -th good costs a_ia**i coins.
You got tired of remembering the price of each product when customers ask for it, thus you decided to simplify your life. More precisely you decided to set the same price for all nn goods you have.
However, you don't want to lose any money so you want to choose the price in such a way that the sum of new prices is not less than the sum of the initial prices. It means that if you sell all nn goods for the new price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.
On the other hand, you don't want to lose customers because of big prices so among all prices you can choose you need to choose the minimum one.
So you need to find the minimum possible equal price of all nn goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.
You have to answer qq independent queries.
输入格式
The first line of the input contains one integer qq ( 1 \le q \le 1001≤q≤100 ) — the number of queries. Then qq queries follow.
The first line of the query contains one integer nn ( 1 \le n \le 100)1≤n≤100) — the number of goods. The second line of the query contains nn integers a_1, a_2, \dots, a_na1,a2,…,a**n ( 1 \le a_i \le 10^71≤a**i≤107 ), where a_ia**i is the price of the ii -th good.
输出格式
For each query, print the answer for it — the minimum possible equal price of all nn goods so if you sell them for this price, you will receive at least the same (or greater) amount of money as if you sell them for their initial prices.
输入输出样例
输入 #1复制
输出 #1复制
题意翻译:
(版权来自巨佬@littleseven)
题解:
因为要把全部的商品赋上一个值,又想让这些值的和大于等于原来的数列和。要求维护这个答案最小,那么就一定是等于原来的数列和。
试想,假如钱可以有小数,那么这个答案一定是整个数列的平均值。
但是,因为不能有小数,而C++的除法运算又是向下取整。所以我们要在做完一次除法之后进行判断:如果的确是整除,那么直接输出商,否则要输出商加一。
这就是这道比较复杂的题(滑稽)的题解:
Code:
#include<cstdio>
#define ll long long
using namespace std;
ll t,sum,n;
int main()
{
scanf("%lld",&t);
while(t--)
{
sum=0;
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
ll x;
scanf("%lld",&x);
sum+=x;
}
int t=sum/n;
if(t*n==sum)
printf("%d\n",t);
else
printf("%d\n",t+1);
}
return 0;
}