题目链接: https://vjudge.net/problem/Gym-101628J
题目:
Jenny works at BBB (Battery Bank of Brazil). She’s in charge of optimizing the batteries’ displacements, but can’t solve the problem. Knowing you are a programmer, she asked for your help.
At BBB, the batteries are arranged in piles. In a bank, the batteries can’t be discharged. So, when recharging then, the bank spends M energy units, where M is the size of the greatest pile. Each pile initially has ai batteries.
There is a machine the can move a battery from pile i to pile j with cost bi + cj. Jenny has a limited budget of K for moving the batteries and the bank wants the minimum M possible at the end.
Input
The first line of input consists of the integers N and K (1 ≤ N ≤ 105, 1 ≤ K ≤ 1018). The following N lines contain ai, bi and ci (0 ≤ ai, bi, ci ≤ 106).
Output
Print the minumum M possible.
Example
Input
5 20
5 3 8
2 8 3
3 2 4
7 2 1
6 1 1
Output
5
题意: 给你N堆电池,总时间K,每堆电池有三个属性,电池的数量,还有一个bi、ci值,从第i堆拿一个电池放到第j堆,需要花费(bi+cj)时间,让你在K时间内,对电池进行移动,输出的是电池数量最多的那一堆的电池数目(最小值)。
代码:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <string>
#include <stdlib.h>
#include <algorithm>
#include <queue>
#define ll long long
#define P pair<int,int>
using namespace std;
const int maxn=100000+5;
int N;
ll k;
struct Point
{
ll a,b,c;
bool operator <(const Point &rhs)const
{
return c <rhs.c;
}
}p[maxn];
int ok(int h)
{
ll tot=0,sum=0;//tot: 大于平均值的电池数 sum:总花费
for(int i=0; i<N; i++)
{
if(p[i].a<h)
continue;
tot+=p[i].a-h;
sum+=(p[i].a-h)*p[i].b;
}
for(int i=0; i<N&&sum<=k&&tot; i++)
{
if(p[i].a>=h)
continue;
ll num=min(tot,h-p[i].a);
tot-=num;
sum+=num*p[i].c;
}
if(sum<=k&&!tot)
return 1;
return 0;
}
int main()
{
while(scanf("%d %lld",&N, &k)!=EOF)
{
ll l=0,r=0,mid,ans;
for(int i=0; i<N; i++)
{
scanf("%lld %lld %lld",&p[i].a,&p[i].b,&p[i].c);
r=max(r,p[i].a);
l+=p[i].a;
}
sort(p,p+N);
l=(l+N-1)/N; //向上取整
while(l<=r)
{
mid=(l+r)/2;
if(ok(mid))
r=mid-1,ans=mid;
else
l=mid+1;
}
printf("%lld\n",ans);
}
return 0;
}