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

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;
}

  

时间: 2024-08-16 01:28:12

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

Minimum Sum(思维)

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

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

L - Minimum Sum LCM Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10791 题意:输入正整数n,<注意n=2^31-1是素数,结果是2^31已经超int,用long long,>找至少两个数,使得他们的LCM为n且要输出最小的和: 思路:既然LCM是n,那么一定是n的质因子组成的数,又要使和最小,那么就是ans+=[

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

HDU 3473 Minimum Sum

Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2667    Accepted Submission(s): 609 Problem Description You are given N positive integers, denoted as x0, x1 ... xN-1. Then give you

UVA - 10791 - Minimum Sum LCM (数论相关!)

题目链接:Minimum Sum LCM UVA - 10791 Minimum Sum LCM Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

[email&#160;protected] Minimum sum partition (Dynamic Programming)

http://www.practice.geeksforgeeks.org/problem-page.php?pid=166 Minimum sum partition Given an array, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum. Input: The first line contains an i

hdu 3473 Minimum Sum(划分树-sum操作)

划分树.只是考虑求当前区间大于第k值的值得和,和小于第k值的和.显然可以在查询的时候直接搞出来.sum[d][i]表示第d层子区间l,r种l-i的和.写错了一个下标,检查了半辈子... #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue&g

HDOJ 3473 Minimum Sum

划分树,统计每层移到左边的数的和. Minimum Sum Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2959    Accepted Submission(s): 684 Problem Description You are given N positive integers, denoted as x0, x1 ... x

Linq扩展方法获取单个元素

在使用Linq 提供的扩展方法时,First(OrDefault), Single(OrDefault), Last(OrDefault)都具有返回单个元素的功能.MSDN对这些方法的描述只有功能说明,没有关于内部的相关实现的描述说明. 首先我们来看下MSDN上关于这些扩展方法的官方描述: First: 返回序列中的第一个元素 . FirstOrDefault: 返回序列中的第一个元素:如果未找到元素,则返回默认值. Last:返回序列的最后一个元素. LastOrDefault: 返回序列中的