当前位置:网站首页>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;}
边栏推荐
- Android interview question - how to write with his hands a non-blocking thread safe queue ConcurrentLinkedQueue?
- Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
- [GYCTF2020]EasyThinking
- MRTK3 develops Hololens application - gesture drag, rotate, zoom object implementation
- Industry Status?Why do Internet companies prefer to spend 20k to recruit people rather than raise their salary to retain old employees~
- MySql的索引学习和使用;(本人觉得足够详细)
- DEJA_VU3D - Cesium功能集 之 058-高德地图纠偏
- iMedicalLIS监听程序(2)
- Mathematics - Properties of Summation Symbols
- You may use special comments to disable some warnings. 报错解决的三种方式
猜你喜欢
【8.4】代码源 - 【数学】【历法】【删库】【不朴素的数列(Bonus)】
JeeSite新建报表
Static method to get configuration file data
Web3.0 Dapps——通往未来金融世界的道路
商业智能BI业务分析思维:现金流量风控分析(一)营运资金风险
Getting Started with Kubernetes Networking
Leading the highland of digital medicine, Zhongshan Hospital explores to create a "new paradigm" for future hospitals
Use Unity to publish APP to Hololens2 without pit tutorial
Mysql的redo log详解
Shell script: for loop and the while loop
随机推荐
ffmpeg 像素格式基础知识
2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer appears after successful startup of presto
bytebuffer 内部结构
【8.1】代码源 - 【第二大数字和】【石子游戏 III】【平衡二叉树】
Redis key basic commands
Confessing the era of digital transformation, Speed Cloud engraves a new starting point for value
Qixi Festival code confession
Package zip is not available, but is referred to by another package.
36-Jenkins-Job迁移
Android Practical Development - Kotlin Tutorial (Introduction - Login Function Implementation 3.3)
pyqt5 + socket 实现客户端A经socket服务器中转后主动向客户端B发送文件
10 years of testing experience, worthless in the face of the biological age of 35
【树莓派】树莓派调光
Web3.0 Dapps——通往未来金融世界的道路
Fifteen. Actual combat - MySQL database building table character set and collation
如何解决复杂的分销分账问题?
UE4 第一人称角色模板 添加蹲伏功能
Haproxy搭建Web群集
presto启动成功后出现2022-08-04T17:50:58.296+0800 ERROR Announcer-3 io.airlift.discovery.client.Announcer
BI业务分析思维:现金流量风控分析(二)信用、流动和投资风险