UVa 11369 - Shopaholic

來源:互聯網
上載者:User

標籤:blog   os   io   for   資料   2014   sp   log   amp   

題目:商店打折,每買三件商品中最便宜的不要錢,問最多省多少錢。

分析:貪心。

            如果只有3個商品的話,那一定是最便宜的錢省下來;

            如果有6個商品的話,在省下最便宜的基礎上,最好情況可以省下第3貴的商品的錢;

            由上述的推到過程可得貪心方法:

            將資料按從大到小排序,每次取前三個商品,可以省下其中最便宜的,此時最省錢;

            證明如下:

            定理:設有a > b > c > d > e > f,六個數字,分成兩組,最小值和最大為 c + f;

            證明:含有 f 的3個數中的最小值一定是 f,那另一組最大的最小值為 c;得證;

            現在這裡有n個數字,每3個分成一組,如果上述方法不對,則存在交換一對數能到更優解;

            這與上述定理矛盾;所以結論成立。

說明:衝進前1000了(⊙_⊙)。

#include <algorithm>#include <iostream>#include <cstdlib>#include <cstdio>using namespace std;int p[20004];int cmp(int a, int b){return a > b;}int main(){int t,n;while (~scanf("%d",&t))while (t --) {scanf("%d",&n);for (int i = 0 ; i < n ; ++ i)scanf("%d",&p[i]);sort(p, p+n, cmp);int sum = 0;for (int i = 2 ; i < n ; i+= 3)sum += p[i];printf("%d\n",sum);}return 0;}

UVa 11369 - Shopaholic

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.