原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18696
方法很多,我用的是快排+二分,练习一下stl lower_bound这个函数。。。
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 using std::sort; 7 using std::lower_bound; 8 const int Max_N = 100010; 9 struct node { 10 int v, id; 11 }rec[Max_N]; 12 int arr[Max_N]; 13 bool cmp(node &a, node &b) { 14 if (a.v == b.v) return a.id < b.id; 15 return a.v < b.v; 16 } 17 int main() { 18 #ifdef LOCAL 19 freopen("in.txt", "r", stdin); 20 freopen("out.txt", "w+", stdout); 21 #endif 22 int n, m, a, b, pos; 23 while (~scanf("%d %d", &n, &m)) { 24 for (int i = 0; i < n; i++) { 25 scanf("%d", &rec[i].v); 26 rec[i].id = i + 1; 27 } 28 sort(rec, rec + n, cmp); 29 for (int i = 0; i < n; i++) arr[i] = rec[i].v; 30 while (m--) { 31 scanf("%d %d", &a, &b); 32 pos = lower_bound(arr, arr + n, b) - arr; 33 node &k = rec[pos + a - 1]; 34 if (k.v == b) printf("%d ", k.id); 35 else puts("0"); 36 } 37 } 38 return 0; 39 }