Codeforces 1132 problem D Stressful Training —— 二分+并查集

27 篇文章 1 订阅
24 篇文章 0 订阅

Berland SU holds yet another training contest for its students today. n students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!

Let students be numbered from 1 to n. Laptop of the i-th student has charge ai at the beginning of the contest and it uses bi of charge per minute (i.e. if the laptop has c charge at the beginning of some minute, it becomes c−bi charge at the beginning of the next minute). The whole contest lasts for k minutes.

Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.

Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can’t be changed afterwards. Let the chosen power output be x. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student’s laptops and use it for some integer number of minutes. If the laptop is using bi charge per minute then it will become bi−x per minute while the charger is plugged in. Negative power usage rate means that the laptop’s charge is increasing. The charge of any laptop isn’t limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.

The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn’t matter.

Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.

Input
The first line contains two integers n and k (1≤n≤2⋅105, 1≤k≤2⋅105) — the number of students (and laptops, correspondigly) and the duration of the contest in minutes.

The second line contains n integers a1,a2,…,an (1≤ai≤1012) — the initial charge of each student’s laptop.

The third line contains n integers b1,b2,…,bn (1≤bi≤107) — the power usage of each student’s laptop.

Output
Print a single non-negative integer — the minimal possible power output the charger should have so that all the students are able to successfully finish the contest.

If no such charger exists, print -1.

Examples
inputCopy
2 4
3 2
4 2
outputCopy
5
inputCopy
1 5
4
2
outputCopy
1
inputCopy
1 6
4
2
outputCopy
2
inputCopy
2 2
2 10
3 15
outputCopy
-1
Note
Let’s take a look at the state of laptops in the beginning of each minute on the first example with the charger of power 5:

charge: [3,2], plug the charger into laptop 1;
charge: [3−4+5,2−2]=[4,0], plug the charger into laptop 2;
charge: [4−4,0−2+5]=[0,3], plug the charger into laptop 1;
charge: [0−4+5,3−2]=[1,1].
The contest ends after the fourth minute.

However, let’s consider the charger of power 4:

charge: [3,2], plug the charger into laptop 1;
charge: [3−4+4,2−2]=[3,0], plug the charger into laptop 2;
charge: [3−4,0−2+4]=[−1,2], the first laptop has negative charge, thus, the first student doesn’t finish the contest.
In the fourth example no matter how powerful the charger is, one of the students won’t finish the contest.

题意:

有n个学生,每个人有一台电脑,每台电脑起始电量为ai,每分钟消耗bi电量。你有一个充电宝,每次可以给一个学生充电,可以冲整数分钟,充电和耗电都是在每一分钟的开头进行的,你要确保每个学生都有>=0的电量,考试结束的时候不算。问你你输出的电量最少是每分钟多少,不存在输出-1.

题解:

最大值最小,,二分。至于check,我一开始是想用枚举分钟,用数据结构维护当前最小电量的学生,但是可能会有点难做,至少我一下子想不出有什么方法,那我就想到了我们可以维护每个学生需要充电的最晚时间点,由于只有2e5个点,所以不会t。如果这个时间点被占用了,那么就去找小于这个时间的最大没被占用时间点,如果for一遍一定会t,那么我们可以用并查集维护每个时间点之前的最大未被占用时间点是多少即可。(上一道题目1000人做了半天,这个100多的十几分钟就做出来了,所以怎么说呢,还是看运气)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
ll n,t;
struct node
{
    ll a,b;
}p[N];
int vis[N];
int finds(int x)
{
    return x==vis[x]?x:vis[x]=finds(vis[x]);
}
bool check(ll x)
{
    for(int i=1;i<=t;i++)
        vis[i]=i;

    for(int i=1;i<=n;i++)
    {
        for(ll j=0;;j++)
        {
            if((p[i].a+j*x)/p[i].b+1>=t)
                break;
            int tim=(p[i].a+j*x)/p[i].b;
            int f=finds(tim+1);
            if(f==0)
                return 0;
            vis[f]=f-1;
        }
    }
    return 1;
}
int main()
{
    scanf("%lld%lld",&n,&t);
    for(int i=1;i<=n;i++)
        scanf("%lld",&p[i].a);
    for(int i=1;i<=n;i++)
        scanf("%lld",&p[i].b);
    ll low=0,high=1e18,ans=0,mid;
    while(high>=low)
    {
        mid=(high+low)/2;
        //if(mid<100)
            //printf("1\n");
        if(check(mid))
            high=mid-1,ans=mid;
        else
            low=mid+1;
    }
    if(check(ans)==0)
        printf("-1\n");
    else
        printf("%lld\n",ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值