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_i個電量,每一分鐘消耗bib_i個電量。電源每次只能給一點電腦充電一分鐘,現在問你這個電源最少每分鐘給筆記本充多少電可以使得所有學生完成比賽,如果不存在這樣的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後,如何判斷能否滿足所有學生的充電需求。

那麼需要先充電的學生肯定的是最先有可能把電量使用完的學生,那麼這裏建立一個大頂堆,每次將當前學生電源的電量與該學生每分鐘消耗電量的比值作爲排序規則即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章