POJ·Sunscreen【贪心】

本文解析了POJ3614题目——防晒霜问题,通过排序算法和匹配策略,解决如何最大化数量的奶牛在不被晒伤的情况下享受阳光浴。文章详细阐述了解题思路和代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description–

有C个奶牛去晒太阳 (1 <=C <= 2500),每只奶牛各自能够忍受的阳光强度有一个minSPF[i]和一个maxSPF[i]值,太大就晒伤了,太小奶牛没感觉。

而刚开始的阳光的强度非常大,奶牛都承受不住,然后奶牛就得涂抹防晒霜,防晒霜的作用是让阳光照在身上的阳光强度固定为某个值。

那么为了不让奶牛烫伤,又不会没有效果,给出了L种防晒霜 (1 <=L <= 2500)。每种的数量cover[i]和固定的阳光强度SPF[i]也给出来了

每个奶牛只能抹一瓶防晒霜,最后问能够享受阳光浴的奶牛有几只。


Input–

  • Line 1: Two space-separated integers: C and L

  • Lines 2…C+1: Line i describes cow i’s lotion requires with two integers: minSPFi and maxSPFi

  • Lines C+2…C+L+1: Line i+C+1 describes a sunscreen lotion bottle i with space-separated integers: SPFi and coveri

Output–

A single line with an integer that is the maximum number of cows that can be protected while tanning


Sample Input–

3 2
3 10
2 5
1 5
6 2
4 1

Sample Output–

2

解题思路–

先排个序~~

  • 1.按照最小值递减的顺序把奶牛排序
  • 2.按照防晒霜值递减的顺序把防晒霜排序

然后 举个栗子在这里插入图片描述


代码–

#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int c,l,ans;
struct cl
{
	int x,y;
}cc[2505],ll[2505];
bool clc(cl xx,cl yy)
{
	return xx.x>yy.x;
}
int main()
{
	scanf("%d%d",&c,&l);
	for (int i=1;i<=c;++i)
	  scanf("%d%d",&cc[i].x,&cc[i].y);
	for (int i=1;i<=l;++i)
	  scanf("%d%d",&ll[i].x,&ll[i].y);
	sort(cc+1,cc+c+1,clc);
	sort(ll+1,ll+l+1,clc);
	for (int i=1;i<=c;++i)
  	  for (int j=1;j<=l;++j)
		if (ll[j].x>=cc[i].x && ll[j].x<=cc[i].y && ll[j].y)//可以抹
		{
		  	ll[j].y--;
		  	ans++;
		  	break;
		}
	printf("%d",ans);
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值