当前位置:网站首页>1007 Climb Stairs (贪心 | C思维)
1007 Climb Stairs (贪心 | C思维)
2022-08-05 03:53:00 【Codiplay】
题意
小明要爬一个n层的楼,每一层都一个生命值为hi的怪。只有小明的攻击力a>=血量h的时候才能消灭这只怪兽。如果小明消灭了这只怪兽,他会吸收他的生命值等量地转化为自己的攻击力。初始小明在第0层,有攻击力a0。
小明有两种行动方式
一是向上传送i格,i<=k;二是向下走1格。一个楼层不能被爬2次
现在小明想爬到楼顶并消灭所有的怪兽,问小明能不能办到。
思路
分段处理。由于题目要求必须把所有怪物打完,所以跳过的怪物还必须得走回去打败才行。我们要从当前位置x, 找到一个右端点r,使得可以按照r,r-1,r-2,x的顺序打败这一段的怪物。不难看出r更小的 时候不会更劣。
贪心。从后往前贪心。最远可以从 i + k - 1(相当于是小明跳到 i + k - 1之后一直往下走,走到 i 还能在跳跃到 i + k 层) 开始贪心,贪不动了就重置value(跳过这个硬骨头一会处理,先处理当前a[i]这个硬骨头),缩小贪心的范围,目的是为了使value >= a[i],如果达不到就输出NO
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int n,w,k;
void solve()
{
cin >> n >> w >> k;
std::vector<int> 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;
}
边栏推荐
- 2022.8.4-----leetcode.1403
- UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
- 36-Jenkins-Job迁移
- Growth-based checkerboard corner detection method
- UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
- Common open source databases under Linux, how many do you know?
- iMedicalLIS listener (2)
- 队列题目:最近的请求次数
- 银行数据采集,数据补录与指标管理3大问题如何解决?
- Spark Basics [Introduction, Getting Started with WordCount Cases]
猜你喜欢
基于生长的棋盘格角点检测方法
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
冰蝎V4.0攻击来袭,安全狗产品可全面检测
Haproxy搭建Web群集
Walter talked little knowledge | "remote passthrough" that something
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
YYGH-13-Customer Service Center
This year's Qixi Festival, "love vegetables" are more loving than gifts
用Unity发布APP到Hololens2无坑教程
随机推荐
ffmpeg 枚举decoders, encoders 分析
用Unity发布APP到Hololens2无坑教程
ASP.NET application--Hello World
After the large pixel panorama is completed, what are the promotion methods?
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
iMedicalLIS listener (2)
Growth-based checkerboard corner detection method
第一次性能测试实践,有“亿”点点紧张
UE4 通过重叠事件开启门
The sword refers to Offer--find the repeated numbers in the array (three solutions)
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
2022 Hangzhou Electric Multi-School 1st Game
如何解决复杂的分销分账问题?
UE4 opens doors with overlapping events
This year's Qixi Festival, "love vegetables" are more loving than gifts
Dive into how it works together by simulating Vite
Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
Queue Topic: Recent Requests
Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..