当前位置:网站首页>A collection of dichotomous (dichotomous answers) questions
A collection of dichotomous (dichotomous answers) questions
2022-07-18 16:01:00 【Follow some R in the distance】
Dichotomous questions have obvious keywords such as : The maximum is the smallest , The minimum is the maximum .
Or only log When the level is acceptable , We should all give priority to two points .
The difficulty of dichotomy is not dichotomy , But how to use greed to cooperate with dichotomy , Greedy strategy is often the difficulty .
The question :n One worker ,m A mission , Each task specifies which worker is suitable for the task , If the right workers do this task, it will only cost 1 Hours , Otherwise, it will cost 2 Hours . Ask how many hours it will take at least
keyword : Take the least time .
Ideas : Greedy thinking is , If we set a time , This worker can't complete all the assigned tasks , Then add up all the unfinished tasks , Workers who can complete the task should use it to help complete these extra tasks .
It is worth noting that : We need to count the number of tasks that all workers cannot complete , Then ask someone to help , You can't help out of thin air .
#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;
}
Accuracy related dichotomy
P1577 Cut the rope
#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...
边栏推荐
- Share a super useful polling + timer manager
- Elaticsearch安装越南语分词器
- Do you know the meaning of call concurrency in okcc?
- 7.13学习记录
- This domestic editor will open source soon!
- VR (I) ATW ASW
- 中关村e谷·苏高新承办2022苏州中日韩高层次人才项目路演大赛
- Interviewer: what are the methods of redis performance optimization?
- 全网优秀的开源攻防武器项目
- Deepin wine QQ/微信中文显示为方块的原因之一
猜你喜欢
随机推荐
剑指offer 46:把数字翻译成字符串
The problem and solution of calling glcreateshader to crash
Big guy said * computing talk Club | Sanxingdui fantasy trip: an experience that only cloud computing can bring
Joint Autoregressive and Hierarchical Priors for Learned Image Compression文献复现
How to set up domain name resolution?
##负载均衡LVS集群详解##
二分(二分答案)问题合集
redis实现浏览历史模块
指定 TLS 1.3 和 Ciphers 以提升安全性及性能
uniapp扫码原生插件(Google MLKit、zxing;支持同时扫多个码)
VMware 虚拟机运行卡慢的解决办法
apache 压力测试工具 ab ,带post参数,token请求
《名侦探柯南》1049话作画大崩坏 角色频频“变脸”
eoc是什么
VR(一)ATW ASW
bbox center-size representation and corners representation
Charles使用教程
Autojs learning - sound transformer template
大咖说*计算讲谈社|三星堆奇幻之旅:只有云计算才能带来的体验
How to handle code exceptions gracefully with assertion ideas









