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. 关于memecache的使用及清楚示意

    Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力.它可以应对任意多个连接,使用非阻塞 ...

  2. MSSQL 镜像

    1.设置数据库CollectionDB 为完整备份模式服务端: USE master ALTER DATABASE CollectionGuest SET RECOVERY FULL GO 镜相端: ...

  3. Android 属性动画(Property Animation) 全然解析 (下)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38092093 上一篇Android 属性动画(Property Animatio ...

  4. swift通过摄像头读取每一帧的图片,并且做识别做人脸识别

    最近帮别人做一个项目,主要是使用摄像头做人脸识别 github地址:https://github.com/qugang/AVCaptureVideoTemplate 要使用IOS的摄像头,需要使用AV ...

  5. smaba服务的搭建

    一. samba配置1. 什么是sambaSamba服务类似于windows上的共享功能,可以实现在Linux上共享文件,windows上访问,当然在Linux上也可以访问到.是一种在局域网上共享文件 ...

  6. C#基础学习心得(二)

    索引器 class Program { static void Main(string[] args) { Employee e1 = new Employee(); e1[0] = "三& ...

  7. ref 关键字out关键字

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. U - stl 的 优先队列 Ⅰ

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  9. 关于ajax提交的公共接口的一大用处

    在项目框架搭建的时候,就写了ajax提交的公共接口,是想统一的日志和处理ajax返回的错误信息. 今天,却又帮我解决了另外一个问题:每次点开某个页面,有一个ajax请求总是会调用两次,于是打开chro ...

  10. .Net Service开发(一)

    一, 新增一个服务项目