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

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

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

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无敌大饺子 dot

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值