当前位置:网站首页>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
- leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
- Initial solution of the structure
- 第一次性能测试实践,有“亿”点点紧张
- UE4 通过重叠事件开启门
- Queue Topic: Recent Requests
- 不看后悔,appium自动化环境完美搭建
- Static method to get configuration file data
- Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
- DEJA_VU3D - Cesium功能集 之 059-腾讯地图纠偏
猜你喜欢

Kubernetes 网络入门

YYGH-13-Customer Service Center

After the large pixel panorama is completed, what are the promotion methods?

mutillidae下载及安装

Why is the pca component not associated

冰蝎V4.0攻击来袭,安全狗产品可全面检测

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

Static method to get configuration file data

The second council meeting of the Dragon Lizard Community was successfully held!Director general election, 4 special consultants joined

UE4 opens doors with overlapping events
随机推荐
测试薪资这么高?刚毕业就20K
ASP.NET application--Hello World
数据库设计的酸(ACID)碱(BASE)原则
结构体初解
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
[GYCTF2020]EasyThinking
DEJA_VU3D - Cesium功能集 之 059-腾讯地图纠偏
Burp installation and proxy settings
905. Interval selection
Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
Android 面试题——如何徒手写一个非阻塞线程安全队列 ConcurrentLinkedQueue?
Ice Scorpion V4.0 attack, security dog products can be fully detected
Swing有几种常用的事件处理方式?如何监听事件?
[Filter tracking] based on matlab unscented Kalman filter inertial navigation + DVL combined navigation [including Matlab source code 2019]
Acid (ACID) Base (BASE) Principles for Database Design
36-Jenkins-Job迁移
Call Alibaba Cloud oss and sms services
Detailed and comprehensive postman interface testing practical tutorial
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)