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+20; 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=1,end=val; LL res = a[1]; //能够插多少个 LL can = a[1]; int need = 1; //有没一定要插一的 int cur = 1; while (1) { //res += a[cur]; while (end >= begin && b[end] == need) { //一定要插这个位置 res--; can--; end--; if (res < 0) return false; } if (end == begin-1) return true; if (can == 0) return false; // printf ("%d\n",n); while (can && cur + 1 <= n) { //插尽量多的排插 --can; --res; ++cur; res += a[cur]; //printf ("%d****\n",a[cur]); } if (cur == n) { //所有排插都用完了. return res >= end-begin+1; } can = res; need++; // printf ("%d\n",res); } } void work () { scanf("%d%d",&n,&m); for (int i=1; i<=n; ++i) scanf("%d",&a[i]); int lenb=0; for (int j=1; j<=m; ++j) { int x; scanf("%d",&x); if (x != 0) b[++lenb] = x; //0是没用的 } sort (a+1,a+1+n,cmp); sort(b+1,b+1+lenb,cmp); int begin=1,end=lenb; while (begin<=end) { int mid = (begin+end)/2; if (check(mid)) begin=mid+1; else end=mid-1; } printf ("%d\n",max(1,end)); return ; } int main() { #ifdef local freopen("data.txt","r",stdin); #endif work(); return 0; }
posted on 2016-08-27 22:37 stupid_one 阅读(373) 评论(0) 编辑 收藏 举报
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· MQ 如何保证数据一致性?