A. Equalize Prices Again(水题) Codeforces Round #590 (Div. 3)

原题链接:https://codeforces.com/contest/1234/problem/A

派大星既是”比奇堡”一家小商店的店主又是售货员。店里有n种商品,第i种商品价格为ai。
派大星厌倦了在顾客要求时记住每件产品的价格,因此派大星决定简化自己的生活。更确切地说,派大星决定对店里所有的n种商品都定同样的价格。
但是,派大星不想失去任何钱,所以派大星想选择这样的价格,新的价格总和不低于初始价格总和。这意味着,如果派大星以新的价格出售所有n种商品,派大星将获得至少与以初始价格出售相同(或更大)的金额。
另一方面,派大星不想因为价格太高而失去客户,所以在所有的价格中,派大星可以选择自己需要选择的最低价格。所以派大星需要找到所有n种商品的最低可能相等价格,所以如果派大星以这个价格出售它们,派大星将获得至少相同(或更大)的金额,就像派大星以它们的初始价格出售它们一样。
派大星觉得这个问题很麻烦,请求机智的你来帮助他解决这个问题。


Input
输入的第一行包含一个整数q(1≤q≤100)组样例。接下来是q个组样例。
样例的第一行包含一个整数n(1≤n≤100)-货物数量。
样例的第二行包含n个整数a1、a2、…、an(1≤ai≤1e7),其中ai是第i个货物的价格。
Output
对于每一个问题,给定一个价格------所有商品的最低可能相等价格,因此如果派大星以这个价格出售它们,派大星将收到至少相同(或更大)的金额,就好像派大星以最初的价格出售它们一样。


Example
Input
3
5
1 2 3 4 5
3
1 2 2
4
1 1 1 1
Output
3
2
1


题意:派大星有n件商品,且都知道价格,想统一价格,前提是不亏损,求最低的价格是多少?

解题思路:我们发现价格都是整数,我们要求的最低价格也应该是整数,那么我们完全可以利用向上取整的思想来求得这一平均价格。先统计总金额再判断是否可以对总数取模为0,若不行则加一即可。

AC代码:

/*
*邮箱:[email protected]
*blog:https://blog.csdn.net/hzf0701
*注:代码如有问题请私信我或在评论区留言,谢谢支持。
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<cstring>
#include<map>
#include<iterator>
#include<list>
#include<set>
#include<functional>
#include<memory.h>//低版本G++编译器不支持,若使用这种G++编译器此段应注释掉
#include<iomanip>
#include<vector>
#include<cstring>
#define scd(n) scanf("%d",&n)
#define scf(n) scanf("%f",&n)
#define scc(n) scanf("%c",&n)
#define scs(n) scanf("%s",n)
#define prd(n) printf("%d",n)
#define prf(n) printf("%f",n)
#define prc(n) printf("%c",n)
#define prs(n) printf("%s",n)
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为代码自定义代码模板***************************************//

int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	ios::sync_with_stdio(false);//打消iostream中输入输出缓存,节省时间。
	cin.tie(0); cout.tie(0);//可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。
	int q;
	int n;
	int nums[105];
	ll sum;
	while(cin>>q){
        while(q--){
            cin>>n;
            sum=0;
            rep(i,0,n-1){
                cin>>nums[i];
                sum+=nums[i];
            }
            if(sum%n==0)
                cout<<sum/n<<endl;
            else
                cout<<sum/n+1<<endl;
        }
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/107814547