考研机试贪心算法(二)

第一题:排队打水

思想: 将打水时间进行从小大到排序, 等待时间累加为res += p[i].t * (n - 1 - i);

第i 个人不需要等待自己的排队时间, 而是需要前面i-1个人的等待时间和

res += p[i].t * (n - 1 - i);

cpp 复制代码
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector> 
using namespace std;

int n;
struct node{
	int t,id;
	bool operator<(const node&b)const{
		return t<b.t;//按打水时间排序,从小大
	}
}; 

int main(){
	scanf("%d",&n);
	vector<node> p;
	for(int i =1;i<=n;i++){
		int a;
		cin>>a;
		p.push_back({a,i});//压入
	}
	sort(p.begin(),p.end());
	double res = 0;
	for(int i = 0;i<n;i++){//输出id排序
			printf("%d",p[i].id);
		if(i!=n)
			printf(" ");
	} 
	printf("\n"); 
	for(int i = 0;i<n;i++){
		res += p[i].t*(n-i-1); //计算时间 为: i 的时间 等于 前面 i-1个人的时间和
	}
	printf("%.2lf",res/n);//计算平均等待时间 
	return 0;
}

题目二: 仓库选址(区间选点)

思想: 先将仓库 位置进行升序排序, 取最小距离 为 res+=abs(a[i] -a[n/2]) 中间位置的绝对值

贪心思想: 就是把仓库建在中间位置

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
const int N =1010;
int n,a[N];
int main(){
	scanf("%d",&n);
	for(int i =0;i<n;i++){
		scanf("%d",&a[i]);
	}
	sort(a,a+n);
	int res = 0;
	for(int i = 0;i<n;i++)
		res += abs(a[i]-a[n/2]) ;//贪心 ,找中间位置绝对值和
	printf("%d",res);
	return 0;
} 

题目3:奶牛耍杂技(相邻交换的贪心)

相邻交换贪心 :

背景:必须是「顺序决策问题」------ 结果依赖于元素的排列顺序(比如调度、排队、叠放、路径选择) 先进行证明 相邻顺序的 大小

证明:交换论证法 通过数学对比,得出 "A 在 B 前更优" 的条件(如 w1+s1 < w2+s2

对其进行排序

思想:贪心规则, 按照 s+w的大小从小到大进行排序, 危险系数 = 前i-1个牛的体重和 减去 当i的压力系数

cpp 复制代码
#include<iostream>
#include<algorithm>
using namespace std;
int n;
const int N = 10010; 
struct node{
	int w,s;
	bool operator<(const node &t)const{
		return w+s<t.w+t.s;//相邻交换贪心
	}
}; 

int main(){
	int n;
	scanf("%d",&n);
	vector<node> E; 
	for(int i = 0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		E.push_back({a,b});
	}
	sort(E.begin(),E.end());//排序
	int val = 0,res= -2e9; 
	for(int i = 0;i<n;i++){
		res = max(res,val-E[i].s);//危险系数 前i-1个重量和 减去当前i个牛 压力指数
		val+=E[i].w;
	}
	printf("%d",res);	
	return 0;
}
相关推荐
小O的算法实验室17 小时前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
郭涤生18 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿18 小时前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz19 小时前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能19 小时前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****19 小时前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能19 小时前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能19 小时前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo19 小时前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法
Proxy_ZZ019 小时前
从零实现LDPC比特翻转译码器:C语言实战与底层逻辑解析
c语言·算法