Stressful Training

题目描述:

Berland SU holds yet another training contest for its students today. nnn students came, each of them brought his laptop. However, it turned out that everyone has forgot their chargers!

Let students be numbered from 111 to nnn. Laptop of the iii-th student has charge aia_iai at the beginning of the contest and it uses bib_ibi of charge per minute (i.e. if the laptop has ccc charge at the beginning of some minute, it becomes c−bic - b_ic−bi charge at the beginning of the next minute). The whole contest lasts for kkk minutes.

Polycarp (the coach of Berland SU) decided to buy a single charger so that all the students would be able to successfully finish the contest. He buys the charger at the same moment the contest starts.

Polycarp can choose to buy the charger with any non-negative (zero or positive) integer power output. The power output is chosen before the purchase, it can’t be changed afterwards. Let the chosen power output be xxx. At the beginning of each minute (from the minute contest starts to the last minute of the contest) he can plug the charger into any of the student’s laptops and use it for some integer number of minutes. If the laptop is using bib_ibi charge per minute then it will become bi−xb_i - xbi−x per minute while the charger is plugged in. Negative power usage rate means that the laptop’s charge is increasing. The charge of any laptop isn’t limited, it can become infinitely large. The charger can be plugged in no more than one laptop at the same time.

The student successfully finishes the contest if the charge of his laptop never is below zero at the beginning of some minute (from the minute contest starts to the last minute of the contest, zero charge is allowed). The charge of the laptop of the minute the contest ends doesn’t matter.

Help Polycarp to determine the minimal possible power output the charger should have so that all the students are able to successfully finish the contest. Also report if no such charger exists.

题意:

有n台电脑,一共要撑过k分钟
对于每台电脑,他的初始电量为ai,每分钟消耗电量为bi
问,至少需要一个功率(每分钟)为多大的充电器才能保证[0,k)时间内每一台电脑的电量都不为负

思路:

最大功率的最小值,很显然是二分

但是这个题的check函数极其不好写(╥﹏╥)

首先到底给哪个充电?这就是一个贪心,每次都找能坚持的时间最小的那个,如果坚持的时间相同,我们就选bi大的那个,如果bi也相同,我们就选ai小的那个

所以就需要用结构体来存每个电脑的ai,bi和ci(也就是ai / bi)

同时要用一个优先队列/小根堆来维护

维护的东西就是上面写的那个贪心,这里就要写结构体重载,不然塞不进优先队列

对于每次check(x)时,先选出来ci < k的放进优先队列q中,判断一下对列是否为空,空就返回true,从0循环到k-1(从0开始是因为可能有电脑一次都撑不住,k-1是因为最后一次即比赛结束时电量为多少就已经不在乎了),取出队首,扔出去,判断一下此时他的ci是否小于i,小于的话就说明它撑不到给他充电的时候(就是两个电脑同时缺电,但却只有一个充电器,无法撑住),就return false,然后将加上x的点亮的点重新放进优先队列,循环往复……

再就是会有-1的存在,也就是没有能满足的充电器,此时的check返回的是false,所以我们就在true的地放更新ans,对于没有充电器的情况,就不会运行到r = mid + 1 , 就不会改ans的值,就只需要初始化ans为-1即可

再就是这个题比较坑的就是,卡输入,不开快读会TLE,再就是输入的数是1e12,所有得开longlong,所以就得用longlong的快读╮( ̄▽ ̄"")╭

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <sstream>
#include <map>
#include <set>
using namespace std;
#define inf 0x3f3f3f3f
#define MAX 1000000 + 50
#define endl '\n'
#define seed 13331
#define mod 1000000007
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a,b) memset((a),(b),sizeof(a))
typedef  long long ll ;
//不开longlong见祖宗!
//inline __int128 read(){__int128 x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-'){f = -1;}ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
//inline void print(__int128 x){if(x < 0){putchar('-');x = -x;}if(x > 9){print(x / 10);}putchar(x % 10 + '0');}
inline ll llRead(){ll x(0), t(1);char o (getchar());while (o < '0' || o > '9') {if (o == '-') {t = -1;}o = getchar();}for (; o >= '0' && o <= '9'; o = getchar()) {x = (x << 1) + (x << 3) + (o ^ 48);}return x * t;}
inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;}
inline void write(int x){if (x < 0) {x = ~x + 1; putchar('-');}if (x > 9){write(x / 10);}putchar(x % 10 + '0');}
ll qpow(ll a,ll n){ll ans=1,base=a%mod;while(n){if(n&1)ans=(ans*base)%mod;base=(base*base)%mod;n>>=1;}return ans;}
inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }ll n, k, ans = -1;
struct ran{ll a, b, c;bool operator < (const ran &X)const{if(X.c != c)return c > X.c;else if(X.b != b)return b < X.b;return a > X.a;}
}tr[MAX], now, nextt;bool check(ll x){priority_queue<ran>q;for(int i = 1; i <= n; ++i){if(tr[i].c < k)q.push(tr[i]);}if(q.empty())return true;for(int i = 0; i < k; ++i){now = q.top();q.pop();if(now.c < i)return false;if((now.a + x) / now.b < k){nextt.a = now.a + x;nextt.b = now.b;nextt.c = nextt.a / nextt.b;q.push(nextt);}if(q.empty())return true;}return true;
}int main(){n = llRead();k = llRead();for(int i = 1; i <= n; ++i)tr[i].a = llRead();for(int i = 1; i <= n; ++i){tr[i].b = llRead();tr[i].c = tr[i].a / tr[i].b;}ll l = 0, r = 2e12;while (l <= r){ll mid = (l + r) / 2;if(check(mid)){ans = mid;r = mid - 1;}else l = mid + 1;}cout<<ans<<endl;return 0;
}

