第十四屆浙江財經大學程式設計競賽 A A Sad Story 貪心 __Algorithm

來源:互聯網
上載者:User

連結:https://www.nowcoder.com/acm/contest/89/A
來源:牛客網

時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld
題目描述
The Great Wall story of Meng Jiangnv’s Bitter Weeping happened during the Qin Dynasty (221BC- 206BC). Meng jiangnv was a beauty in the Qin Dynasty, and she lived happily with her husband. At that time, Emperor Qin Shihuang (the first 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 off to look for her husband. After a long journey, finally she reached the foot of the Great Wall at the present Shanhaiguan Pass. Upon her arrival, a bad news came to her, however, her husband had already died of exhaustion and was buried into the Great Wall! Meng could not help crying. She sat on the ground and cried and cried. Suddenly with a tremendous noise, a 400 kilometer-long (248-mile-long) section of the wall collapsed over her bitter wail.
Today, Qin Shihuang gets N stones. The height of the ith stone is Ai. He will use all these stones to rebuild the Great Wall. In order to make the Great Wall more sturdy, the prime minister Li Si proposes a formula to calculate the “weakness” of the reconstructed Great Wall

The Bi is the height of the ith stone in the reconstructed Great Wall, and the K is 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 120 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 11, respectively.

Now, Li Si wants to know the minimum value of “weakness”. Li Si is too old to calculate the answer quickly, so he asks you for help.

輸入描述:
The first line contains an integer T, where T is the number of test cases. T test cases follow.
For each test case, the first line contains two integers N and K, where N is the number of stones and K is a variable which provided by Li Si.

The second line contains N integers A1, A2, … , AN, where Ai is the height of the ith stone that QinShiHuang gets.
• 1 ≤ T ≤ 50.

• 1 ≤ N ≤ 103.
• 1 ≤ K ≤ N.
• 1≤ Ai ≤104.
輸出描述:
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”.
樣本1
輸入
2
5 2
1 2 3 4 5
5 3
1 3 2 2 7
輸出
Case #1: 4
Case #2: 7
備忘:
For the first case, one of 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.

分析:排個序,遍曆一下就可以了。

#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 long intint 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;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.