Problem 1603 - Minimum Sum
Time Limit: 2000MS   Memory Limit: 65536KB   
Total Submit: 623  Accepted: 178  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
 
题目大意:让你从n个数中挑出m个,求这m个数中任意两个元素差的绝对值,然后求和。问你最小的绝对值和为多少。
 
解题思路:首先可以确定的是,如果挑出的m个数越平稳,那么绝对值和最小。(当时没考虑到只要排序后,就是最平稳的了)所以问题转化成,如何求排序后的n个元素的序列中m个相连元素差的绝对值和最小。滑动窗口(计算机网络中的名词),当加入a[i]时,a[i-m]就要退出来,始终保证只有m个元素。那么只需要考虑加入和退出元素产生的贡献。
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int maxn = 1e5+200;
typedef long long LL;
int a[maxn], sum[maxn];
int main(){
int n, m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 1; i <= n; i++){
scanf("%d",&a[i]);
}
sort(a+1,a+1+n);
for(int i = 1; i <= n; i++){
sum[i] = sum[i-1]+a[i];
}
int ans = 0, tmp;
for(int i = 2; i <= m; i++){
ans += a[i]*(i-1) - (sum[i-1] - sum[0]);
}
tmp = ans;
if(n == m){
printf("%d\n",ans); continue;
}
for(int i = m+1; i <= n; i++){
tmp = tmp + a[i]*(m-1) - (sum[i-1] - sum[i-m]) - ((sum[i-1]-sum[i-m]) - a[i-m]*(m-1));
ans = min(ans,tmp);
}
printf("%d\n",ans);
}
return 0;
}

  

Whu 1603——Minimum Sum——————【单个元素贡献、滑窗】的更多相关文章

  1. 数学 - Whu 1603 - Minimum Sum

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

  2. Minimum Sum(思维)

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

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

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

  5. Selenium定位一 --单个元素定位方法

    Selenium-Webdriver 提供了强大的元素定位方法,支持以下三种方法. 单个对象的定位方法 多个对象的定位方法 层级定位 定位单个元素在定位单个元素时,selenium-webdriver ...

  6. geeksforgeeks@ Minimum sum partition (Dynamic Programming)

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

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

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

  8. 定义一个Collection接口类型的变量,引用一个Set集合的实现类,实现添加单个元素, 添加另一个集合,删除元素,判断集合中是否包含一个元素, 判断是否为空,清除集合, 返回集合里元素的个数等常用操作。

    package com.lanxi.demo2; import java.util.HashSet; import java.util.Iterator; import java.util.Set; ...

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

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

随机推荐

  1. VIN-Fusion config with Realsense D435i

    ### First shot Copy the .launch file in package VINS-Fusion to the directory of realsense2_cameara/l ...

  2. spring boot日志及Log4j日志配置

    1.默认实现的日志配置 Spring boot默认已经集成了logging,同时也是默认开启的,如果想根据自己的需求对日志进行配置,方法很简单--只需要在配置文件中进行相应设置,这里提供我自己的配置如 ...

  3. kali linux之webshell

    webacoo(web backdoor cookie) 类终端的shell 编码通信内容通过cookie头传输,隐蔽性较强 cm:base64编码的命令 cn:服务器用于返回数据的cookie头的名 ...

  4. 连接数据库+注册->登录->抽奖(有关联关系的接口)

    注册账号信息需要写入数据库,登录和抽奖时从数据库获取数据 一.连接数据库 my_sql.py: import pymysql class MyDb: def __init__(self,host,pa ...

  5. 【bzoj4036】[HAOI2015]按位或 fmt+期望

    Description 刚开始你有一个数字0,每一秒钟你会随机选择一个[0,2^n-1]的数字,与你手上的数字进行或(c++,c的|,pascal 的or)操作.选择数字i的概率是p[i].保证0&l ...

  6. 【转】IDEA快捷键功能说明及Eclipse对应操作

    1.Ctrl+z是撤销快捷键 2.如果想恢复Ctrl+z 掉的内容,按快捷键为:Ctrl + Shift + Z.方可 3.Ctrl-H(Browse Type Hierarchy) Ctrl + A ...

  7. Android SharedPreferences(数据存储,需掌握)

    1.SharePrefences类介绍 SharedPreferences 类提供了一个通用框架,以便您能够保存和检索原始数据类型的永久性键值对. 您可以使用 SharedPreferences 来保 ...

  8. 洛谷 P3157 [CQOI2011]动态逆序对(树套树)

    题面 luogu 题解 树套树(树状数组套动态开点线段树) 静态使用树状数组求逆序对就不多说了 用线段树代替树状数组,外面套树状数组统计每个点逆序对数量 设 \(t1[i]\)为\(i\)前面有多少个 ...

  9. 最近做了一个短网址服务 di81.com

    最近做了一个短网址服务:   di81.com 项目中有一处需求,需要把长网址缩为短网址,把结果通过短信.微信等渠道推送给客户.刚开始直接使用网上现成的开放服务,然后在某个周末突然手痒想自己动手实现一 ...

  10. git 下载 安装

    1.下载Git,官网地址:https://git-scm.com/,进入官网首页 在右下方的显示器中找到最新的版本下载,点击下载,跳转到下载页面 下载完成 2.安装Git 双击刚刚下载完成的安装文件, ...