#UVa:10611-The Playboy Chimp

灆洢 2016-07-30 08:46:04

找出Query所要的數字在不遞減數列中的前後位置找出即可。

C++(0.010)

/*******************************************************/
/* UVa 10611 The Playboy Chimp                         */
/* Author: Maplewing [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;
}

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料