当前位置:网站首页>二分(二分答案)问题合集
二分(二分答案)问题合集
2022-07-16 08:19:00 【追随远方的某R】
二分问题有很明显的关键字如:最大值最小,最小值最大。
或者在数据上发现只有log级别才能接受时,我们都应该优先考虑二分。
二分问题的难点也不在二分,而是如何使用贪心配合二分,贪心策略往往是难点所在。
题意:n个工人,m个任务,每个任务都指定哪个工人适合这个任务,如果由适合的工人来做这个任务就只花费1小时,否则要花费2小时。求最少花费多少小时
关键字:花费时间最小。
思路:贪心思路是,如果对于我们规定时间,这个工人完成不所有指定任务,那么就把所有没完成的任务累加起来,可以完成任务的工人要利用起来去帮助完成这些多出的任务。
值得注意的是:我们需要先统计出所有工人不能完成的任务数,然后再去找人帮忙,不能凭空帮忙。
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 2e5+100;
int a[N],n,m;
bool check(int num)
{
int sum=0;
for(int i=1;i<=n;i++)
{
if(a[i]>num)
{
sum+=a[i]-num;
}
else
{
sum-=(num-a[i])/2;
sum=max(sum,0);
}
}
return sum==0?true:false;
}
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int t;
for(cin>>t;t;t--)
{
cin>>n>>m;
for(int i=1,temp;i<=m;i++)
{
cin>>temp;
a[temp]++;
}
sort(a+1,a+n+1,cmp);
int l=0,r=2*m;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
{
r=mid-1;
}
else
{
l=mid+1;
}
}
cout<<l<<endl;
for(int i=1;i<=n;i++)
{
a[i]=0;
}
}
return 0;
}
精度相关的二分问题
P1577 切绳子
#include <bits/stdc++.h>
using namespace std;
double a[10000+10],l,r=1000000,mid;
int n,k;
bool check(double mid){
int ans=0;
for(int i=1;i<=n;i++)
ans+=a[i]/mid;
return ans>=k;
}
int main(){
//freopen("in.txt","r",stdin);
cin>>n>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
while(r-l>=0.001){
mid=(l+r)/2.0;
if(check(mid))
l=mid+0.001;
else
r=mid-0.001;
}
cout<<r<<endl;
return 0;
}
TO BE CONTINUE。。。
边栏推荐
- 基于epoll实现聊天室(内含定时器,处理客户连接状态)
- Airiot low code development platform, 10 minutes to build the Internet of things system
- Domestic postman tool, easy to use!
- C语言——常用字符串函数的实现
- Full link voltage test: preparations for the test
- The solution to the slow download speed of vs2019 recently
- C language -- the implementation of common string functions
- HMS core graphics and image technology shows the latest functions and application scenarios, and accelerates the construction of digital intelligence life
- Redis cluster test
- Runtime data area & method area (permanent generation / meta space) & stack frame
猜你喜欢
随机推荐
启用远程 rsyslog 日志服务
From it R & D staff turnover thought
Realize chat room based on epoll (including timer to handle customer connection status)
如何利用无常损失从流动资金池中提取价值
Autojs learning TTS grabs voice red envelopes
Specify TLS 1.3 and ciphers to improve security and performance
从IT研发人员离职工作交接想到的
Lscale theme emlog background management panel theme source code
Autojs learning - Application List
R语言---颜色选择和设置
spk接口指的是什么
Differences among key, primary key, unique key and index in MySQL
成都 Meetup |分布式数据库,企业降本增效新引擎
How to set up domain name resolution?
【前缀和和差分】
指定 TLS 1.3 和 Ciphers 以提升安全性及性能
Detailed explanation of the decision table method of common test case design methods
Blue Hat Cup 2022 preliminaries electronic forensics
bbox center-size representation and corners representation
Charles使用教程









