当前位置:网站首页>1007 Climb Stairs (greedy | C thinking)
1007 Climb Stairs (greedy | C thinking)
2022-08-05 03:59:00 【Codiplay】
Intention
Xiao Ming wants to climb a building with n floors, and each floor has a monster with a life value of hi.This monster can only be destroyed when Xiao Ming's attack power a>= health h.If Xiao Ming destroys this monster, he will absorb his HP and convert it into his own attack power.Initially, Xiao Ming is on the 0th floor and has an attack power of a0.
Xiao Ming has two ways of acting
One is to move up i cells, i<=k; the other is to go down 1 cell.A floor cannot be climbed twice
Now Xiaoming wants to climb to the top of the building and destroy all the monsters, ask Xiaoming if he can do it.
Thoughts
Segmented processing.Since the title requires that all monsters must be beaten, the skipped monsters have to go back to defeat them.We need to find a right endpoint r from the current position x, so that the monsters in this section can be defeated in the order of r, r-1, r-2, x.It is not difficult to see that it is not worse when r is smaller.
greedy.Greed from back to front.The farthest can be from i + k - 1 (equivalent to Xiaoming jumping to i + k - 1 and then going down, and when you reach i, you can still jump to the i + k layer) Start greedy, and reset the value if you are not greedy.(Skip this hard bone for a while, and process the current a[i] hard bone first), reduce the scope of greed, the purpose is to make value >= a[i], if it is not reached, output NO
#includetypedef long long ll;using namespace std;int n,w,k;void solve(){cin >> n >> w >> k;std::vector 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;}
边栏推荐
- 结构体初解
- How to wrap markdown - md file
- 2022.8.4-----leetcode.1403
- 炎炎夏日教你利用小米智能家居配件+树莓派4接入Apple HomeKit
- Package zip is not available, but is referred to by another package.
- YYGH-13-Customer Service Center
- 10 years of testing experience, worthless in the face of the biological age of 35
- GC Gaode coordinate and Baidu coordinate conversion
- Index Mysql in order to optimize paper 02 】 【 10 kinds of circumstances and the principle of failure
- token, jwt, oauth2, session parsing
猜你喜欢
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
[MRCTF2020]Ezpop(详解)
冰蝎V4.0攻击来袭,安全狗产品可全面检测
[CISCN2019 华东南赛区]Web11
事件解析树Drain3使用方法和解释
[BJDCTF2020]EasySearch
10 years of testing experience, worthless in the face of the biological age of 35
Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
Hard power or soft power, which is more important to testers?
How to solve the three major problems of bank data collection, data supplementary recording and index management?
随机推荐
UE4 为子弹蓝图添加声音和粒子效果
How to discover a valuable GameFi?
ffmpeg -sources分析
国学*周易*梅花易数 代码实现效果展示 - 梅花心易
多御安全浏览器新版下载 | 功能优秀性能出众
今年七夕,「情蔬」比礼物更有爱
[BJDCTF2020]EasySearch
Queue Topic: Recent Requests
【测量学】速成汇总——摘录高数帮
UE4 更改组件变量 (以修改第一人称角色模板的最大行走速度和跳跃高度为例)
1007 Climb Stairs (贪心 | C思维)
mutillidae下载及安装
Ice Scorpion V4.0 attack, security dog products can be fully detected
UE4 通过重叠事件开启门
token、jwt、oauth2、session解析
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
4T硬盘剩余很多提示“No space left on device“磁盘空间不足
程序开发的一些常规套路(一)
You may use special comments to disable some warnings. Three ways to report errors
冰蝎V4.0攻击来袭,安全狗产品可全面检测