Jenny and the Batteries Gym - 101628J (二分)

题目链接: 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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值