UVA11136 【Hoax or what】我的标题哪里空了

优先队列ADT
UVA11136 【Hoax or what】
题意:沃尔玛超市有n天做活动,每天有k笔交易。每次交易都将含有交易额的小票放在一个箱子中抽奖。每天从箱子中抽取最大和最小交易额的小票,并支出它们两个差的金额的钱给最大交易额的顾客。求n天沃尔玛超市总共支出。

做法有二:两个优先队列;multset。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<queue>
 5 using namespace std;
 6 int num[10000000];
 7 int main()
 8 {
 9     int n,m,a;
10     long long ans;
11     while(scanf("%d",&n)==1&&n)
12     {
13         priority_queue<int> da;
14         priority_queue<int,vector<int>,greater<int> >xiao;
15         memset(num,0,sizeof(num));
16         ans=0;
17         while(n--)
18         {
19             scanf("%d",&m);
20             for(int i=1;i<=m;++i)
21             {
22                 scanf("%d",&a);
23                 da.push(a);
24                 xiao.push(a);
25                 num[a]++;
26             }
27             while(!num[da.top()]) da.pop();
28             while(!num[xiao.top()]) xiao.pop();
29             int zuida=da.top();da.pop();num[zuida]--;
30             int zuixiao=xiao.top();xiao.pop();num[zuixiao]--;
31             ans+=zuida-zuixiao;
32         }
33         printf("%lld\n",ans);
34     }
35     return 0;
36 }
 1 #include <cstdio>
 2 #include <set>
 3  
 4 using namespace std;
 5  
 6 typedef long long ll;
 7  
 8 multiset<int> s;
 9 multiset<int>::iterator first, last;
10  
11 int main () {
12  
13     int n, k, num;
14     ll ans;
15     while (scanf ("%d", &n) && n) {
16  
17         ans = 0;
18         s.clear();
19         while (n--) {
20             
21             scanf ("%d", &k);
22             for (int i = 0; i < k; i++) {
23                 scanf ("%d", &num);
24                 s.insert (num);
25             }
26             first = s.begin();
27             last = s.end();
28             last--;
29             
30             ans += *last - *first; 
31             s.erase(first);
32             s.erase(last);
33         }
34         printf ("%lld\n", ans);
35     }
36     return 0;
37 }

猜你喜欢

转载自www.cnblogs.com/zytwan/p/9930544.html