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开发 如何通过web服务器访问MYSQL数据库并且使其数据同步到android SQLite数据库?
通过web服务器访问MYSQL数据库有以下几个过程: 1.在MySql下创建自己的数据库和自己的表单 2.连接数据库. 3.访问数据库 1.创建web工程 (服务器端) 在Myeclipse下新建一个 ...
- 上传下载,使用,commons-fileupload,commons-io来加载上传下载
导入两个jarcommons-fileupload.jar,和commons-io.jar 在请求中创建核心类 // 1. 创建工厂对象 FileItemFactory factory = new D ...
- 问题7:如何实现用户的历史记录功能(最多n条)
实例:制作猜字游戏,添加历史记录功能,显示用户最近猜过的数字 解决方案:使用容量为n的队列存储历史记录 使用标准库colections中的deque,一个双端循环队列 程序退出前,可以使用pickle ...
- docker-machine create --driver virtualbox myvm1 创建失败
1. 问题描述 docker-machine create --driver virtualbox myvm1 安装完 virtualbox 后,无法创建. 输出内容为: Running pre-cr ...
- j++与++j
(j++)+(++j)+(++j) 核心部分汇编代码 执行顺序j原始值为5,存放在栈的ebp-4中;q无初始值,存放在栈的ebp-8中先执行一次j自增赋值++j ;j=6执行第一个加法运算;值存放在C ...
- J2EE 学习路线
分享一个比较好的学习网站 http://edu.51cto.com/roadmap/view/id-86.html ================================J2EE=== ...
- tomcat jsp 数字串传值异常问题
1.在下面的jsp内嵌java代码去除num之前,有某Controller已经有了操作: request.getSession().setAttribute("num", ...
- js 使用中一些需要提醒的点
1.js 中可以直接使用输出java 变量 <script> var path = '<%=basePath%>'; 2.js重新注册事件后,如何让事件不自动执行? mzTxt ...
- 微信小程序自学第一课:工程目录结构与.json文件配置
注册成为开发者 地址: https://mp.weixin.qq.com/cgi-bin/wx 开发者工具下载地址 https://mp.weixin.qq.com/debug/wxadoc/dev/ ...
- Gson应用:利用map和list来拼装Json消息
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...