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