蓝桥杯2022技能升级_二分枚举

P2045 - [蓝桥杯2022初赛] 消除游戏 - New Online Judgehttp://oj.ecustacm.cn/problem.php?id=2045

 暴力能过40%的数据。。。

#include <bits/stdc++.h>
#define a first
#define b second
using namespace std;
typedef long long ll;
typedef pair<int, int> pll;
const int N = 1e5 + 4;
multiset<pll>s;
int main() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        pll temp;
        cin >> temp.a >> temp.b;
        s.insert(temp);
    }
    ll ans = 0;
    while (m > 0 && !s.empty()) {
        pll cur = *s.rbegin();
        s.erase(*s.rbegin());
        ans += cur.a;
        m--;
        cur.a -= cur.b;
        if (cur.a > 0) {
            s.insert(cur);
        }
    }
    cout << ans;
    return 0;
}

正解是二分枚举最后一次攻击力最高能加多少,并且要注意最后的计算不能把等于这个值的直接加在答案里:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+4;
ll n, m;
ll a[N] = {0}, b[N] = {0};
// 最后一次加攻击能不能到x
ll check(ll x) {
    ll cnt = 0;
    for(int i=0; i<n; i++) {
        if (a[i] >= x) {
            cnt += (a[i] - x) / b[i] + 1;
            if (cnt >= m) {
                return 1;
            }
        }
    }
    return 0;
}
// 二分枚举最后一次攻击力最高能加多少
int getHighestLast() {
    int low = 0, high = 1e6;
    while(low < high) {
        int mid =  (low + high + 1) >> 1;
        if (check(mid)) { // 如果mid可以
            low = mid;
        } else {
            high = mid-1;
        }
    }
    return low;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
    cin >> n >> m;
    ll totalCnt = 0;
    for(int i=0; i<n; i++) {
        cin >> a[i] >> b[i];
        if (a[i] % b[i] == 0) {
            totalCnt += a[i] / b[i];
        } else {
            totalCnt += a[i] / b[i] + 1;
        }
    }
    m = min(m, totalCnt);
    int highestLast = getHighestLast();
    ll ans = 0;
    for (int i=0 ; i<n; i++) {
        if (a[i] >= highestLast) {
            int cnt = (a[i] - highestLast) / b[i] + 1;
            int last = a[i] - (cnt-1) * b[i];
            if (last == highestLast) {
            	cnt--;
            	last += b[i];
			}
            ans += (a[i] + last) * cnt >> 1;
            m -= cnt;
        }
    }
    cout << ans + m * highestLast;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值