找出Query所要的數字在不遞減數列中的前後位置找出即可。
C++(0.010)
/*******************************************************/ /* UVa 10611 The Playboy Chimp */ /* Author: LanyiKnight [at] knightzone.studio */ /* Version: 2016/07/30 */ /*******************************************************/ #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int main(){ int N, Q; while( scanf("%d", &N) != EOF ){ int chimps[50005] = {0}; for( int i = 0 ; i < N ; ++i ){ scanf("%d", &chimps[i]); } scanf("%d", &Q); int height; for( int i = 0 ; i < Q ; ++i ){ scanf("%d", &height); int *lower = lower_bound(chimps, chimps+N, height); int *upper = upper_bound(chimps, chimps+N, height); if( (lower == chimps+N) || *lower >= height ){ --lower; } if( (lower - chimps) < 0 ){ printf("X "); } else{ printf("%d ", *lower); } if( (upper - chimps) >= N ){ printf("X\n"); } else{ printf("%d\n", *upper); } } } return 0; }
回應