【每日一题】Present 题解

Present

https://ac.nowcoder.com/acm/problem/110615

Description

Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on his windowsill and started waiting for them to grow. However, after some time the beaver noticed that the flowers stopped growing. The beaver thinks it is bad manners to present little flowers. So he decided to come up with some solutions.

There are m days left to the birthday. The height of the i-th flower (assume that the flowers in the row are numbered from 1 to n from left to right) is equal to ai at the moment. At each of the remaining m days the beaver can take a special watering and water w contiguous flowers (he can do that only once at a day). At that each watered flower grows by one height unit on that day. The beaver wants the height of the smallest flower be as large as possible in the end. What maximum height of the smallest flower can he get?

Solution

看加粗的部分,要使得最小高度的花的高度最大,经典的二分答案套路题。
每次二分答案,从左往右扫描,但目前高度小于答案时,必须要做一次区间加的操作。
区间加的操作可以用数据结构维护一下,我用的时差分数组。

Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5; 
ll a[N], b[N];
ll n, m, w;
bool check(ll x) {
    ll res = 0;
    memset(b, 0, sizeof(b));
    for(int i = 1; i <= n; i++) {
        b[i] = b[i - 1] + b[i];
        if(a[i] + b[i] < x) {
            ll num = x - (a[i] + b[i]);
            res += num; b[i] += num;
            if(i + w <= n) b[i + w] -= num;
            if(res > m) return false;
        }
    }
    return res <= m;
}
int main() {
    cin >> n >> m >> w;
    for(int i = 1; i <= n; i++) cin >> a[i];
    ll left = 1, right = 2e9;
    int ans = -1;
    while(left <= right) {
        ll mid = left + right >> 1;
        if(check(mid)) {
            ans = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    cout << ans << "\n";
    return 0;
}
Kurisu与牛客的每日一题 文章被收录于专栏

每日一题

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
9396次浏览 87人参与
# 你的实习产出是真实的还是包装的? #
1708次浏览 40人参与
# MiniMax求职进展汇总 #
23809次浏览 308人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7436次浏览 43人参与
# 简历第一个项目做什么 #
31541次浏览 329人参与
# 重来一次,我还会选择这个专业吗 #
433336次浏览 3926人参与
# 巨人网络春招 #
11300次浏览 223人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186951次浏览 1120人参与
# 牛客AI文生图 #
21408次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152285次浏览 887人参与
# 研究所笔面经互助 #
118864次浏览 577人参与
# 简历中的项目经历要怎么写? #
310032次浏览 4191人参与
# AI时代,哪些岗位最容易被淘汰 #
63371次浏览 801人参与
# 面试紧张时你会有什么表现? #
30484次浏览 188人参与
# 你今年的平均薪资是多少? #
213002次浏览 1039人参与
# 你怎么看待AI面试 #
179831次浏览 1232人参与
# 高学历就一定能找到好工作吗? #
64304次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76430次浏览 374人参与
# 我的求职精神状态 #
447977次浏览 3128人参与
# 正在春招的你,也参与了去年秋招吗? #
363224次浏览 2637人参与
# 腾讯音乐求职进展汇总 #
160574次浏览 1110人参与
# 校招笔试 #
470312次浏览 2962人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务