UVA_11991
先将所有值排个序,剩下的工作就比较好办了。
#include<stdio.h> #include<string.h> #include<algorithm> #define MAXD 100010 struct Point { int id, v; bool operator < (const Point &t) const { if(v == t.v) return id < t.id; return v < t.v; } }p[MAXD]; int N, M; void init() { int i; for(i = 1; i <= N; i ++) { p[i].id = i; scanf("%d", &p[i].v); } std::sort(p + 1, p + 1 + N); } int BS(int v) { int min = 0, max = N, mid; for(;;) { mid = min + max + 1 >> 1; if(mid == max) break; if(p[mid].v >= v) max = mid; else min = mid; } return mid; } void solve() { int i, k, v, t; for(i = 0; i < M; i ++) { scanf("%d%d", &k, &v); t = BS(v) + k - 1; if(t > N || p[t].v != v) printf("0\n"); else printf("%d\n", p[t].id); } } int main() { while(scanf("%d%d", &N, &M) == 2) { init(); solve(); } return 0; }