当前位置:网站首页>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;
}边栏推荐
- Ali's local life's single-quarter revenue is 10.6 billion, Da Wenyu's revenue is 7.2 billion, and Cainiao's revenue is 12.1 billion
- This year's Qixi Festival, "love vegetables" are more loving than gifts
- [TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
- Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
- token, jwt, oauth2, session parsing
- After the large pixel panorama is completed, what are the promotion methods?
- Growth-based checkerboard corner detection method
- Bubble Sort and Quick Sort
- Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
- 36-Jenkins-Job Migration
猜你喜欢
随机推荐
DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
Ffmpeg - sources analysis
public static <T> List<T> asList(T... a) 原型是怎么回事?
DEJA_VU3D - Cesium功能集 之 059-腾讯地图纠偏
The test salary is so high?20K just graduated
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险
数组常用方法总结
How to wrap markdown - md file
多御安全浏览器新版下载 | 功能优秀性能出众
达梦8数据库导出导入
如何解决复杂的分销分账问题?
Dive into how it works together by simulating Vite
UE4 第一人称角色模板 添加蹲伏功能
开发Hololens遇到The type or namespace name ‘HandMeshVertex‘ could not be found..
Queue Topic: Recent Requests
You may use special comments to disable some warnings. Three ways to report errors
基于生长的棋盘格角点检测方法
Open-Falcon of operation and maintenance monitoring system
[TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction









