Code Forces Gym 100886J Sockets(二分)
J - Sockets
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Valera has only one electrical socket in his flat. He also has m devices which require electricity to work. He's got n plug multipliers to plug the devices, the i-th plug multiplier has ai sockets.
A device or plug multiplier is supplied with electricity if it is either plugged into the electrical socket, or if it is plugged into some plug multiplier which is supplied with electricity.
For each device j, Valera knows the safety value bj which is the maximum number of plug multipliers on the path between the device and the electrical socket in his flat. For example, if bj = 0, the device should be directly plugged into the socket in his flat.
What is the maximum number of devices Valera could supply with electricity simultaneously? Note that all devices and plug multipliers take one socket to plug, and that he can use each socket to plug either one device or one plug multiplier.
Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 2·105), the number of plug multipliers and the number of devices correspondingly.
The second line contains n space-separated integers a1, a2, ..., an (2 ≤ ai ≤ 2·105). Here, the number ai stands for the number of sockets on the i-th plug multiplier.
The third line contains m space-separated integers b1, b2, ..., bm (0 ≤ bj ≤ 2·105). Here, the number bj stands for the safety value of the j-th device.
Output
Print a single integer: the maximum number of devices that could be supplied with electricity simultaneously.
Sample Input
Input
3 5
3 2 2
1 2 2 1 1
Output
4
Input
3 3
2 2 2
1 2 2
Output
3 题意:给你N个插排,a[i]代表第i个插排上面插孔的个数;接着给你M个电器,b[i]代表第i个电器最多能承受的级联层数,然后让你求出最多能有个电器同时工作?
题解:贪心+二分,插排按插孔的个数从大到小排序,电器按所能承受的级联层数从大到小排序,接着在[1,m]的区间内二分答案即可。
二分的判断主要是看当前区间内(1,mid)的电器能否全部都插在插排上面,如果满足条件,就把当前的mid记录下来。
代码:
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <sstream>
#define push_back pb
#define make_pair mp
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
#define mt(A) memset(A,0,sizeof(A))
#define mod 1000000007
using namespace std;
typedef long long LL;
const int N=+;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
LL a[N],b[N],n,m;
bool check(LL num)//判断(1.num)内的电器能否全部工作
{ //cnt代表的是当前还没有工作的电器的个数
LL cnt=num,i=,tot=,step=;//step代表级联的层数,初始状态当然为0;
while(true) //tot代表的是当前剩余的插孔的个数
{
while(cnt>=&&b[cnt]<=step)//如果电器的级联层数满足条件,就插上插排
{
cnt--;tot--;
}
if(cnt<&&tot>=)return true;//如果电器插完了,插孔>=0,那说明(1,num)是满足的
if(tot<)return false;//插孔<0说明不满足
LL newtot=;
while(tot&&i<=n)//更新插孔的个数
{
newtot+=a[i++];
tot--;
}
tot+=newtot;
step++;//级联层数每次增加一层
}
}
int main()
{
#ifdef Local
freopen("data","r",stdin);
#endif
LL i,j,k,l,r,mid,ans;
cin>>n>>m;
for(i=;i<=n;i++)cin>>a[i];
for(i=;i<=m;i++)cin>>b[i];
sort(a+,a+n+,greater<LL>());
sort(b+,b+m+,greater<LL>());
l=;r=m;
while(l<=r)
{
mid=(l+r)/;
if(check(mid))
{
ans=mid;//记录答案
l=mid+;
}
else r=mid-;
}
cout<<ans<<endl;
return ;
}
Code Forces Gym 100886J Sockets(二分)的更多相关文章
- Gym 100886J Sockets 二分答案 + 贪心
Description standard input/outputStatements Valera has only one electrical socket in his flat. He al ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- K - Video Reviews Gym - 101755K (二分)
题目链接: K - Video Reviews Gym - 101755K 题目大意: 一家公司想让个人给他们的产品评论,所以依次去找这个人,第i个人会评论当且仅当已经有个人评论或他确实对这个产品感兴 ...
- Gym - 101908G Gasoline 二分+最大流
G - Gasoline Gym - 101908G 题意:给出R个提供点,P个接收点,每个接收点都要接收满,还有一个运输的时间,问最小时间能够完成所有的运输 题解:首先每次都必须要满流,所以我们只要 ...
- B - Glider Gym - 101911B(二分)
output standard output A plane is flying at a constant height of hh meters above the ground surface. ...
- J - Joseph and Tests Gym - 102020J (二分+线段树)
题目链接:https://cn.vjudge.net/contest/283920#problem/J 题目大意:首先给你n个门的高度,然后q次询问,每一次询问包括两种操作,第一种操作是将当前的门的高 ...
随机推荐
- 表格行变换顺序功能(jquery)
周末写了个更改表格行顺序的小功能,直接贴代码 表格部分如下: <table class="table" id="test_table"> <t ...
- Instructions Set JAVA_HOME System-Wide
Instructions Set JAVA_HOME System-Wide 1 Start a root terminal session and then change directories t ...
- 2014年度辛星html教程夏季版第四节
我们前面也涉及了HTML中的一些东西,接下来我们要涉及到图像了,如果没有图像,即使文字的样式再多,再复杂,终归还是单调的,我们就需要用图片来给我们的网页增加更多的表现形式. ************* ...
- 为什么很多语言选择在JVM上实现
非常经济地实现跨平台.你的语言编译器后端只需要输出 JVM 字节码就可以.跨平台需要极大的工作量,举个例子,只是独立开发生成本地代码,就需要花费大量精力去针对不同平台和处理器进行优化(比如 Firef ...
- python类库26[web2py之基本概念]
一 web2py的应用的执行环境Models,Controllers和views所在的执行环境中,以下对象已经被默认地导入: Global Objects: request,response,ses ...
- MDK建立STM32F103*开发模板
一.整体流程 1.获取ST库--STM32F10x_StdPeriph_Lib_V3.5.0 2.新建文件夹并加载文件 3.新建工程 4.给工程添加组 5.设置"Target Option& ...
- Longest common prefix | leetcode
Write a function to find the longest common prefix string amongst an array of strings. 思路:要去是寻找字符串ve ...
- iOS 图片填充 UIImageView
UIViewContentModeScaleAspectFit, //这个图片都会在view里面显示,并且比例不变 这就是说 如果图片和view的比例不一样 就会有留白如下图1 UIView ...
- [BZOJ 1009] [HNOI2008] GT考试 【AC自动机 + 矩阵乘法优化DP】
题目链接:BZOJ - 1009 题目分析 题目要求求出不包含给定字符串的长度为 n 的字符串的数量. 既然这样,应该就是 KMP + DP ,用 f[i][j] 表示长度为 i ,匹配到模式串第 j ...
- Codeforces Round #198 (Div. 2) —— A
最水的题,可惜当时赶时间没有注意数据范围:暴力超时了! 其实应该用x,y的最大公约数来判断: 代码: #include<iostream> using namespace std; int ...