Minimum Sum(思维)
There are n numbers A[1] , A[2] .... A[n], 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.

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = ;
int A[MAXN];
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
for(int i = ; i < n; i++){
scanf("%d",&A[i]);
}
sort(A, A + n);
int ans = 0x3f3f3f3f;
for(int i = n - m; i >= ;i--){
int x = m - , temp = ;
for(int j = i + m - ; j >= i;j--){
temp += x * A[j];
// printf("x = %d a[%d] = %d temp = %d ",x,j,A[j],temp);
x -= ;
}
// printf("i = %d\n",i);
ans = min(ans, temp);
}
printf("%d\n",ans);
}
return ;
}
Minimum Sum(思维)的更多相关文章
- 数学 - Whu 1603 - Minimum Sum
Minimum Sum Problem's Link ------------------------------------------------------------------------- ...
- geeksforgeeks@ Minimum sum partition (Dynamic Programming)
http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...
- Minimum Sum LCM(uva10791+和最小的LCM+推理)
L - Minimum Sum LCM Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submi ...
- UVA.10791 Minimum Sum LCM (唯一分解定理)
UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...
- Minimum Sum of Array(map迭代器)
You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...
- HDU 3473 Minimum Sum(划分树)
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Minimum Sum of Array(map)
You are given an array a consisting of n integers a1, ..., an. In one operation, you can choose 2 el ...
- Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】
Problem 1603 - Minimum Sum Time Limit: 2000MS Memory Limit: 65536KB Total Submit: 623 Accepted: ...
- HDOJ 3473 Minimum Sum
划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K ...
随机推荐
- EXT JS 4.3 在线学习
官网地址:http://docs.sencha.com/extjs/4.1.3/ 相关示例:http://docs.sencha.com/extjs/4.1.3/#!/example Examples ...
- Php开发官方IDE ZEND
From http://www.zend.com/en/products/studio 注:唯一的缺点是收费.
- IBatis——(一)
IBatis是持久层的框架,也就是我们说的Dao层框架,关注数据库操作以及和Java对象之间的关联,我们将这样的框架也称之为ORM(Object/Relaction Mapping)框架.而这里映射的 ...
- 【沙茶了+筛选保存最大质因数】【HDU2136】Largest prime factor
Largest prime factor Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- stagefright框架(四)-Video Buffer传输流程
這篇文章將介紹Stagefright中是如何和OMX video decoder传送buffer. (1) OMXCodec會在一開始的時候透過read函式來傳送未解碼的data給decoder,並且 ...
- JQ 模仿注册时等待的时间
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- javascript 高级程序设计学习笔记(面向对象的程序设计)继承
ECMAScript中描述了原型链的概念,原型链是实现继承的主要方法. 实现原型链继承有一种基本模式 function SuperType () { this.property = true; } S ...
- hadoop压缩配置
为何要使用压缩,压缩可以是文件的大小减小很多,节省空间:另外压缩后的文件在传输时更节省带宽. 所需软件: 1)lzo 2)hadoop-lzo 3)maven 安装编译: 1)lzo wget htt ...
- Submission Details
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [转载]vs2010中臃肿的ipch和sdf文件
使用VS2010建立C++解决方案时,会生成SolutionName.sdf和一个叫做ipch的文件夹,这两个文件再加上*.pch等文件使得工程变得非常的庞大,一个简单的程序都会占用几十M的硬盘容量, ...