蓝桥杯备战10.分巧克力

P8647 [蓝桥杯 2017 省 AB] 分巧克力 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

暴力枚举 过70%样例

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e7+10,M = 1e3+10;
int n,k;
int h[N],w[N];
bool check(int x)
{
	int cnt = 0;
	for(int i=1;i<=n;i++)
	{
		cnt+=(h[i]/x)*(w[i]/x);
	}
	if(cnt>=k)return true;
	else return false;
}
signed main()
{
	std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>h[i]>>w[i];
	int ans = 0;
	for(int i=1;i<=1e5;i++)
	{
		if(check(i))
		ans=i;
	}
	cout<<ans;
	return 0;
}

二分

cpp 复制代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
const int N = 2e7+10,M = 1e3+10;
int n,k;
int h[N],w[N];
bool check(int x)
{
	int cnt = 0;
	for(int i=1;i<=n;i++)
	{
		cnt+=(h[i]/x)*(w[i]/x);
	}
	if(cnt>=k)return true;
	else return false;
}
signed main()
{
	std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>h[i]>>w[i];
	int ans = -1;
	int l = 0,r = 1e5;
	while(l<r)
	{
		int mid = (l+r+1)/2;
		if(check(mid))
		{
			l = mid;
			ans = l;
		}
		else
		{
			r = mid-1;
			ans = r;
		}
	}
	cout<<ans;
	return 0;
}
相关推荐
Emilia486.7 小时前
【Leetcode&nowcode】代码强化练习(二叉树)
算法·leetcode·职场和发展
墨染点香7 小时前
LeetCode 刷题【135. 分发糖果】
算法·leetcode·职场和发展
云青山水林9 小时前
零基础如何准备蓝桥杯
蓝桥杯
熬了夜的程序员12 小时前
【LeetCode】90. 子集 II
数据结构·算法·leetcode·链表·职场和发展·排序算法
熬了夜的程序员12 小时前
【LeetCode】91. 解码方法
算法·leetcode·链表·职场和发展·排序算法
熬了夜的程序员1 天前
【LeetCode】89. 格雷编码
算法·leetcode·链表·职场和发展·矩阵
程序员杰哥1 天前
Pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
熬了夜的程序员2 天前
【LeetCode】87. 扰乱字符串
算法·leetcode·职场和发展·排序算法