An Easy Problem 优先队列/二分

链接:https://ac.nowcoder.com/acm/contest/33467/A
来源:牛客网
image

优先队列

先放入每个x坐标的最上面的值都优先队列中,然后取出队头,更新该x坐标的下一个值即可。

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

#define endl '\n'
#define int long long
#define fi first
#define se second
#define pb push_back

#define foa(x, y, z) for(int x = (y), ooo = (z); x <= z; ++x)
#define fos(x, y, z) for(int x = (y), ooo = (z); x >= z; --x)
#define ckmax(x, y) ((x) < (y) ? (x) = (y), 1 : 0)
#define ckmin(x, y) ((x) > (y) ? (x) = (y), 1 : 0)

typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10;
int n, m;
vector<int> v;
int a[N];
void solve()
{
    int k;
    cin >> n >> m >> k;
    priority_queue<pii> q;
    foa(i, 1, n) q.push({i * m, i});
//     cout << q.top().fi << endl;
    
    foa(i, 1, k - 1) {
        auto t = q.top();
//         printf("%d \n", t.fi);
        q.pop();
        q.push({t.fi - t.se, t.se});
    }
    cout << q.top().fi << endl;
}
signed main()
{
	// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
	solve();
    return 0;
}

二分

二分一个值x,然后暴力计算出大于该值的数量,然后二分即可,注意二分值等于k-1时,右区间向mid的值逼近,不能搞反。

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

#define endl '\n'
#define int long long
#define fi first
#define se second
#define pb push_back

#define foa(x, y, z) for(int x = (y), ooo = (z); x <= z; ++x)
#define fos(x, y, z) for(int x = (y), ooo = (z); x >= z; --x)
#define ckmax(x, y) ((x) < (y) ? (x) = (y), 1 : 0)
#define ckmin(x, y) ((x) > (y) ? (x) = (y), 1 : 0)

typedef pair<int, int> pii;
typedef long long ll;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int N = 1e6 + 10;
int n, m;
vector<int> v;
int a[N];
// > x num
int gt(int x)
{
    int ans = 0;
    foa(i, 1, n)
        ans += max(m - x / i, 0ll);
    return ans;
}
void solve()
{
    int k;
    cin >> n >> m >> k;
    int l = 0, r = 1e13;
    while(l < r) {
        int mid = (l + r) >> 1;
        if(gt(mid) <= k - 1) r = mid;
        else l = mid + 1;
    }
    cout << l << endl;
}
signed main()
{
	// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    
	solve();
    return 0;
}
posted @   1564269628  阅读(18)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示