P2107 小Z的AK计划 题解
分析
考虑一个贪心,由于 呈线性排列,我们一定要保证去到的机房越多越好,所以将 排序,每次前往最近的机房,如果可以 AK 则 AK,累加答案。
代码实现
我相信我的解法一定有漏洞,欢迎各位 dalao 来 hack。
#include <bits/stdc++.h> #define int long long using namespace std; inline int read(){ int x=0,f=0;char ch=getchar(); while(!isdigit(ch))f^=!(ch^45),ch=getchar(); while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); return f?-x:x; } int n,m,ans,pos; int x[100005]; int t[100005]; signed main(){ n=read();m=read(); for(int i=1;i<=n;i++)x[i]=read(),t[i]=read(); sort(x+1,x+n+1); for(int i=1;i<=n&&m;i++){ m-=abs(x[i]-pos);pos=x[i]; if(t[i]<=m)m-=t[i],ans++; } cout<<ans; //system("pause"); }
本文作者:tmjyh09
本文链接:https://www.cnblogs.com/tmjyh09/p/15902028.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步