Gym 100886J Sockets 二分答案 + 贪心
Description
Statements
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.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e5+;
int a[maxn],b[maxn];
int num[maxn];
int dp[maxn];
LL sum[maxn];
int n,m;
bool cmp(int a,int b)
{
return a>b;
}
bool check (int val)
{
int begin=,end=val;
LL res = a[]; //能够插多少个
LL can = a[];
int need = ; //有没一定要插一的
int cur = ;
while () {
//res += a[cur];
while (end >= begin && b[end] == need) { //一定要插这个位置
res--; can--; end--;
if (res < ) return false;
}
if (end == begin-) return true;
if (can == ) return false;
// printf ("%d\n",n);
while (can && cur + <= n) { //插尽量多的排插
--can; --res; ++cur;
res += a[cur];
//printf ("%d****\n",a[cur]);
}
if (cur == n) { //所有排插都用完了.
return res >= end-begin+;
}
can = res;
need++;
// printf ("%d\n",res);
}
}
void work ()
{
scanf("%d%d",&n,&m);
for (int i=; i<=n; ++i) scanf("%d",&a[i]);
int lenb=;
for (int j=; j<=m; ++j) {
int x; scanf("%d",&x);
if (x != ) b[++lenb] = x; //0是没用的
}
sort (a+,a++n,cmp);
sort(b+,b++lenb,cmp); int begin=,end=lenb;
while (begin<=end)
{
int mid = (begin+end)/;
if (check(mid))
begin=mid+;
else end=mid-;
}
printf ("%d\n",max(,end));
return ;
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
work();
return ;
}
Gym 100886J Sockets 二分答案 + 贪心的更多相关文章
- BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心
BZOJ_2196_[Usaco2011 Mar]Brownie Slicing_二分答案+贪心 Description Bessie烘焙了一块巧克力蛋糕.这块蛋糕是由R*C(1 <= R,C ...
- 洛谷3933 Chtholly Nota Seniorious 二分答案+贪心
题目链接 题意 给你一个N*M的矩阵 (N,M <=2000) 把他分成两部分 使两部分的极差较大的一个最小 求这个最小值.然后分矩阵的要求是:每个部分内部的方块之间,可以通过上下左右相互到 ...
- Code Forces Gym 100886J Sockets(二分)
J - Sockets Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Valera ...
- G - 土耳其冰淇凌 Gym - 101194D(二分答案 + 贪心检验)
熊猫先生非常喜欢冰淇淋,尤其是冰淇淋塔.一个冰淇淋塔由K个冰淇淋球堆叠成一个塔.为了使塔稳定,下面的冰淇淋球至少要有它上面的两倍大.换句话说,如果冰淇淋球从上到下的尺寸是A0, A1, A2,···, ...
- 【二分答案+贪心】UVa 1335 - Beijing Guards
Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...
- 【二分答案+贪心】解决“最小值最大”问题(UVa 12124 - Assemble)
Problem A - Assemble Time limit: 2 seconds Recently your team noticed that the computer you use to p ...
- 【洛谷】【二分答案+贪心】P1316 丢瓶盖
[题目描述:] 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想从这些瓶盖里找出B个,使得距离最近的2个距离最大,他想知道,最大可以到多少呢? [ ...
- BZOJ5321 JXOI2017加法(二分答案+贪心+堆+树状数组)
二分答案后得到每个位置需要被加的次数.考虑贪心.从左到右考虑每个位置,将以该位置为左端点的区间按右端点从大到小加进堆.看该位置还需要被加多少次,如果不需要加了就不管,否则取堆顶区间将其选择,BIT实现 ...
- bzoj 4310 跳蚤 —— 后缀数组+二分答案+贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4310 二分答案——在本质不同的子串中二分答案! 如果二分到的子串位置是 st,考虑何时必须分 ...
随机推荐
- Hdu 4762 网络赛 高精度大数模板+概率
注意题目中的这句话he put the strawberries on the cake randomly one by one,第一次选择草莓其实有N个可能,以某一个草莓为开头,然后顺序的随机摆放, ...
- [置顶]
什么是C语言结构体字节对齐,为什么要对齐?
一.概念 对齐跟数据在内存中的位置有关.如果一个变量的内存地址正好位于它长度的整数倍,他就被称做自然对齐.比如在32位cpu下,假设一个整型变量的地址为0x00000004,那它就是自然对齐的. ...
- 【jQuery】jquery.metadata.js验证失效
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- shell入门-连接符(并且、和、或者)
特殊符号:&& 说明:并且,左右两边是两条命令,左面的执行成功才会去执行右面的命令.右. 特殊符号:|| 说明:或者,左右两边是两条命令,左边的命令执行不成功,才会执行右面的命令 &a ...
- cadence spb 16.5 破解过程实例和使用感受_赤松子耶_新浪博客
cadence spb 16.5 破解过程实例和使用感受_赤松子耶_新浪博客 Cadence Allegro16.5详细安装具体的步骤 1.下载SPB16.5下来后,点setup.exe,先安装第一项 ...
- 关闭socket以及Socket选项
1 关闭socket ·1)socket套接字使用完毕之后,我们需要将起及时的关闭,正如输入输出流的关闭是一样的:在我上一篇文章中介绍了如何模拟httpClient发送请求数据:这里我还是使用上一篇文 ...
- [poj3250]单调栈 Bad Hair Day
解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 #include<cstdio> #include<c ...
- Flask06 地址约定、利用falsk提供的方法渲染模板
1 访问地址约定 在访问路径的末尾是否需要加 / -> 可以加,也可以不加 前端的访问路径必须和后端的路径完全匹配才能够访问成功,如果我们在后台的路径都是以 / 结尾,但是我们的访问路径是没有以 ...
- 21 、GPD-PSL-VCF
https://genome.ucsc.edu/FAQ/FAQformat.html#format9 1.Variant Call Format(VCF) Example ##fileformat=V ...
- tensorflow placeholder
placeholder 是 Tensorflow 中的占位符,暂时储存变量. Tensorflow 如果想要从外部传入data, 那就需要用到 tf.placeholder(), 然后以这种形式传输数 ...