The 14th session of Zhejiang University of Finance and Economics Program Design Competition A a Sad Story greedy __algorithm

Source: Internet
Author: User

Link: Https://www.nowcoder.com/acm/contest/89/A
Source: Niu Ke Net

Time limit: c/s + + 1 seconds, other languages 2 seconds
Space restrictions: C/C + + 262144K, other languages 524288K
64bit IO Format:%lld
Topic description
The great Wall Story of Meng Jiangnv ' s bitter weeping happened during the Qin dynasty (221BC-206BC). Meng JIANGNV is a beauty in the Qin dynasty, and she lived happily with her husband. At this time, Emperor Qin Shihuang (the ' Emperor of Qin ') announced to build the great Wall. And the officials suddenly broke in their happy life and took Meng ' s husband away to build the wall. Because of the missing for her husband, she decided to set out to look for her husband. After a long journey, finally she reached the foot's the great Wall at the present Shanhaiguan. Upon her arrival, a bad news came to her, however, her husband, had already died of exhaustion and is buried into the Grea T wall! Meng could not help crying. She sat on the ground and cried and cried. Suddenly with a tremendous noise, a-Kilometer-long (248-mile-long) section of the wall collapsed over her bitter.
Today, Qin Shihuang gets N stones. The height of the ith stone is Ai. He'll use all of these stones to rebuild the great Wall. The great Wall more sturdy, the Prime Minister Li Si proposes a formula to calculate the "weakness" of th E reconstructed great Wall

The Bi is the height of the ith stone in the reconstructed great Wall, and the K are provided by Li Si.
For example, Qin Shihuang gets 5 stones. The height of these stones are [5,3,2,4,1], and the K is 2. There are different ways to rebuild the great Wall. The following figures show the two solutions:

The weakness of left figure and right figure are 4 and respectively.

Now, Li Si wants to know the minimum value of "weakness". Li Si is too old to calculate the answer quickly.

Enter a description:
The contains an integer T, where T is the number of test cases. T test Cases follow.
For the contains two integers n and K, where n are the number of stones and K is a variable whic H provided by Li Si.

The second line contains N integers A1, A2, ..., a, where Ai is the height of the ith stone that Qinshihuang gets.
1≤t≤50.

1≤n≤103.
1≤k≤n.
1≤ai≤104.
Output Description:
For each test case, print one line containing ' case #x: Y ', where x is the ' Test Case Number ' (starting from 1) and Y is the Minimum value of "weakness".
Example 1
Input
2
5 2
1 2 3 4 5
5 3
1 3 2 2 7
Output
Case #1:4
Case #2:7
Note:
For the "The" best ways is [1,2,3,4,5], weakness = (2−1) + (3−2) + (4−3) + (5−4) = 4.
For the "second case", one of the best ways is [7,3,2,2,1], weakness = (7−2) + (3−2) + (2−1) = 7.

Analysis: Arrange a sequence, iterate over it.

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
const int MAXN=1E5+10;
#define LL long int
int A[MAXN];
int main ()
{
    int T;
    scanf ("%d", &t); int cs=0;
    while (t--) {
        int m,n;cs++;
        scanf ("%d%d", &m,&n);
        for (int i=1;i<=m;i++) {
            scanf ("%d", &a[i]);
        Sort (a+1,a+1+m); ll ans=0;
        for (int i=n;i<=m;i++) {
            ans+=a[i];
            ANS-=A[I-N+1];
        }
        printf ("Case #%d:", CS);
        printf ("%lld\n", ans);
    }
    return 0;
}

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.

Tags Index: