当前位置:网站首页>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;} 边栏推荐
- Event parse tree Drain3 usage and explanation
- 2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
- [TA-Frost Wolf_may-"Hundred Talents Project"] Graphics 4.3 Real-time Shadow Introduction
- 程序开发的一些常规套路(一)
- Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
- [Paper Notes] MapReduce: Simplified Data Processing on Large Clusters
- 不看后悔,appium自动化环境完美搭建
- 4T硬盘剩余很多提示“No space left on device“磁盘空间不足
- [Software testing] unittest framework for automated testing
- 结构体初解
猜你喜欢
随机推荐
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
[Paper Notes] MapReduce: Simplified Data Processing on Large Clusters
leetcode-每日一题1403. 非递增顺序的最小子序列(贪心)
【树莓派】树莓派调光
Mysql的redo log详解
flink读取mongodb数据源
冰蝎V4.0攻击来袭,安全狗产品可全面检测
iMedicalLIS监听程序(2)
Use Unity to publish APP to Hololens2 without pit tutorial
【8.3】代码源 - 【喵 ~ 喵 ~ 喵~】【树】【与】
从企业的视角来看,数据中台到底意味着什么?
2022 Hangzhou Electric Multi-School 1st Game
The sword refers to Offer--find the repeated numbers in the array (three solutions)
ffmpeg enumeration decoders, encoders analysis
银行数据采集,数据补录与指标管理3大问题如何解决?
You may use special comments to disable some warnings. Three ways to report errors
What is the difference between SAP ERP and ORACLE ERP?
bytebuffer put flip compact clear 方法演示
token, jwt, oauth2, session parsing
Redis1:Redis介绍、Redis基本特性、关系型数据库、非关系型数据库、数据库发展阶段


![[CISCN2019 华东南赛区]Web11](/img/15/843334fec0a5cc8cfaba92aab938db.png)




