【IOI2020国家集训队作业 Part 1】CF505E Mr. Kitayuta vs. Bamboos

题目

题目描述
Mr. Kitayuta’s garden is planted with nn bamboos. (Bamboos are tall, fast-growing tropical plants with hollow stems.) At the moment, the height of the ii -th bamboo is h_{i}h
i

meters, and it grows a_{i}a
i

meters at the end of each day.

Actually, Mr. Kitayuta hates these bamboos. He once attempted to cut them down, but failed because their stems are too hard. Mr. Kitayuta have not given up, however. He has crafted Magical Hammer with his intelligence to drive them into the ground.

He can use Magical Hammer at most kk times during each day, due to his limited Magic Power. Each time he beat a bamboo with Magical Hammer, its height decreases by pp meters. If the height would become negative by this change, it will become 00 meters instead (it does not disappear). In other words, if a bamboo whose height is hh meters is beaten with Magical Hammer, its new height will be max(0,h-p)max(0,h−p) meters. It is possible to beat the same bamboo more than once in a day.

Mr. Kitayuta will fight the bamboos for mm days, starting today. His purpose is to minimize the height of the tallest bamboo after mm days (that is, mm iterations of “Mr. Kitayuta beats the bamboos and then they grow”). Find the lowest possible height of the tallest bamboo after mm days.

输入格式
The first line of the input contains four space-separated integers nn , mm , kk and pp ( 1<=n<=10{5},1<=m<=5000,1<=k<=10,1<=p<=10{9}1<=n<=10
5
,1<=m<=5000,1<=k<=10,1<=p<=10
9
). They represent the number of the bamboos in Mr. Kitayuta’s garden, the duration of Mr. Kitayuta’s fight in days, the maximum number of times that Mr. Kitayuta beat the bamboos during each day, and the power of Magic Hammer, respectively.

The following nn lines describe the properties of the bamboos. The ii -th of them ( 1<=i<=n1<=i<=n ) contains two space-separated integers h_{i}h
i

and a_{i}a
i

( 0<=h_{i}<=10{9},1<=a_{i}<=10{9}0<=h
i

<=10
9
,1<=a
i

<=10
9
), denoting the initial height and the growth rate of the ii -th bamboo, respectively.

输出格式
Print the lowest possible height of the tallest bamboo after mm days.

题意翻译
给定 nn 个数 h_{1 \dots n}h
1…n


你需要进行 mm 轮操作,每轮操作为 kk 次修改,每次修改可以选择一个数 h_ih
i

修改为 \max(h_i - p, 0)max(h
i

−p,0)。
每轮操作后每个 h_ih
i

将会被修改为 h_i + a_ih
i

+a
i


你需要最小化最终 h_{1 \dots n}h
1…n

中的最大值。
n \le 10^5n≤10
5
,m \le 5 \times 10^3m≤5×10
3
,k \le 10k≤10。
输入输出样例
输入 #1复制
3 1 2 5
10 10
10 10
15 2
输出 #1复制
17
输入 #2复制
2 10 10 1000000000
0 10
0 10
输出 #2复制
10
输入 #3复制
5 3 3 10
9 5
9 2
4 7
9 10
3 8
输出 #3复制
14

思路

我们先假设第m天后,自然生长下最高的竹子高度是max_height。那么我们经过处理后的答案一定会在区间[0,max]内。不可能超过我们最高的竹子。

此时我们假定一个最后的答案ans,然后二分查找的方法去找到正确的答案。

如果这个答案是正确的那么第m天的时候所有的竹子高度都会小于等于ans。那么前一天的最高的竹子高度就是ans-a[i]+p*times,times是我们对这个竹子处理的次数。然后再前一天一次类推。我们反向处理我们的竹子。

如果我们无论怎么处理最后这个竹子的高度还是会大于我们的预想答案ans,则说明我们的答案小了,继续在区间(ans,max]寻找。反之则在[0,ans]寻找。

代码

#include<bits/stdc++.h>
using namespace std;

#define int long long

int n,m,k,p;

const int maxn = 1e5 + 5;

int a[maxn],h[maxn];

int he[maxn];

struct bamboo {
    int ne;
    int id;
    bool operator < (const bamboo & x) const {
        return x.ne < ne;
    }//重载运算符
} bam[maxn];

bool check(int x) {
    priority_queue <bamboo> q;
//  memset(he,x,sizeof(he));
    for(int i = 1; i<= n; i++)
        he[i] = x;//初始值
    for(int i = 1; i <= n; i++)
        if(he[i] - a[i] * m < h[i])
            q.push({x / a[i],i});//预处理
    for(int i = 1; i <= m; i++)//枚举m天
        for(int j = 1; j <= k; j++) {//增加k棵竹子高度
            if(!q.size())
                return 1;
            bamboo y = q.top();
            q.pop();
            if(y.ne < i)//无论如何都不满足
                return 0;
            he[y.id] += p;
            if(he[y.id] - a[y.id] * m < h[y.id])//增加后仍不满足就进堆
                q.push({he[y.id] / a[y.id],y.id});
        }
    return q.empty();
}

signed main() {
    scanf("%d%d%d%d",&n,&m,&k,&p);
    for(int i = 1; i <= n; i++)
        scanf("%d%d",&h[i],&a[i]);
    int l = 0,r = 0;
    for(int i = 1; i <= n; i++)
        r = max(r,(h[i] + a[i] * m))while(l < r) {
        int mid = l + r >> 1;
        if(check(mid))
            r = mid;
        else
            l = mid +1;
    }
    cout << l << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值