蓝桥杯刷题(二分)

前言:

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

正文:

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;
}

后记:

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

相关推荐
先吃饱再说15 小时前
判断回文字符串,从一行代码到双指针优化
算法
见过夏天15 小时前
C++ 基础入门完全指南
c++
黄敬峰18 小时前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术19 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六1 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术1 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
Asize1 天前
初识DFS 与 BFS:递归、队列与图遍历
算法
罗西的思考2 天前
机器人 / 强化学习】HIL-SERL:人类在环驱动的具身智能进化框架
人工智能·算法·机器学习
美团技术团队2 天前
LongCat 开源 VitaBench 2.0:长期动态智能体基准新标杆
人工智能·算法