问题1468--Post office

1468: Post office

时间限制: 1 Sec  内存限制: 128 MB
提交: 56  解决: 19
[状态] [讨论版] [提交] [命题人:]
题目描述
There are N(N<=1000) villages along a straight road, numbered from 1 to N for simplicity. We know exactly the position of every one (noted pos[i],pos[i] is positive integer and pos[i]<=10^8). The local authority wants to build a post office for the people living in the range i to j(inclusive). He wants to make the sum of |pos[k]-position_of_postoffice| (i<=k<=j) is minimum.

输入
  For each test case, the first line is n. Then n integer, representing the position of every village and in acending order. Then a integer q (q<=200000), representing the queries. Following q lines, every line consists of two integers i and j. the input file is end with EOF. Total number of test case is no more than 10.
Be careful, the position of two villages may be the same.

输出
  For every query of each test case, you tell the minimum sum.

样例输入 Copy
3
1 2 3
2
1 3
2 3
样例输出 Copy
2
1
来源/分类