# Codeforces Round #415 (Div. 2)

A. Straight «A»

题目

有n个数,现在给定一个数k。要求至少增加m个k,使得这(n+m)个数的平均值为k(四舍五入到个位),问m的最小值是多少。

题解

一个for循环就行了。

代码

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

void exec(int n, int k){
	int sum = 0, m = 0;
	for(int i = 0, t; i < n; i++){
		cin>>t;
		sum+=t;
	}	
	while(2*(sum+m*k)<(2*k-1)*(n+m)){
		m++;
	}
	cout<<m<<endl;
}

int main(){
	for(int n, k;cin>>n>>k;exec(n,k));
}

B. Summer sell-off

题目

商店要开门n天,其中有f天要求能卖多少卖多少。
  接着给出n行,给出每天的商品数和来商店的人数(每人只能买一件商品)。
  我们任选f天,这f天每天的商品量都增加一倍。
  问我们选哪f天,使得总销售量最大,并且输出总销售量。

题解

贪心。首先求出n天的销售量,然后再求一下每天的增加量,最后由大到小排个序即可。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

#define INIT(x) memset(x,0,sizeof(x))
#define eps 1e-8
#define next next_
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 200005;
const int N = 105;

inline void read(ll &x) {
    ll f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}

ll n,k;
ll a[maxn],b[maxn],c[maxn],ans;

int main()
{
	cin>>n>>k;
	for(int i=0;i<n;i++) {
		read(a[i]),read(b[i]);
		ans += min(a[i],b[i]);
	}
	for(int i=0;i<n;i++) {
		ll x = min(a[i],b[i]);
		ll temp = 2*a[i];
		c[i] = min(temp,b[i]);
		c[i] -= x;
	}
	sort(c,c+n,greater<long long>());
	for(int i=0;i<k;i++) {
		ans += c[i];
	}
	cout<<ans<<endl;
	return 0;
}

C. Do you want a date?

题目

给定n个数,对于这n个数组成的集合的每一个自己,用s来表示该子集最大值和最小值的差,要求所有子集的s的和。

题解

数学规律题。
  举个例子,对于1,2,4,7;
  它的每个子集都会用到1(2-1),2(4-2),3(7-4)这三个差值,难点在于求要用到多少次,这个也不难推出来。具体的细节看代码即可。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
using namespace std;

#define INIT(x) memset(x,0,sizeof(x))
#define eps 1e-8
#define next next_
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x7fffffff;
const int inf = 0x3f3f3f3f;
const int maxn = 300005;
const int N = 105;
const ll mo = 1e9+7;

inline void read(ll &x) {
    ll f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}

ll n,a[maxn],ans;
ll s[maxn],f[maxn];

ll pow(ll a,ll b,ll p) {
	ll res = 1%p;
	for(;b;b>>=1) {
		if(b&1)	res = res * a % p;
		a = a * a % p;
	}
	return (res+p-1)%p;
}

int main()
{
	read(n);
	for(int i=1;i<=n;i++)	{
		read(a[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=n;i++) {
		s[i] = pow(2,n-i,mo);
		f[i] = (s[i]+f[i-1]-(pow(2,i-1,mo))+mo)%mo;
		if(f[i]<0) {
			f[i]+=mo;		
		}
	}
	for(int i=2;i<=n;i++) {
		ans = (ans+f[i-1]*(a[i]-a[i-1]))%mo;
	}		
	cout<<ans<<endl;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

总想玩世不恭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值