当前位置:网站首页>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;} 边栏推荐
- DEJA_VU3D - Cesium功能集 之 056-智图Arcgis地图纠偏
- 工业级远距离无线传输装置的功能有哪些?
- Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
- Initial solution of the structure
- DEJA_VU3D - Cesium功能集 之 057-百度地图纠偏
- UE4 通过与其它Actor互动开门
- DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
- Event parse tree Drain3 usage and explanation
- Increasing leetcode - a daily topic 1403. The order of the boy sequence (greed)
- [MRCTF2020]Ezpop(详解)
猜你喜欢

There are several common event handling methods in Swing?How to listen for events?

UI自动化测试 App的WebView页面中,当搜索栏无搜索按钮时处理方法
![[论文笔记] MapReduce: Simplified Data Processing on Large Clusters](/img/89/8adef42b0cfd154e6fa7205afaeade.png)
[论文笔记] MapReduce: Simplified Data Processing on Large Clusters

MySql index learning and use; (I think it is detailed enough)

银行数据采集,数据补录与指标管理3大问题如何解决?

Paparazzi: Surface Editing by way of Multi-View Image Processing
![[CISCN2019 华东南赛区]Web11](/img/15/843334fec0a5cc8cfaba92aab938db.png)
[CISCN2019 华东南赛区]Web11

不看后悔,appium自动化环境完美搭建

七夕节代码表白

Growth-based checkerboard corner detection method
随机推荐
35岁的软件测试工程师,月薪不足2W,辞职又怕找不到工作,该何去何从?
[BJDCTF2020]EasySearch
Mysql的redo log详解
mutillidae下载及安装
七夕节代码表白
UE4 第一人称角色模板 添加生命值和调试伤害
大佬们,我注意到mysql cdc connector有参数scan.incremental.sna
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
Swing有几种常用的事件处理方式?如何监听事件?
ffmpeg -sources分析
[CISCN2019 华东南赛区]Web11
2022软件测试工程师最全面试题
【8.2】代码源 - 【货币系统】【硬币】【新年的问题(数据加强版)】【三段式】
银行数据采集,数据补录与指标管理3大问题如何解决?
There are several common event handling methods in Swing?How to listen for events?
bytebuffer 使用demo
[SWPU2019]Web1
调用阿里云oss和sms服务
You may use special comments to disable some warnings. Three ways to report errors