Problem 1603 - Minimum Sum
Time Limit: 2000MS   Memory Limit: 65536KB    Total Submit: 563  Accepted: 156  Special Judge: No
Description

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.

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[n].( 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
题解:题意就是求连续m个数字相互差的绝对值最小;

刚开始就想着先排序,从大到小模拟了下找到了3x1+x2-x3-3x4;由此可以看出规律;但是由于想着数据是1e5,就不敢写。。。然后队友写了下就过了。。。吊。。。其实是因为当N是1e5的时候,如果M也很大,那么i是从n-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(思维)的更多相关文章

  1. 数学 - Whu 1603 - Minimum Sum

    Minimum Sum Problem's Link ------------------------------------------------------------------------- ...

  2. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

    http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, ...

  3. Minimum Sum LCM(uva10791+和最小的LCM+推理)

    L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submi ...

  4. UVA.10791 Minimum Sum LCM (唯一分解定理)

    UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...

  5. 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 ...

  6. HDU 3473 Minimum Sum(划分树)

    Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. 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 ...

  8. Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】

    Problem 1603 - Minimum Sum Time Limit: 2000MS   Memory Limit: 65536KB   Total Submit: 623  Accepted: ...

  9. HDOJ 3473 Minimum Sum

    划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

随机推荐

  1. EXT JS 4.3 在线学习

    官网地址:http://docs.sencha.com/extjs/4.1.3/ 相关示例:http://docs.sencha.com/extjs/4.1.3/#!/example Examples ...

  2. Php开发官方IDE ZEND

    From http://www.zend.com/en/products/studio 注:唯一的缺点是收费.

  3. IBatis——(一)

    IBatis是持久层的框架,也就是我们说的Dao层框架,关注数据库操作以及和Java对象之间的关联,我们将这样的框架也称之为ORM(Object/Relaction Mapping)框架.而这里映射的 ...

  4. 【沙茶了+筛选保存最大质因数】【HDU2136】Largest prime factor

    Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. stagefright框架(四)-Video Buffer传输流程

    這篇文章將介紹Stagefright中是如何和OMX video decoder传送buffer. (1) OMXCodec會在一開始的時候透過read函式來傳送未解碼的data給decoder,並且 ...

  6. JQ 模仿注册时等待的时间

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. javascript 高级程序设计学习笔记(面向对象的程序设计)继承

    ECMAScript中描述了原型链的概念,原型链是实现继承的主要方法. 实现原型链继承有一种基本模式 function SuperType () { this.property = true; } S ...

  8. hadoop压缩配置

    为何要使用压缩,压缩可以是文件的大小减小很多,节省空间:另外压缩后的文件在传输时更节省带宽. 所需软件: 1)lzo 2)hadoop-lzo 3)maven 安装编译: 1)lzo wget htt ...

  9. Submission Details

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  10. [转载]vs2010中臃肿的ipch和sdf文件

    使用VS2010建立C++解决方案时,会生成SolutionName.sdf和一个叫做ipch的文件夹,这两个文件再加上*.pch等文件使得工程变得非常的庞大,一个简单的程序都会占用几十M的硬盘容量, ...