WHU 1603--minimum Sum —————— "single element contribution, sliding window"

Source: Internet
Author: User

Problem 1603-minimum Sum Time Limit: 2000MS Memory Limit: 65536KB
Total Submit: 623 Accepted: 178 Special Judge: nodescription

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

Inputthere is multiple test cases.
First line of all case contains, 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 isn't larger than 1000000.OutputFor each test case and output minimum sum in a line. Sample Input4 2
5 1 7 10
5 3
1 8 6 3 10Sample Output2
8 topic: Let you choose M from the n number, the absolute value of any two element difference in the m number, and then sum. Ask you the minimum absolute value and how much it is. Problem-solving ideas: The first thing to be sure is that if the number of m selected is more stable, then absolute value and minimum. (At that time, it was not considered as long as it was the most stable after sorting), so the question was converted to how to find the absolute value and the minimum of M-connected element difference in the sequence of n elements ordered. Sliding window (noun in computer network), when adding a[i], A[i-m] will be returned, always guaranteed only m elements. Then you only need to consider the contribution of joining and exiting elements.
#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 —————— single element contribution, sliding window

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.