标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5199
题目意思:给出鸟在树上的高度,以及射击到的高度,问每次射击能射中鸟的数量
赛中的时候这个代码是AC的,交到杭电就一直TLE了。。。。希望有心之人可以指点指点。忙中偷闲也~~~~
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <algorithm> 7 using namespace std; 8 9 const int maxn = 1e6 + 5; 10 int vis[maxn]; 11 vector<int>::iterator p1, p2; 12 vector<int> h; 13 14 int main() 15 { 16 #ifndef ONLINE_JUDGE 17 freopen("in.txt", "r", stdin); 18 #endif // ONLINE_JUDGE 19 20 int n, m, q, hi; 21 while (scanf("%d%d", &n, &m) != EOF) { 22 h.clear(); 23 for (int i = 0; i < n; i++) { 24 scanf("%d", &hi); 25 h.push_back(hi); 26 } 27 28 sort(h.begin(), h.end()); 29 memset(vis, 0, sizeof(vis)); 30 for (int i = 0; i < m; i++) { 31 scanf("%d", &q); 32 p1 = lower_bound(h.begin(), h.end(), q); 33 p2 = upper_bound(h.begin(), h.end(), q); 34 int pos = p1 - h.begin(); 35 int pos1 = p2 - h.begin(); 36 if (pos1 <= n && pos <= n && pos1 != pos) { 37 int t1 = h[pos], t2 = h[pos1]; 38 if (t1 == q) { 39 if (!vis[pos]) { 40 printf("%d\n", pos1-pos); 41 vis[pos] = 1; 42 } 43 else 44 printf("0\n"); 45 } 46 } 47 else 48 printf("0\n"); 49 } 50 } 51 return 0; 52 }
标签:
原文地址:http://www.cnblogs.com/windysai/p/4392953.html