当前位置:网站首页>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;} 边栏推荐
- 测试薪资这么高?刚毕业就20K
- Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
- The most comprehensive exam questions for software testing engineers in 2022
- [GYCTF2020]EasyThinking
- 结构体初解
- UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
- presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
- 国学*周易*梅花易数 代码实现效果展示 - 梅花心易
- There are several common event handling methods in Swing?How to listen for events?
- How do newcomers get started and learn software testing?
猜你喜欢

Web3.0 Dapps - the road to the future financial world

How to discover a valuable GameFi?

Developing Hololens encountered The type or namespace name 'HandMeshVertex' could not be found..

How to solve the three major problems of bank data collection, data supplementary recording and index management?

Based on holding YOLOv5 custom implementation of FacePose YOLO structure interpretation, YOLO data format conversion, YOLO process modification"

Use Unity to publish APP to Hololens2 without pit tutorial
![Spark Basics [Introduction, Getting Started with WordCount Cases]](/img/90/ebe887db0f8c36895691dea05f62cf.png)
Spark Basics [Introduction, Getting Started with WordCount Cases]

token、jwt、oauth2、session解析

Mysql的redo log详解

UE4 后期处理体积 (角色受到伤害场景颜色变淡案例)
随机推荐
Slapped in the face: there are so many testers in a certain department of byte
达梦8数据库导出导入
YYGH-13-客服中心
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险
Dameng 8 database export and import
UE4 在游戏运行时更改变量 (通过鼠标滑轮来更改第一人称角色的最大行走速度)
MySql index learning and use; (I think it is detailed enough)
数据库设计的酸(ACID)碱(BASE)原则
Shell script: for loop and the while loop
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
[BSidesCF 2019]Kookie
ffmpeg enumeration decoders, encoders analysis
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
Solana NFT开发指南
Static method to get configuration file data
mutillidae下载及安装
Web3.0 Dapps——通往未来金融世界的道路
UE4 通过与其它Actor互动开门
Paparazzi: Surface Editing by way of Multi-View Image Processing
2022 Hangzhou Electric Multi-School 1st Game