其他分享
首页 > 其他分享> > cf Educational Codeforces Round 61 D. Stressful Training

cf Educational Codeforces Round 61 D. Stressful Training

作者:互联网

原题:
D. Stressful Training
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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⋅10^5) — 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≤10^12) — the initial charge of each student’s laptop.

The third line contains n integers b1,b2,…,bn (1≤bi≤10^7) — 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
input
2 4
3 2
4 2
output
5
input
1 5
4
2
output
1
input
1 6
4
2
output
2
input
2 2
2 10
3 15
output
-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个学生用笔记本电脑参加编程比赛,但是他们都没带电源(笔记本有电池)。老师拿来一个电源,可以给任意一个笔记本电池充电,笔记电池充电没有上限,可以无限充满。比赛时,每个电脑初始有aia_iai​个电量,每一分钟消耗bib_ibi​个电量。电源每次只能给一点电脑充电一分钟,现在问你这个电源最少每分钟给笔记本充多少电可以使得所有学生完成比赛,如果不存在这样的x,输出-1.

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=200001;
ll a[maxn],b[maxn];
int n,k;
 
struct node
{
    ll power,consume,cnt;
    node(ll p,ll co)
    {
        power=p;
        consume=co;
        cnt=p/co;
    }
    bool operator < (const node& rhs) const
    {
        return cnt>rhs.cnt;
    }
 
};
 
bool solve(ll x)
{
    priority_queue<node> Q;
    for(int i=1;i<=n;i++)
        Q.push(node(a[i],b[i]));
    for(int i=1;i<=k;i++)
    {
        node tmp=Q.top();
        Q.pop();
        if(tmp.cnt<i-1)
            return false;
        if(tmp.cnt>=k)
            return true;
        Q.push(node(tmp.power+x,tmp.consume));
    }
    return true;
}
 
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>k)
    {
        ll inf=0;
        for(int i=1;i<=n;i++)
            cin>>a[i];
        for(int i=1;i<=n;i++)
        {
            cin>>b[i];
            inf=max(b[i],inf);
        }
        inf = inf*k;
 
        ll L=0,R=inf,mid;
        while(L<R)
        {
            mid=(L+R)>>1;
            if(solve(mid))
                R=mid;
            else
                L=mid+1;
        }
 
 
        if(R==inf)
            cout<<-1<<endl;
        else
            cout<<R<<endl;
    }
    return 0;
}
 
 
 

解答:

这种问题,再加上数据的大小,肯定会想到用二分来枚举答案。关键在于枚举到电源每分钟可充电量x后,如何判断能否满足所有学生的充电需求。

那么需要先充电的学生肯定的是最先有可能把电量使用完的学生,那么这里建立一个大顶堆,每次将当前学生电源的电量与该学生每分钟消耗电量的比值作为排序规则即可。

标签:Educational,Stressful,power,contest,charger,laptop,Training,charge,minute
来源: https://blog.csdn.net/tengfei461807914/article/details/94355115