ZCMU—1465
来源:互联网 发布:出货单打单软件 编辑:程序博客网 时间:2024/05/21 14:04
1465: Post office
Time Limit: 1 Sec Memory Limit: 128 MB[Submit][Status][Web Board]
Description
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.
Input
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.
Output
For every query of each test case, you tell the minimum sum.
Sample Input
Sample Output
【分析】
#include <stdio.h>long long f[2000];long long a[2000];long long g[2000];int main() { int n; while (~scanf("%d",&n)&& n) { f[0]=0; for (int i=1;i<=n;i++) { scanf("%I64d",&a[i]); f[i]=f[i-1]+a[i]; } g[n+1]=0; for (int i=n;i>=1;i--) g[i]=g[i+1]+a[i]; int m;scanf("%d",&m); for (int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); int mid=(x+y)/2; long long ans=(a[mid]*(mid-x)-(f[mid-1]-f[x-1]))+((g[mid+1]-g[y+1])-a[mid]*(y-mid)); printf("%lld\n",ans); } } return 0; }
- ZCMU—1465
- ZCMU-1465-Post office
- ZCMU—1678
- ZCMU—1605
- ZCMU—1067
- ZCMU—1607
- ZCMU—1069
- ZCMU—1611
- ZCMU—1606
- ZCMU—1774
- ZCMU—1766
- ZCMU—1752
- ZCMU—1724
- ZCMU—1623
- ZCMU—1689
- ZCMU—1667
- ZCMU—1639
- ZCMU—1668
- 逻辑回归寻找最佳θ向量
- Android 图片轮播,图片左右自动滚动与页面侧滑菜单冲突,请问怎么解决?
- 通过单选按钮的value值判断一个文本框是否有用!求解决方案
- 双缓冲自绘制WinForm Listbox控件实现方法
- SpringMVC访问静态资源的三种方式
- ZCMU—1465
- U盘安装Windows和Ubuntu 15.04双系统图解教程
- js客户端加密
- 会场安排问题
- leetcode-389. Find the Difference
- c++ const
- HDU 1853 Cyclic Tour(最佳二分图匹配寻找图中的环)
- python学习笔记(四)
- Matlab 4. Matlab2016 不能保存数据(变量)的解决方法(中文版)-v7.3 switch