首页 > ACM题库 > HDU-杭电 > HDU 3763-CD-分治-[解题报告]HOJ
2015
04-10

HDU 3763-CD-分治-[解题报告]HOJ

CD

问题描述 :

Jack and Jill have decided to sell some of their Compact Discs, while they still have some value. They have decided to sell one of each of the CD titles that they both own. How many CDs can Jack and Jill sell?

Neither Jack nor Jill owns more than one copy of each CD.

输入:

The input consists of a sequence of test cases. The first line of each test case contains two non-negative integers N and M, each at most one million, specifying the number of CDs owned by Jack and by Jill, respectively. This line is followed by N lines listing the catalog numbers of the CDs owned by Jack in increasing order, and M more lines listing the catalog numbers of the CDs owned by Jill in increasing order. Each catalog number is a positive integer no greater than one billion. The input is terminated by a line containing two zeros. This last line is not a test case and should not be processed.

输出:

The input consists of a sequence of test cases. The first line of each test case contains two non-negative integers N and M, each at most one million, specifying the number of CDs owned by Jack and by Jill, respectively. This line is followed by N lines listing the catalog numbers of the CDs owned by Jack in increasing order, and M more lines listing the catalog numbers of the CDs owned by Jill in increasing order. Each catalog number is a positive integer no greater than one billion. The input is terminated by a line containing two zeros. This last line is not a test case and should not be processed.

样例输入:

3 3
1
2
3
1
2
4
0 0

样例输出:

2

TLE超时用scanf神奇的解决了。。。

用set超内存,囧,二分解决了

果然ACM好变态,老老实实用最原始的方法

#include<iostream>
#include<set>
#include<algorithm>
#include<string>
#include<stdio.h>
#define N 1000005

int m,n;
int tmp;
int ja[N];
int nsell;

bool serach(int v)
{
	int st=0,ed=m-1;
	int md;
	bool flag=false;
	while (st<=ed){
		md=(st+ed)/2;
		if(ja[md]>v){
			ed=md-1;
		}else if(ja[md]<v){
			st=md+1;
		}else{
			flag=true;
			break;
		}
	}

	return flag;
}
int main()
{
	while (std::cin>>m>>n&&(m!=0||n!=0)){
		for(int i=0;i!=m;i++){
			scanf("%d",&ja[i]);  
		}
		for(int i=0;i!=n;i++){
			scanf("%d",&tmp);  
			if(serach(tmp)){
				nsell++;
			}
		}

		std::cout<<nsell<<std::endl;

		memset(ja,0,sizeof(ja));
		nsell=0;
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/emiyasstar__/article/details/9131967


  1. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.