蓝桥杯刷题(二分)

前言:

最近学校的学业有点重,好多课的作业一下布置导致我时间紧张,今天好不容易找到了整块的时间来刷刷题,不过有几道题难度对我来说有一点大了,所以到现在为止也没做几道,希望自己在后面能更勤奋一点吧。

正文:

1、分巧克力:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+9;
int n,k,h[N],w[N],ans;
bool check(int x){
	int num=0;
	for(int i=1;i<=n;i++){
		num+=(h[i]/x)*(w[i]/x);
		if(num>=k)return 1;
	}
	return 0;
}
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>h[i]>>w[i];
	long long l=1,r=1e5,mid;
	while(l<r){
		mid=(l+r+1)/2;
		if(check(mid))l=mid;
		else r=mid-1;
	}
	cout<<r<<endl;
	return 0;
}

2、管道:

cpp 复制代码
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
const int N=1e5+10;
typedef long long ll;
int n,m;
pair<int,int>w[N],q[N];
bool check(int mid){
	int cnt=0;
	for(int i=1;i<=n;i++){
		int L=w[i].x,S=w[i].y;
		if(mid>=S){
			int t=mid-S;
			int l=max(1,L-t),r=min((ll)m,(ll)L+t);
			q[cnt++]={l,r};
		}
	}
	sort(q,q+cnt);
	int st = -1, ed = -1;
    for(int i=0;i<cnt;i++){
        if (q[i].x <= ed + 1) ed = max(ed, q[i].y);
        else st = q[i].x, ed = q[i].y;
    }
    return st==1&ed==m;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>w[i].x>>w[i].y;
	}
	ll l=0,r=2e9;
	while(l<r){
		ll mid=l+r>>1;
		if(check(mid))r=mid;
		else l=mid+1;
	}
	cout<<r<<endl;
	return 0;
}

3、技能升级:

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100010;
int n, m;
int a[N], b[N];
bool check(int mid)
{
    LL res = 0;
    for (int i = 0; i < n; i ++ )
        if (a[i] >= mid)
            res += (a[i] - mid) / b[i] + 1;
    return res >= m;
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d%d", &a[i], &b[i]);
    int l = 0, r = 1e6;
    while (l < r)
    {
        int mid = l + r + 1 >> 1;
        if (check(mid)) l = mid;
        else r = mid - 1;
    }
    LL res = 0, cnt = 0;
    for (int i = 0; i < n; i ++ )
        if (a[i] >= r)
        {
            int c = (a[i] - r) / b[i] + 1;
            int end = a[i] - (c - 1) * b[i];
            cnt += c;
            res += (LL)(a[i] + end) * c / 2;
        }
    printf("%lld\n", res - (cnt - m) * r);
    return 0;
}

后记:

之后要开会,不知道大学怎么这么多破事。

相关推荐
沫璃染墨14 分钟前
从零入门数据结构与算法(一):复杂度分析——从算法效率到时间与空间复杂度
c语言·开发语言·数据结构·笔记·算法·飞书
熊猫钓鱼>_>1 小时前
面试出现 606 次的第 K 大,被拆成三个问题问
算法·面试·职场和发展·校招·面经·oj·ai生成
Lzg_na1 小时前
僵尸进程、孤儿进程、守护进程:含义、区别与生命周期详解
c++
zhanghaha13141 小时前
C++ 的类是什么(新手保姆讲解,配合 Qt 对照理解)
c++
xuxigifxfh1 小时前
牛客:BISHI2 【模板】栈的操作
java·开发语言·windows·算法
aqiu1111111 小时前
【C++算法打怪专栏】LeetCode 402. 移掉 K 位数字(单调栈 + 贪心)
c++·算法·leetcode
(Charon)1 小时前
【C++面试】线程并发与异步:区别、实现原理及使用场景
开发语言·c++
糖果豆豆2 小时前
对象是字段,还是关系?集合论和范畴论吵了三十年,答案是两个都对
算法·架构
JMchen2 小时前
实战项目一——实现一个高性能图片滤镜App
android·c++·性能优化
第六种自由2 小时前
[路飞]_Leetcode 22. 括号生成
javascript·算法·leetcode