BZOJ P2947 [POI2000] 促销【multiset】

题目描述

G r e a t   B y t e l a n d i s h 的超级市场网络请你编写一个程序模拟促销商品的成本费用推销商品要遵守以下规则:
1. 想参与促销的顾客在自己的帐单上写下个人信息,然后将票据投入一个特制的投票箱中。
2. 促销期间,每天结束后,有 2 张票据将被取出——消费金额最大的和最小的两张帐单。消费金额最大的那位顾客得到的奖品价值等于取出的 2 张帐单的差额。
3. 为了避免多次得奖,所有取出的帐单将不再放回箱中,其余的票继续参加促销活动.
由于商场的顾客特别多,所以每天投票箱中都至少有 2 张帐单。你的任务是计算在促销期间,商家一共要送出多少前的礼品。

这道题大概是很多的板题了(各种数据结构皆可),由于我最开始写 S p l a y 超时一直没有过所以就先写了 s e t 填上…

参考代码:

#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define SG string
#define DB double
#define LL long long
using namespace std;
LL N,M,Ans;
multiset<LL>Set;
multiset<LL>::iterator It; 
inline LL Read(){
    LL X=0;char CH=getchar();bool F=0;
    while(CH>'9'||CH<'0'){if(CH=='-')F=1;CH=getchar();}
    while(CH>='0'&&CH<='9'){X=(X<<1)+(X<<3)+CH-'0';CH=getchar();}
    return F?-X:X;
}
inline void Write(LL X){
    if(X<0)X=-X,putchar('-');
    if(X>9)Write(X/10);
    putchar(X%10+48);
}
int main(){
    LL I,J,K;
    N=Read();
    for(I=1;I<=N;I++){
        M=Read();
        for(J=1;J<=M;J++){
            K=Read();Set.insert(K);
        } 
        It=Set.begin();
        Ans-=*It;Set.erase(It);
        It=Set.end();It--;
        Ans+=*It;Set.erase(It);
    }Write(Ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yanzhenhuai/article/details/81089738