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,考虑何时必须分 ...
随机推荐
- 【Android学习笔记】 点击穿透(Click Through)
问题:开发一个App,主界面用了Activity,子页面用了Fragment.从Activity跳转到Fragment后Fragment透明,并且点击击穿到Axtivity. 分析:刚开始没有注意到点 ...
- [hdu1176]免费馅饼(数塔dp)
题意:中文题,不解释了 = = 解题关键:逆推,转化为数塔dp就可以了 dp[i][j]表示在i秒j位置的最大值. 转移方程:$dp[i][j] = \max (dp[i + 1][j],dp[i + ...
- mongodb的备份还原
一:备份数据库 G:\Program Files\MongoDB\Server\3.0\bin>mongodump -d mydb -o g:/data/back mongodump -h IP ...
- c# Aspose.Words插入饼图PieChart
private static void Main(string[] args) { Document doc = new Document(); DocumentBuilder builder = n ...
- xgene:疾病相关基因,耳聋,彩色,老年痴呆,帕金森
神经元的传递:一个下游神经元,它接受其上游神经元的各个突触传过来的信号,然而,每个突触对该下游神经元的激活权重是不同的. 从神经网络的本质上说,当人连续.多次遭受失败的时候,大脑内就会释放大量的抑制性 ...
- 7.31实习培训日志-docker sql
docker mysql docker问题 今天问了老师有关docker的问题,entrypoint和cmd的区别,两者同时存在cmd的执行结果会被当做参数传入entrypoint. dockerfi ...
- C#和Python 图片和base64的互转
C#实例代码: /// <summary> /// 图片转base64 /// </summary> /// <param name="bmp"> ...
- virtio使用
Windows 参考文档 下载virtio驱动 下载地址 如果是在Fedora或CentOS环境下,可使用yum的方式下载驱动 [root@centos centos]#wget https://fe ...
- 三种Hash算法对比以及秒传原理.
三种Hash算法对比以及秒传原理 CRC (32/64) MD5 Sha1 分5个点来说 1.校验值长度 2.校验值类别 3.安全级别 4.应用场景 1).校验值长度 CRC(32/64) 分别 ...
- 2018宁夏邀请赛G(DFS,动态规划【VECTOR<PAIR>】)
//代码跑的很慢四秒会超时,结尾附两秒代码(标程) #include<bits/stdc++.h>using namespace std;typedef long long ll;cons ...