考研机试贪心算法(二)

第一题:排队打水

思想: 将打水时间进行从小大到排序, 等待时间累加为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;
}
相关推荐
小资同学1 小时前
考研机试之递归与贪心算法
算法·华为·贪心算法
我能坚持多久1 小时前
链式二叉树OJ问题详解
算法
2401_844221321 小时前
使用PictureBox实现图片缩放与显示的深入探讨
jvm·数据库·python·算法
Java面试题总结2 小时前
Go图像处理基础: image包深度指南
图像处理·算法·golang
C羊驼2 小时前
C 语言:哥德巴赫猜想
c语言·开发语言·人工智能·经验分享·笔记·算法·课程设计
田梓燊2 小时前
算法题学习题单
学习·算法
Sunsets_Red2 小时前
乘法逆元的 exgcd 求法
c++·学习·数学·算法·c#·密码学·信息学竞赛
阿Y加油吧2 小时前
力扣打卡——接雨水、无重复字符的最长子串
算法·leetcode·职场和发展