当前位置:网站首页>1007 Climb Stairs (greedy | C thinking)
1007 Climb Stairs (greedy | C thinking)
2022-08-05 03:59:00 【Codiplay】
Intention
Xiao Ming wants to climb a building with n floors, and each floor has a monster with a life value of hi.This monster can only be destroyed when Xiao Ming's attack power a>= health h.If Xiao Ming destroys this monster, he will absorb his HP and convert it into his own attack power.Initially, Xiao Ming is on the 0th floor and has an attack power of a0.
Xiao Ming has two ways of acting
One is to move up i cells, i<=k; the other is to go down 1 cell.A floor cannot be climbed twice
Now Xiaoming wants to climb to the top of the building and destroy all the monsters, ask Xiaoming if he can do it.
Thoughts
Segmented processing.Since the title requires that all monsters must be beaten, the skipped monsters have to go back to defeat them.We need to find a right endpoint r from the current position x, so that the monsters in this section can be defeated in the order of r, r-1, r-2, x.It is not difficult to see that it is not worse when r is smaller.
greedy.Greed from back to front.The farthest can be from i + k - 1 (equivalent to Xiaoming jumping to i + k - 1 and then going down, and when you reach i, you can still jump to the i + k layer) Start greedy, and reset the value if you are not greedy.(Skip this hard bone for a while, and process the current a[i] hard bone first), reduce the scope of greed, the purpose is to make value >= a[i], if it is not reached, output NO
#includetypedef long long ll;using namespace std;int n,w,k;void solve(){cin >> n >> w >> k;std::vector a(n + 1);for (int i = 1; i <= n; i++ ) cin >> a[i];for (int i = 1; i <= n; i++ ) {if(a[i] <= w) {w += a[i];} else {bool ok = false;int j = min(n, i + k - 1);int value = w;for (int z = j; z >= i; z -- ) {if(a[z] <= value) {value += a[z];if(z == i) {ok = true;}} else {value = w;j = z;// i = j;;}}if(ok) {i = j;w = value;} else {cout << "NO\n";return;}}}cout << "YES\n";}int main(){std::ios::sync_with_stdio(false);std::cin.tie(nullptr);int t;cin >> t;while(t--) {solve();}return 0;} 边栏推荐
- 多御安全浏览器新版下载 | 功能优秀性能出众
- Mysql的redo log详解
- Open-Falcon of operation and maintenance monitoring system
- token、jwt、oauth2、session解析
- rpc-remote procedure call demo
- How to find all fields with empty data in sql
- 10 years of testing experience, worthless in the face of the biological age of 35
- [Software testing] unittest framework for automated testing
- Ice Scorpion V4.0 attack, security dog products can be fully detected
- XMjs cross-domain problem solving
猜你喜欢

Getting Started with Kubernetes Networking

bytebuffer 内部结构

Use Unity to publish APP to Hololens2 without pit tutorial

YYGH-13-客服中心

日志导致线程Block的这些坑,你不得不防

Shell script: for loop and the while loop
![[Paper Notes] MapReduce: Simplified Data Processing on Large Clusters](/img/89/8adef42b0cfd154e6fa7205afaeade.png)
[Paper Notes] MapReduce: Simplified Data Processing on Large Clusters

Acid (ACID) Base (BASE) Principles for Database Design
![[BJDCTF2020]EasySearch](/img/60/464de3bcdda876171b9f61ad31bff1.png)
[BJDCTF2020]EasySearch

Qixi Festival code confession
随机推荐
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
[CISCN2019 华东南赛区]Web11
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
Getting Started with Kubernetes Networking
日志导致线程Block的这些坑,你不得不防
Slapped in the face: there are so many testers in a certain department of byte
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
测试薪资这么高?刚毕业就20K
Dive into how it works together by simulating Vite
markdown如何换行——md文件
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
【树莓派】树莓派调光
队列题目:最近的请求次数
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
cross domain solution
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
ffmpeg pixel format basics
Haproxy搭建Web群集
bytebuffer put flip compact clear 方法演示