WHOJ 1603 - Minimum Sum【思维】
2016-04-09 21:24
417 查看
Problem 1603 - Minimum Sum
Time Limit: 2000MS Memory Limit: 65536KB
Total Submit: 561 Accepted: 154 Special Judge: No
Description
There are n numbers A[1] , A[2] .... A
, you can select m numbers of it A[B[1]] , A[B[2]] ... A[B[m]] ( 1 <= B[1] < B[2] .... B[m] <= n ) such that Sum as small as possible.
Sum is sum of abs( A[B[i]]-A[B[j]] ) when 1 <= i < j <= m.
Input
There are multiple test cases.
First line of each case contains two integers n and m.( 1 <= m <= n <= 100000 )
Next line contains n integers A[1] , A[2] .... A
.( 0 <= A[i] <= 100000 )
It's guaranteed that the sum of n is not larger than 1000000.
Output
For each test case, output minimum Sum in a line.
Sample Input
4 2
5 1 7 10
5 3
1 8 6 3 10
Sample Output
2
8
AC-code:
Time Limit: 2000MS Memory Limit: 65536KB
Total Submit: 561 Accepted: 154 Special Judge: No
Description
There are n numbers A[1] , A[2] .... A
, you can select m numbers of it A[B[1]] , A[B[2]] ... A[B[m]] ( 1 <= B[1] < B[2] .... B[m] <= n ) such that Sum as small as possible.
Sum is sum of abs( A[B[i]]-A[B[j]] ) when 1 <= i < j <= m.
Input
There are multiple test cases.
First line of each case contains two integers n and m.( 1 <= m <= n <= 100000 )
Next line contains n integers A[1] , A[2] .... A
.( 0 <= A[i] <= 100000 )
It's guaranteed that the sum of n is not larger than 1000000.
Output
For each test case, output minimum Sum in a line.
Sample Input
4 2
5 1 7 10
5 3
1 8 6 3 10
Sample Output
2
8
AC-code:
<pre name="code" class="cpp">#include<cstdio> #include<algorithm> using namespace std; int s[100005]; int main() { int n,m,i,k,j; long long sum,ans; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) scanf("%d",&s[i]); sort(s+1,s+n+1); ans=0x3f3f3f3f; for(i=1;i<=n-m+1;i++)//每个可能的数列的起始位置 { sum=0; for(j=1;j<=m;j++) sum+=(2*j-m-1)*s[i+j-1];//2*j-m-1=(j-1)-(m-j)即该数在该序列中前面的数的个数减去后面的数的个数 if(sum<ans) ans=sum; } printf("%lld\n",ans); } return 0; }
相关文章推荐
- Mysql常用语句和案例介绍
- matrix(No.1)operations
- 团队作业一 庆祝"十五万的总冠军"成立
- 简单配置搞定 Nginx + Tomcat + HTTPS
- uva 1585 Score
- 逻辑回归
- leetcode 110. Balanced Binary Tree-平衡二叉树|深度遍历
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
- CentOS 7.0 关闭防火墙和selinux
- matrix(No.1)operations
- synchronized的使用总结
- 《LeetBook》leetcode题解(4): Median of Two Sorted Arrays[H]——两个有序数组中值问题
- HDU 1074
- DeepLearning(Bengio,ch4)-数值计算
- MVC传值——Controller向View传值(一)
- 在非anctivity类中使用Toast
- 开车
- uva 1586 Molar mass
- 数据结构学习笔记(六)堆-优先队列
- 分析exec*函数对应的系统调用处理过程