Http://acm.nyist.net/JudgeOnline/problem.php?pid=86
Find the ball number (a) time limit:MS | Memory limit:65535 KB Difficulty:3
-
Describe
A
-
game is popular in a country. The rules of the game are: in a pile of balls, each ball has an integer number I (0<=i<=100000000), the number can be duplicated, now say a random integer k (0<=k<=100000100), determine whether the ball number k is in this heap (exists as " YES, otherwise "NO"), the first answer is the winner. Now there is a man who wants to play the game, but he is very lazy. He wants you to help him win.
-
Input
-
The first line has two integers m,n (0<=n<=100000,0<=m<=1000000); M means there is a ball of m in the pile, n means the game is n times.
Next enter the M+n integer, the first m is the number I of the M ball, and the last n represents the random integer k in each game respectively.
-
Output
-
Output "YES" or "NO"
-
Sample input
-
6 423 34 46 768 343 3432 4 23 343
-
Sample output
Nonoyesyes
-
Ideas for solving A:set container find function
-
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <Set>5#include <algorithm>6 7 using namespacestd;8 9 intMain () {Ten intm,n,t; One Set<int>se; A Set<int>:: iterator it; -scanf"%d%d",&m,&n); - for(intI=0; i<m;i++){ thescanf"%d",&t); - Se.insert (t); - } - for(intI=0; i<n;i++){ +scanf"%d",&t); -it=Se.find (t); + if(It==se.end ()) puts ("NO"); A ElsePuts"YES"); at } - return 0; - } -
View Code
Problem-solving ideas b:sort, two-point search
Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <Set>5#include <algorithm>6 7 using namespacestd;8 9 intam[1000005];Ten intm,n,t; One A BOOLFind (intt); - - intMain () { thescanf"%d%d",&m,&n); - for(intI=0; i<m;i++){ -scanf"%d",&am[i]); - } +Sort (am,am+m); - for(intI=0; i<n;i++){ +scanf"%d",&t); A if(Find (t)) puts ("YES"); at ElsePuts"NO"); - } - return 0; - } - BOOLFind (intt) { - intL=0, r=m-1, MI; in while(l<=R) { -Mi= (L+R)/2; to if(am[mi]==t)return true; + Else if(am[mi]<t) l=mi+1; - Elser=mi-1; the } * return false; $}
View Code
nyoj86-Find the ball number (a) "Set binary search"