TOJ 2761.Buy One Get One Free

题目链接:http://acm.tju.edu.cn/toj/showp2761.html


2761.    Buy One Get One Free
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 1117    Accepted Runs: 409     Multiple test files



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).

Input

* 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.

Output

* Line 1: The maximum total number of bales of hay Farmer John can obtain.

Sample Input

3 4
6
1
3
1
5
3
4

Sample Output

5

Input Details

There are 3 bales of high quality hay, with sizes 6, 1, and 3, and 4 bales of low quality hay, with sizes 1, 5, 3, and 4.

Output Details

Obviously, Farmer John can buy all the bales of high quality hay. When he buys the size 6 high quality bale, he can get any low quality bale for free (say, the bale of size 3). When he buys the size 3 high quality bale, he can get the size 1 low quality bale for free. When he buys the size 1 high quality bale, however, he cannot get any low quality bales for free (since the size must be strictly less). The total, no matter how clever FJ is, comes to five bales.



Source: USACO 2007 February Competition
Submit   List    Runs   Forum   Statistics


水题,直接贪心就可,莫名其妙的过了(本来以为会超时的。。)


#include <stdio.h>
#include <algorithm>
using namespace std;
int low[10001],high[10001];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
		scanf("%d",&high[i]);
	for(int i=0;i<m;i++)
		scanf("%d",&low[i]);
	sort(high,high+n);
	sort(low,low+m);
	int sum=0;
	for(int i=n-1;i>=0;i--)
		for(int j=m-1;j>=0;j--){
			if(high[i]>low[j] && low[j]!=-1){
				sum++;
				low[j]=-1;
				break;
			}
		}
	printf("%d\n",sum+n);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值