【CF1512F Education】题解
题目链接
题目
有一个长度为 的数组 和一个长度为 的数组 ,初始位置为 ,每一天可以选择得到 元钱,或者花费 元钱(钱数不能为负)使得
现在希望买一台 元的电脑,最少需要多少天。
Polycarp is wondering about buying a new computer, which costs tugriks. To do this, he wants to get a job as a programmer in a big company.
There are positions in Polycarp's company, numbered starting from one. An employee in position earns tugriks every day. The higher the position number, the more tugriks the employee receives. Initially, Polycarp gets a position with the number and has tugriks.
Each day Polycarp can do one of two things:
- If Polycarp is in the position of , then he can earn tugriks.
- If Polycarp is in the position of ( ) and has at least tugriks, then he can spend tugriks on an online course and move to the position .
For example, if , , , , then Polycarp can act like this:
- On the first day, Polycarp is in the -st position and earns tugrik. Now he has tugrik;
- On the second day, Polycarp is in the -st position and move to the -nd position. Now he has tugriks;
- On the third day, Polycarp is in the -nd position and earns tugriks. Now he has tugriks;
- On the fourth day, Polycarp is in the -nd position and is transferred to the -rd position. Now he has tugriks;
- On the fifth day, Polycarp is in the -rd position and earns tugriks. Now he has tugriks;
- On the sixth day, Polycarp is in the -rd position and earns tugriks. Now he has tugriks;
- Six days later, Polycarp can buy himself a new computer.
Find the minimum number of days after which Polycarp will be able to buy himself a new computer.
思路
从前往后枚举每个位置,设其为整个过程到达的最右的位置。
枚举的过程可以记录当前过程中 的最大值,然后当前过程中加的话只加这个词。
到达每个位置后在计算一下当前要达到 的最小值,最后再取个最小值就是答案。
Code
// Problem: CF1512F Education
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/CF1512F
// Memory Limit: 250 MB
// Time Limit: 2000 ms
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define N 200010
//#define M
//#define mo
int n, m, i, j, k, T;
int a[N], b[N], d, s, ans, mx, c;
int Z(int x, int y)
{
return x/y+(x%y ? 1 : 0);
}
signed main()
{
// freopen("tiaoshi.in","r",stdin);
// freopen("tiaoshi.out","w",stdout);
T=read();
while(T--)
{
d=-1; s=mx=0; ans=0x7fffffff;
n=read(); c=read();
for(i=1; i<=n; ++i) a[i]=read();
for(i=2; i<=n; ++i) b[i]=read();
for(i=1; i<=n; ++i)
{
if(s<b[i]) d+=Z(b[i]-s, mx), s+=Z(b[i]-s, mx)*mx;
++d; s-=b[i]; mx=max(mx, a[i]);
if(s<c) ans=min(ans, d+Z(c-s, mx));
// printf("%lld %lld %lld\n", s, d, ans);
}
printf("%lld\n", ans);
}
return 0;
}
总结
这道题一开始盲猜是dp,最后发现其实是个很简单的贪心。
核心思想中很重要的一个,就是当前过程如果加就只在当前最大值停留。
感觉和以前的一道什么湖边钓鱼很像。
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/16365234.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!