5017. 垦田计划

摘要
Title: 5017. 垦田计划
Tag: 二分
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

5017. 垦田计划

  • 题意

  • 思路

    二分最小需要几天即可
    注意:

    • 天数不能低于k
    • 二分时,若耗时天数小于mid,直接continue
  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-08-26 22:46:52
    * @FilePath: \Acwing\5017\5017.cpp
    * @LastEditTime: 2023-08-26 23:12:05
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #undef int

    #define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'
    typedef pair<int, int> PII;

    const int N = 1e5 + 10, INF = 0x3f3f3f3f;

    int t[N], c[N];

    signed main()
    {
    //freopen("Tests/input_1.txt", "r", stdin);
    IOS;
    int n, m, k, mx = k;
    cin >> n >> m >> k;

    for (int i = 1; i <= n; ++ i){
    cin >> t[i] >> c[i];
    mx = max(mx, t[i]);
    }

    int l = k, r = mx;


    auto check = [&](int x){
    int m_tmp = m;
    for(int i = 1; i <= n; ++ i){
    if (x > t[i]) continue;
    m_tmp -= (t[i] - x) * c[i];
    if (m_tmp < 0) return false;
    }
    return true;
    };


    while (l < r){
    int mid = l + r >> 1;
    if (check(mid)) r = mid;
    else l = mid + 1;
    }

    cout << r ;
    return 0;
    }
使用搜索:谷歌必应百度