论5ms能干什么(手动狗头

Stressful Training(二分+贪心+优先队列)相关推荐

  1. LA 4254 Processor 处理器 【二分 贪心 优先队列】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21663 二分: 最大值最小的问题通过二分来求解.对处理器速度进行 ...

  2. 蒟蒻的第一篇博客CF1041C Coffee Break(二分+贪心+set)

    CF1041C Coffee Break(二分+贪心+set) 描述 Recently Monocarp got a job. His working day lasts exactly mm min ...

  3. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  4. 1163 最高的奖励(贪心+优先队列)

    有N个任务,每个任务有一个最晚结束时间以及一个对应的奖励.在结束时间之前完成该任务,就可以获得对应的奖励.完成每一个任务所需的时间都是1个单位时间.有时候完成所有任务是不可能的,因为时间上可能会有冲突 ...

  5. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 1 /* 2 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 3 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小, ...

  6. 洛谷P1182 数列分段 Section II(二分+贪心)

    题目描述 对于给定的一个长度为N的正整数数列 A1∼NA _{1∼N}A1∼N​,现要将其分成 M(M≤N)M(M≤N)M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一 ...

  7. 贪心(优先队列) - New Year Snowmen - CodeForces - 140C

    贪心(优先队列) - New Year Snowmen - CodeForces - 140C 题意: 给定一个长度为n的正整数序列a1,a2,...,an.给定一个长度为n的正整数序列a_1,a_2 ...

  8. CodeForces 140C New Year Snowmen (贪心+优先队列)

    题意:n个数,选三个严格下降的数为一组,求最多能选多少组,并列出每组哪些数. 题解:贪心+优先队列 最多能选多少组,那么必须贪心数量多的. 例如:1 1 2 3 4 5 如果按照数的大小排序,只能贪到 ...

  9. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

  10. 【BHOJ 女娲加农炮 |、||】贪心 | 优先队列 | 堆 | E

    这次我们通过两道例题来总结一下优先队列的用法和实现: 目录: [BHOJ 1512]女娲加农炮 [BHOJ 1517]女娲加农炮II [BHOJ 1512]女娲加农炮 核心:贪心 + 优先队列 URL ...

最新文章

  1. 2021年6月程序员平均工资 15052,你给行业拖后腿了吗?
  2. 利用python快速搭建一个ftp文件服务器
  3. Web服务之Nginx浅析
  4. Redis-相关概念记录
  5. CF1365G Secure Password(构造,交互,二进制分组)
  6. MSP430F5529 DriverLib 库函数学习笔记(二)GPIO
  7. php laravel 返回统一格式,封装的统一的Laravel响应类,返回数据类
  8. HTML期末大作业-小米商城
  9. .NET Web实时消息后台服务器推送技术-GoEasy
  10. jeesite如何已生成数据的数据源_jeesite 多数据源配置
  11. 数据分析中会常犯哪些错误,如何解决? 三
  12. 中播放*.mid格式及其它格式的音乐
  13. 英语3500词(十三)society主题(2022.1.25)
  14. Revit二次开发—载入族并交互式放置
  15. 自己做了一个分享网盘资源的网站
  16. 面试官:使用无界队列的线程池会导致内存飙升吗?
  17. 利用Python进行数据分析(Ⅴ)
  18. u盘打不开提示格式化?里面的数据怎么办?
  19. 详解人工智能领域重大突破:GPT-3
  20. 程序猿终级课颈椎腰椎锻炼

热门文章

  1. 超强大的数学计算器——WolframAlpha(含安卓下载连接)
  2. svchost 总是占用网速 一招见效(实测)
  3. 51单片机数码管表白
  4. powerbi服务器性能分配,Power BI 嵌入式分析性能最佳做法
  5. 360 技术岗秋招笔试原题(2022届)
  6. Pytorch:训练中断再恢复时的注意事项
  7. oracle序列高速缓存,行高速缓存上的等待事件
  8. 华为3Com孤单上路
  9. NeRF神经辐射场学习笔记(十)— BungeeNeRF(CityNeRF)实现以及代码注释
  10. 巨杉数据库 java,巨杉Tech|SequoiaDB 巨杉数据库高可用容灾测试