[Greedy] PLES

Title Description

There are N boys and N girls at the party, everyone had the amount of their height. Every boy just dancing with the girls, and girls only danced with boys. Each person can only have one partner. Boy or want to and higher than their girls to dance, or want to be lower than their girls to dance, the same, the girl is higher than their own or want and boys dancing, or want to own low-boy dance.

You can decide how many of the most able to dance together it?
Entry

The first line is a positive integer N (1 <= N <= 100000), indicates the number of men and women. The second row includes N absolute value of an integer from 1500 to 2500, the absolute value of each integer represents the height of each of the boy. If it is a positive integer representing this man than his love and dancing girls, if it is a negative integer, it means that this man likes and lower than his dancing girls. The third row includes N integers, each integer corresponding to the absolute value of the height of the girl. Similarly, if it is a positive integer, then, that this girl loves boy and taller than her dance, if it is a negative integer, then, that this girl likes and lower than her boy to dance.

Export

Only one line an integer representing the number can be used with most of the.

prompt
Here Insert Picture Description


Thinking

Today the AK, la la la la la (Tacca mad epilepsy). . .

The boys and girls are divided into positive and negative, with, it must be negative while the positive side, and then sort + greedy. . . Descending positive, negative, from small to large, the absolute value of the deposit when the deposit
Here Insert Picture Description
to male positive and negative, for example women
Here Insert Picture Description
after non-stop mix into
Here Insert Picture Description


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int male_z[100100],male_f[100100],female_z[100100],female_f[100100],Gun;
//male是男,female是女,z是正,f是负。打这么长,只是为了让我的程序变得高B格一点
int n,male_z_n,female_z_n,male_f_n,female_f_n;
bool cmp(int x,int y){
	return x>y;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		if(x>0)male_z[++male_z_n]=x;
		   else male_f[++male_f_n]=abs(x);
	}
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		if(x>0)female_z[++female_z_n]=x;
		   else female_f[++female_f_n]=abs(x);
	}//把正负分开存
	
	sort(male_z+1,male_z+1+male_z_n,cmp);
	sort(female_z+1,female_z+1+female_z_n,cmp);
	sort(male_f+1,male_f+1+male_f_n);
	sort(female_f+1,female_f+1+female_f_n);
	
	for(int j=1,i=male_z_n;j<=female_f_n&&i>0;j++)
		if(male_z[i]<female_f[j])--i,Gun++;
	for(int j=1,i=female_z_n;j<=male_f_n&&i>0;j++)
		if(female_z[i]<male_f[j])--i,Gun++;//贪心
	printf("%d",Gun);
}
Published 45 original articles · won praise 0 · Views 377

Guess you like

Origin blog.csdn.net/qq_39940018/article/details/102752605