当前位置:网站首页>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;} 边栏推荐
- Ice Scorpion V4.0 attack, security dog products can be fully detected
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- Open-Falcon of operation and maintenance monitoring system
- cross domain solution
- 2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
- Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
- 【背包九讲——01背包问题】
- UE4 为子弹蓝图添加声音和粒子效果
- 冰蝎V4.0攻击来袭,安全狗产品可全面检测
- Solana NFT开发指南
猜你喜欢

UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
![[极客大挑战 2019]FinalSQL](/img/e4/0c8225ef7c5e7e5bdbaac2ef6fc867.png)
[极客大挑战 2019]FinalSQL

UE4 第一人称角色模板 添加冲刺(加速)功能

Acid (ACID) Base (BASE) Principles for Database Design

The test salary is so high?20K just graduated

【测量学】速成汇总——摘录高数帮

How do newcomers get started and learn software testing?

MySql的索引学习和使用;(本人觉得足够详细)

Getting Started with Kubernetes Networking
![[MRCTF2020]PYWebsite](/img/d4/57e8e5ee45b742894679f3f5671516.png)
[MRCTF2020]PYWebsite
随机推荐
概率论的学习和整理8: 几何分布和超几何分布
Ffmpeg - sources analysis
运维监控系统之Open-Falcon
达梦8数据库导出导入
Paparazzi: Surface Editing by way of Multi-View Image Processing
Call Alibaba Cloud oss and sms services
How do newcomers get started and learn software testing?
[MRCTF2020]PYWebsite
iMedicalLIS listener (2)
The sword refers to Offer--find the repeated numbers in the array (three solutions)
UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
iMedicalLIS监听程序(2)
MySql index learning and use; (I think it is detailed enough)
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
Spark基础【介绍、入门WordCount案例】
Package zip is not available, but is referred to by another package.
UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
Web3.0 Dapps——通往未来金融世界的道路
ffmpeg 枚举decoders, encoders 分析
不看后悔,appium自动化环境完美搭建