5439.约翰种地
题目链接:https://www.acwing.com/problem/content/description/5442/
题意:
给定数组h,a,t,分别代表甘蔗高度,甘蔗每天能生长的高度,以及题目约束条件。
询问至少经过多少天,能使得对于所有的1<=i<=n,使得能有ti个甘蔗比第i个甘蔗高
思路:
题目给定的t数组是一个排列,也是一个排名,不难看出最后第0名比第1名高,第1名比第2名高····
假设经过k天,那么最后排名第i甘蔗高度为 h[i]+k*a[i],题目约束其高度要比第i+1名高
所以有不等式h[i]+k*a[i]>h[i+1]+k*a[i+1] =>
k*(a[i]-a[i+1])>h[i+1]-h[i]
显然k>=0,那么分类讨论即可
即分别讨论dh和da
注意不等式取交集
#include<bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<=n;i++) #define pb push_back #define int long long #define endl "\n" #define fi first #define se second //#pragma GCC optimize(3) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; const int inf=0x3f3f3f3f; const ll llmax=LLONG_MAX; const int maxn=2e5+5; const int mod=1e9+7; int n; int a[maxn]; int h[maxn]; pii t[maxn]; signed main() { ios::sync_with_stdio(false),cin.tie(0); int T;cin>>T; while(T--){ cin>>n; rep(i,1,n)cin>>h[i]; rep(i,1,n)cin>>a[i]; rep(i,1,n){ cin>>t[i].fi; t[i].se=i; } sort(t+1,t+1+n); bool ok=true; int l=-1,r=llmax; int ans; rep(i,1,n-1){ int x=t[i].se,y=t[i+1].se; int dh=h[x]-h[y]; int da=a[y]-a[x]; if(da==0){ if(dh<=0){ ok=false;break; } } else if(da>0){ if(dh<=0){ ok=false;break; }else{ int temp=(int)ceil(dh*1.0/da); if(temp<r)r=temp; } } else if(da<0){ if(dh<0){ int temp=(int)floor(dh*1.0/da); if(temp>l)l=temp; } } } if(n==1){ cout<<0<<endl; continue; } if(ok&&l+1<=r-1){ cout<<l+1<<endl; }else{ cout<<-1<<endl; } } return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现