【2019.8.15 慈溪模拟赛 T1】插头(plugin)(二分+贪心)
2024-09-06 08:57:08
二分
首先,可以发现,最后的答案显然满足可二分性,因此我们可以二分答案。
然后,我们只要贪心,就可以验证了。
贪心
不难发现,肯定会优先选择能提供更多插座的排插,且在确定充电器个数的情况下,肯定选择能经过排插数量最大的那些充电器。
所以,我们只要模拟插排插的过程,记录当前深度\(d\)、插座数\(t\)即可。
设选择的能经过排插数量恰好为\(d\)的充电器有\(x\)个,则若\(t<x\),显然不合法。
否则,我们将\(x\)个位置插上充电器,其余位置尽可能地插排插,就可以了。
代码
#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 400000
#define LL long long
#define Gmax(x,y) (x<(y)&&(x=(y)))
using namespace std;
int n,m,a[N+5],b[N+5];
class FastIO
{
private:
#define FS 100000
#define tc() (A==B&&(B=(A=FI)+fread(FI,1,FS,stdin),A==B)?EOF:*A++)
#define tn (x<<3)+(x<<1)
#define D isdigit(c=tc())
char c,*A,*B,FI[FS];
public:
I FastIO() {A=B=FI;}
Tp I void read(Ty& x) {x=0;W(!D);W(x=tn+(c&15),D);}
}F;
class GreedySolver
{
private:
I bool Check(CI x)
{
RI i,p=1,q=x,d=0;LL t=1,nt;W(q)
{
W(q&&d==b[q]) --t,--q;if(t<0) return false;//插充电器
for(i=t;i&&p<=n;--i) t+=a[p++]-1;++d;//插排插
}return true;
}
public:
I void Solve()
{
W(a[n]<=1) --n;RI l=1,r=m,mid;//删去无用的排插
W(l<r) Check(mid=l+r+1>>1)?l=mid:r=mid-1;printf("%d",l);//二分答案
}
}G;
I bool cmp(CI x,CI y) {return x>y;}
int main()
{
freopen("plugin.in","r",stdin),freopen("plugin.out","w",stdout);
RI i;for(F.read(n),F.read(m),i=1;i<=n;++i) F.read(a[i]);for(i=1;i<=m;++i) F.read(b[i]);//读入
return sort(a+1,a+n+1,cmp),sort(b+1,b+m+1,cmp),G.Solve(),0;//排序,贪心
}
最新文章
- SharePoint 2013 托管导航及相关配置 <;二>;
- visul studio 文件分包
- JAVA thread0.interrupt()方法
- windows内核结构
- Java设计模式系列之桥接模式
- 判断浏览器是否支持某个css属性
- atitit。自己定义uml MOF EMF体系eclipse emf 教程o7t
- 关于java中根据身份证求生日和年龄的问题
- WebApi2官网学习记录---Content Negotiation
- JSP前后台数据交互
- python3中的type与object
- 21)django-csrf(跨站请求伪造)
- Python学习之旅(三十一)
- 图片相似原理--Java实现
- 从0开始做一个的Vue图片/ 文件选择(上传)组件[基础向]
- Windows 10 子系统 Ubuntu 中安装 FastAdmin
- 使用 Sublime Text 做 Javascript 编辑器 - 集成 JSHint 问题检测工具
- React中的Keys
- Css选择器定位详解
- 2018.12.22 Spring学习02
热门文章
- NuGet修改默认包保存的位置
- arcgis api for javascript 学习(一) 调用在线发布的动态地图
- 编译原理之LL(1)文法的判断,递归下降分析程序
- RHEL5.6静默安装oracle11.2.0数据库实例脚本
- MQ脚本回放报错2059
- C#的语法----程序结构(1)
- IT兄弟连 HTML5教程 HTML5文字版面和编辑标签 使用HTML表格
- PowerMock学习(八)之Mock Argument Matcher的使用
- PAT 1011 World Cup Betting 查找元素
- C# - VS2019 DataGridView导出到Excel的三种方法