2013
12-13

九度-1173-查找[解题代码]

5
1 5 2 4 3
3
2 5 6

YES
YES
NO

java 代码如下：
import java.util.Arrays;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNextInt()){
int n = s.nextInt();
int[] arr = new int[n];
for(int i=0; i<n; i++){
arr[i] = s.nextInt();
}
Arrays.sort(arr);

int m = s.nextInt();
for(int i=0; i<m; i++){
if((Arrays.binarySearch(arr, s.nextInt())) > -1){
System.out.println("YES");
}else
System.out.println("NO");
}
}
}

}

/**************************************************************
Problem: 1173
User: coder
Language: Java
Result: Accepted
Time:160 ms
Memory:16048 kb
****************************************************************/

1. #include <cstdio>
#include <algorithm>

struct LWPair{
int l,w;
};

int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=5000, MAXVAL=10000;
LWPair sticks[MAXSIZE];
int store[MAXSIZE];
int ncase, nstick, length,width, tmp, time, i,j;
if(scanf("%d",&ncase)!=1) return -1;
while(ncase– && scanf("%d",&nstick)==1) {
for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
for(time=-1,i=0;i<nstick;++i) {
tmp=sticks .w;
for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
if(j==time) { store[++time]=tmp; }
else { store[j+1]=tmp; }
}
printf("%dn",time+1);
}
return 0;
}