您的位置:首页 > 其它

hlg1398邮局问题【找中位数】

2015-04-25 18:09 141 查看
水题  直接上代码:





1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 using namespace std;
6
7 const int maxn = 1005;
8 int a[maxn];
9
10 int main() {
11     int n, m;
12     int x, y;
13     while(EOF != scanf("%d",&n) ) {
14         for(int i = 1; i <= n; i++) {
15             scanf("%d",&a[i]);
16         }
17         scanf("%d",&m);
18         while(m--) {
19             scanf("%d %d",&x, &y);
20             int pos = ( x + y ) / 2;
21             long long sum = 0;
22             for(int i = x; i <= y; i++) {
23                 sum += (long long) ( fabs(a[pos] - a[i]) );
24             }
25             printf("%lld\n", sum);
26         }
27     }
28     return 0;
29 }


View Code
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: