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 ... 
随机推荐
- Google API v3 设置Icon问题处理
			1.查看API实现 //虽然比较符合API实现的思想但这个没法; //会产生Uncaught TypeError: undefined is not a function //google API n ... 
- UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>
			B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 2048/1024KB (Java/Others) Submit ... 
- ubuntu查看硬件信息
			1,外部探针probe sudo apt-get install hwinfo 执行hwinfo获取系统信息 --short 
- wget命令1(转载)
			Linux系统中的wget是一个下载文件的工具,它用在命令行下.对于Linux用户是必不可少的工具,我们经常要下载一些软件或从远程服务器恢复备份到本地服务器.wget支持HTTP,HTTPS和FTP协 ... 
- 一个人的旅行(floyd+dijskra+SPFA+Bellman)
			一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ... 
- 为啥NSString的属性要用copy而不用retain
			之前学习生活中,知道NSString的属性要用copy而不用retain,可是不知道为啥,这两天我研究了一下,然后最终明确了. 详细原因是由于用copy比用retain安全,当是NSString的时候 ... 
- Lazy方式单列模式,一种线程安全模式的新选择
			public class WeProxyClient { private static readonly Lazy<WeProxyClient> list = new ... 
- java学习笔记day05
			1.final关键字:防止被继承的类或覆写的方法修改,变量或方法被final定义后 会在内在中存在 特点: 1)可以修饰类.函数.变量. 2)被final修饰的类不可以被继承. 3)被f ... 
- Unity 之圆环算法
			首先我们要明白圆环生成的原理,其实说白了并不是圆环,而是圆.因为我们使用的预制物体时Cube(物体本身是有大小的)难免会有发生实物的折叠看起来给人的感觉是圆环而已. 1.1 几何中我们要画一个圆,因为 ... 
- App Store不能下载一直等待中的两种解决办法
			1,重启手机,之后确认是否得到改善 2,重启不行,更改WiFi的dns为114.114.114.114或者223.5.5.5 或 223.6.6.6,再重启手机 ps:我是第二种方法 
