当前位置:网站首页>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;
}
边栏推荐
- There are several common event handling methods in Swing?How to listen for events?
- Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
- 【树莓派】树莓派调光
- How do newcomers get started and learn software testing?
- 国学*周易*梅花易数 代码实现效果展示 - 梅花心易
- 2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
- Defect detection (image processing part)
- 结构体初解
- MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
- The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined
猜你喜欢
UE4 第一人称角色模板 添加生命值和调试伤害
MRTK3开发Hololens应用-手势拖拽、旋转 、缩放物体实现
21 Days Learning Challenge (2) Use of Graphical Device Trees
将故事写成我们
markdown如何换行——md文件
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
UE4 第一人称角色模板 添加冲刺(加速)功能
35岁的软件测试工程师,月薪不足2W,辞职又怕找不到工作,该何去何从?
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险
事件解析树Drain3使用方法和解释
随机推荐
burp安装及代理设置
Walter talked little knowledge | "remote passthrough" that something
pyqt5 + socket 实现客户端A经socket服务器中转后主动向客户端B发送文件
Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
Slapped in the face: there are so many testers in a certain department of byte
How to find all fields with empty data in sql
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
How to solve the three major problems of bank data collection, data supplementary recording and index management?
2022 High-level installation, maintenance, and removal of exam questions mock exam question bank and online mock exam
cross domain solution
炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit
public static <T> List<T> asList(T... a) 原型是怎么回事?
Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
UE4 opens door via interaction (keyboard key)
用Unity发布APP到Hololens2无坑教程
UE4 opens doors with overlapping events
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
What is the difference between SAP ERP and ORACLE ERP?