4956. 冶炼金属

摘要
Title: 4956. 冶炼金属
Tag: 二分
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

4956. 冶炼金属

  • 题意

    第十四届蓝桥杯省赛C++B组

  • 思路

    二分最小值和最大值即可
    最小值:check的时候,如果大于b了就不行
    最大值:check的时候,如果小于b了就不行

  • 代码

    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-04-18 20:00:24
    * @FilePath: \Acwing\4956\4956.cpp
    * @LastEditTime: 2023-04-18 20:00:35
    */
    #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 = 1e4 + 10, INF = 0x3f3f3f3f;
    int a[N], b[N];
    int n;

    bool check(int x, int flag)
    {
    for (int i = 1; i <= n; ++i)
    {
    if (flag == 1 && a[i] / x > b[i])
    return false;
    if (flag == 2 && a[i] / x < b[i])
    return false;
    }
    return true;
    }

    signed main()
    {
    IOS;
    cin >> n;
    for (int i = 1; i <= n; ++i)
    {
    cin >> a[i] >> b[i];
    }

    // 最小值
    int l = 1, r = INF;
    while (l < r)
    {
    int mid = l + r >> 1;
    if (check(mid, 1))
    r = mid;
    else
    l = mid + 1;
    }
    cout << r << " ";

    // 最大值
    l = 1;
    r = INF;
    while (l < r)
    {
    int mid = l + r + 1 >> 1;
    if (check(mid, 2))
    l = mid;
    else
    r = mid - 1;
    }
    cout << r;

    return 0;
    }

使用搜索:谷歌必应百度