2015
04-10

# 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神奇的解决了。。。

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

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.