Buy One Get One Free

Buy One Get One Free

总时间限制: 
2000ms 
内存限制: 
65536kB
描述

Farmer John has discovered the Internet is buying bales of hay online when he notices a special deal. For every bale of hay of size A (1 ≤ A ≤ 1,000,000) he buys, he can get a bale of hay of size B (1 ≤ B < A) for free!

The deal, however, is restricted: the larger bale must be high quality hay and the smaller one must be low quality hay. FJ, being a frugal and thrifty fellow, does not care: any quality of hay will do as long as he saves some money.

Given a list of the sizes of N (1 ≤ N ≤ 10,000) bales of high quality hay and M (1 ≤ M ≤ 10,000) bales of low quality hay, find the maximum number of bales of hay Farmer John can purchase. He can buy bales of high quality hay without getting the free bale of low quality hay, but he cannot buy bales of low quality hay (i.e., he must get them for free in the deal).


输入
* Line 1: Two space-separated integers: N and M.
* Lines 2..N + 1: Line i + 1 contains a single integer which is the size of the i-th bale of high quality hay.

* Lines N + 2 .. N + M + 1: Line i + N + 1 contains a single integer which is the size of the i-th bale of low quality hay.
输出
* Line 1: The maximum total number of bales of hay Farmer John can obtain.
样例输入
3 4
6
1
3
1
5
3
4
样例输出
5
贪心法,对两堆数,一堆按递增排序,一堆按递减排序,然后不断的匹配
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int f(const void *a,const void *b){
	return (*(int*)b-*(int*)a); 
}
int g(const void *a,const void *b){
	return (*(int*)a-*(int*)b); 
}

int main(){
	int a[10001];
	int b[10001];
	int n,m,i,j,k; 
	int count=0;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(j=0;j<m;j++){
		scanf("%d",&b[j]);
	}
	qsort(a,n,sizeof(int),g);
	qsort(b,m,sizeof(int),f);
	for(i=0;i<n;i++){   //a[0]最小  
		for(j=0;j<m;j++){  //b[0]最大 
		    if(a[i]>b[j]){
		    	b[j]=1000001;
		    	count+=2;
		    	break;
			}
		}
		if(j==m){
			count+=1;
		}
	}
	printf("%d",count);
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值