蓝桥杯备考:贪心算法之排座椅

这道题的贪心策略就是每次都选能阻碍最多的横线或者纵线,我们可以把每个位置和阻隔学生数量绑定在一起,可能会想到哈希表或者数组,但是我们还要根据阻隔学生数量进行排序,so我们得开个结构体,然后写个cmp来规定排序才好,最后再把排好序的,如果阻隔数量相等的,按位置从小到大排序,也就是说我们需要排两次序

好的,我们来写一下代码吧

cpp 复制代码
#include <iostream>
#include <algorithm>
const int N = 1010;

using namespace std;

int n,m,k,l,d;

struct node{
	int x;
	int cnt;
}col[N],row[N];
bool cmp1(const node& p1,const node& p2)
{
	return p1.cnt>p2.cnt;
}
bool cmp2(const node &p1,const node& p2)
{
	return p1.x < p2.x;
}
int main()
{
	cin >> n >> m >> k >> l >> d;
	for(int i = 1;i<=m;i++)
	{
		col[i].x = i;
	}
	for(int i = 1;i<=n;i++)
	{
		row[i].x = i;
	}
	while(d--)
	{
		int x1,y1,x2,y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if(x1==x2) col[min(y1,y2)].cnt++;
		else if(y1==y2) row[min(x1,x2)].cnt++;
	}
	sort(col+1,col+1+m,cmp1);
	
	sort(row+1,row+1+n,cmp1);
	
	sort(col+1,col+1+l,cmp2);
	sort(row+1,row+1+k,cmp2);
	
	for(int i = 1;i<=k;i++)
	{
		cout << row[i].x << " ";
		
	}
	cout << endl;

	for(int i = 1;i<=l;i++)
	{
		cout << col[i].x << " ";
		
	}
	cout << endl;

	
	return 0;
}
相关推荐
ychqsq1 小时前
203.诊断
经验分享·职场和发展
ychqsq1 天前
201.春风
经验分享·职场和发展
Zhu7581 天前
离线二进制部署-Kubernetes-v1.36.4
容器·贪心算法·kubernetes
泡沫_花火1 天前
大模型开发相关岗位面试资料(含算法)
语言模型·自然语言处理·面试·职场和发展
Elastic 中国社区官方博客1 天前
Kubernetes attributes processor v1:它对 EDOT Collector 意味着什么
java·大数据·elasticsearch·搜索引擎·贪心算法·kubernetes·全文检索
我命由我123451 天前
Photoshop - Photoshop 快速共享作品
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop
我命由我123451 天前
自动化 / 智能装配的对象、内容、层级
运维·学习·职场和发展·自动化·求职招聘·职场发展·学习方法
AI职业加油站1 天前
大模型开发工程师证书怎么考?零基础学习路径与价值拆解
大数据·人工智能·学习·职场和发展·数据分析
blxr_1 天前
力扣hot100爬楼梯https://leetcode.cn/problems/climbing-stairs/
算法·leetcode·职场和发展
我命由我123452 天前
Photoshop - 将Photoshop图像置入其他应用程序
学习·ui·职场和发展·求职招聘·职场发展·学习方法·photoshop