UVa-11136 Hoax or what(优先队列或multiset)

来源:互联网 发布:制作简单 进销存 软件 编辑:程序博客网 时间:2024/05/17 04:29

题意:活动天数为n, 每天有k张小票, 每一天奖品的价值为最大的小票减最小的小票, 同时删除这两张小票。问n天的奖品总价值是多少。

考察multiset的基本用法,它和set的区别就是,multiset可存重复元素,set不能

#include<cstring>#include<iostream>#include<set>using namespace std;multiset<int> q;int n, k, a;int main(){while(scanf("%d", &n)==1 && n!=0){long long ans = 0;//ans范围要注意q.clear();multiset<int>::iterator minp,maxp;for(int i = 0; i<n; i++){    scanf("%d", &k);    for(int j = 0; j<k; j++){    scanf("%d", &a);    q.insert(a);    }    minp = q.begin();    maxp = q.end();    maxp--;//这里要注意    ans+=(*maxp-*minp);    q.erase(maxp);    q.erase(minp);    }    printf("%lld\n", ans); }return 0;}

原创粉丝点击