A26 贪心算法 P1190 [NOIP2010 普及组] 接水问题
视频链接:76 贪心算法 [NOIP2010 普及组] 接水问题_哔哩哔哩_bilibili
Luogu P1190 [NOIP2010 普及组] 接水问题
#include <iostream> #include <cstring> #include <algorithm> using namespace std; int n,m,w[10005]; //w:每人的接水量 int s[105]; //s:每个水龙头的出水量 int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&w[i]); for(int i=1,t; i<=n; i++){ t=1; // 找到出水量最少的水龙头 for(int j=2;j<=m;j++) if(s[t]>s[j]) t=j; s[t]+=w[i]; } int mx=0; for(int i=1;i<=m;i++) mx=max(mx,s[i]); printf("%d\n",mx); }
#include <iostream> #include <cstring> #include <algorithm> #include <queue> using namespace std; int n,m,w[10005]; //w:每人的接水量 priority_queue<int,vector<int>,greater<int> >s; //s:每个水龙头的出水量 int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&w[i]); for(int i=1;i<=m;i++) s.push(0); for(int i=1; i<=n; i++){ int t=s.top(); s.pop(); s.push(t+w[i]); } for(int i=1;i<m;i++) s.pop(); printf("%d\n",s.top()); }
Luogu P1376 [USACO05MAR] Yogurt factory 机器工厂
#include <iostream> #include <cstring> #include <algorithm> using namespace std; //上次的单价花费+s与当前单价花费做比较, //哪个花费更少,就取哪个。 int main(){ int n,s,c,y,last; long long ans=0; cin>>n>>s; for(int i=1;i<=n;i++){ cin>>c>>y; if(i==1) last=c; else last=min(last+s,c); ans+=last*y; } cout<<ans<<endl; return 0; }
分类:
A 基础算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!