当前位置:网站首页>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;
}边栏推荐
- Bosses, I noticed that a mysql CDC connector parameters scan. The incremental. Sna
- What is the difference between SAP ERP and ORACLE ERP?
- UE4 opens doors with overlapping events
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- 关于#SQL#的迭代、父子结构查询问题,如何解决?
- Slapped in the face: there are so many testers in a certain department of byte
- Static method to get configuration file data
- [Software testing] unittest framework for automated testing
- 第一次性能测试实践,有“亿”点点紧张
- Defect detection (image processing part)
猜你喜欢

如何解决复杂的分销分账问题?

多御安全浏览器新版下载 | 功能优秀性能出众

How do newcomers get started and learn software testing?

MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation

UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)

token、jwt、oauth2、session解析

Getting Started with Kubernetes Networking

The test salary is so high?20K just graduated

Static method to get configuration file data

presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
随机推荐
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
MySql index learning and use; (I think it is detailed enough)
Redis key basic commands
日志导致线程Block的这些坑,你不得不防
Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
用Unity发布APP到Hololens2无坑教程
Haproxy搭建Web群集
token、jwt、oauth2、session解析
UE4 通过互动(键盘按键)开门
How to solve the three major problems of bank data collection, data supplementary recording and index management?
UE4 第一人称角色模板 添加生命值和调试伤害
【背包九讲——01背包问题】
Shell script: for loop and the while loop
High Item 02 Information System Project Management Fundamentals
达梦8数据库导出导入
多列属性column元素的可见性:display、visibility、opacity、垂直对齐方式:vertical-align、z-index 越大越显示在上层
Package zip is not available, but is referred to by another package.
运维监控系统之Open-Falcon
[Paper Notes] MapReduce: Simplified Data Processing on Large Clusters
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?