当前位置:网站首页>GYM103660L.Monster Tower 整体二分
GYM103660L.Monster Tower 整体二分
2022-07-17 22:29:00 【HeartFireY】
GYM103660L.Monster Tower
题目思路
有一个高 n n n层的塔,塔的每层有一个怪兽,怪兽拥有 a i a_i ai的血量,初始血量为 x x x,击败第 i i i层后可以获得该层怪兽的血量(即 x + = a [ i ] x += a[i] x+=a[i]),当且仅当 x ≥ a [ i ] x \geq a[i] x≥a[i]时才可以击败该怪兽。每次只能选择底 k k k层的怪兽进行攻击。问最小的 x x x。
容易发现答案具有单调性,因此考虑二分答案。 c h e c k check check的时候使用优先队列维护底 k k k层的怪兽血量,当堆顶的血量大于当前血量时不合法。
数组是2e5
Code
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 2e5 + 10;
int a[N], s[N];
inline void solve(){
int n = 0, k = 0; cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
auto check = [&](int mid){
priority_queue<int, vector<int>, greater<int>> pq;
int stn = mid;
int pos = 1; while(pos <= k) pq.emplace(a[pos++]);
while(pq.size()){
if(stn < pq.top()) return false;
else{
stn += pq.top();
pq.pop();
if(pos <= n) pq.emplace(a[pos++]);
}
}
return true;
};
int l = 0, r = *max_element(a + 1, a + 1 + n), ans = 0;
while(l <= r){
int mid = l + r >> 1;
if(check(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false), cin.tie(0);
int t = 1; cin >> t;
while(t--) solve();
return 0;
}
边栏推荐
- 009 面试题 SQL语句各部分的执行顺序
- Oserror: sndfile library not found solution
- 3438. Number system conversion
- Several points to be analyzed in the domestic fpga/dsp/zynq scheme
- [microservice] microservice learning note 3: use feign to replace resttemplate to complete remote call
- 论文阅读 TEMPORAL GRAPH NETWORKS FOR DEEP LEARNING ON DYNAMIC GRAPHS
- Oracle - lock
- Read the paper: temporary graph networks for deep learning on dynamic graphs
- Scheduled tasks, VIM directly creates and modifies users
- dba
猜你喜欢

【微服务】 微服务学习笔记三:利用Feign替换RestTemplate完成远程调用

Classes abstraites et dérivées

FPGA(VGA协议实现)

Leetcode 1275. Trouver le vainqueur de "Jingzi"

MongoDB分片集群搭建

ObjectARX -- implementation of custom circle
![[flask introduction series] request hook and context](/img/a9/96e330525ee1337daab275b87db892.png)
[flask introduction series] request hook and context

Notes on random nodes of force buckle 382 linked list

ORA-08103

1、DBMS基本概念
随机推荐
Database SQL Server
Tianqin Chapter 9 after class exercise code
2022 P gas cylinder filling examination practice questions simulated examination platform operation
Display module in pyGame
TDesign 在 vitest 的实践
Redis
Damn it, why is there less space on the USB flash drive? It's the EFI partition. Delete it
Leetcode 1296. 划分数组为连续数字的集合(提供一种思路)
背包问题 (Knapsack problem)
Performance design of distributed transaction
Module 1 job
Distributed transaction summary
状态机练习
5-21 interceptor
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
Behind the high salary of programmers' operation and maintenance
LabVIEW uses multithreading. Will the program run faster
JVM常用调优配置参数
Cilium & Hubble
Domestic fpga/dsp/zynq Chip & board scheme