UVa 11136 Hoax or what (STL)

题意:有 n 天,每天有m个数,开始的前一天没有数据,然后每天从这个里面拿出一个最大的和最小的,求 n 天的最大的和最小的差值相加。

析:一看就知道用set啊,多简单的STL,不过要注意,开long long,和multiset,因为可能数是一样。

代码如下:

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <set>
#include <cstdio>

using namespace std;
typedef long long LL;
multiset<int> sets;
multiset<int> :: iterator it1, it2;

int main(){
    int n, m, x;
    while(scanf("%d", &n) == 1 && n){
        sets.clear();
        LL ans = 0;
        while(n--){
            scanf("%d", &m);
            for(int i = 0; i < m; ++i){
                scanf("%d", &x);
                sets.insert(x);
            }
            it1 = sets.end();
            it2 = sets.begin();
            ans += (LL)*--it1;
            ans -= (LL)*it2;
            sets.erase(it1);
            sets.erase(it2);
        }
        printf("%lld\n", ans);
    }
    return 0;
}
时间: 2024-10-17 20:34:45

UVa 11136 Hoax or what (STL)的相关文章

UVa - 12096 集合栈计算机(STL)

[题意] 有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两者的并集入栈,并输出并集的sizeINTERSECT:出栈两个集合,然后把二者的交集入栈,并输出交集的sizeADD:出栈两个集合,然后把先出栈的集合加入到后出栈的集合中,把结果入栈,并输出结果的size       每次操作后,输出栈顶集合的大小(即元素个数).例如栈顶元素是A={ {}, {{}

Uva 10815-Andy&#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

UVA 10375 Choose and divide(数论)

The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s). The Input Input consists of a sequence of lines. Each line contains four non-n

uva:10700 - Camel trading(贪心)

题目:10700 - Camel trading 题目大意:给出一些表达式,表达式由数字和加号乘号组成,数字范围[1,20].这些表达式可能缺少了括号,问这样的表达式加上括号后能得到的最大值和最小值. 解题思路:因为这些数的都是正整数,所以可以用贪心.不然看出最大值就是先做完加法在做乘法,最小值就是先做乘法在做加法.注意这里的数值要用long long 因为比表达式的值可能会超过int. 代码: #include <stdio.h> #include <string.h> cons

UVA 816 - Abbott&#39;s Revenge(BFS)

UVA 816 - Abbott's Revenge 题目链接 题意:一个迷宫,每个点限制了从哪一方向来的,只能往左右前走,然后问起点到终点的最短路径 思路:BFS,每个点拆成4个方向的点,对应能走的方向建图跑一下bfs即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace

UVA 12123 - Magnetic Train Tracks(计数问题)

题目链接:12123 - Magnetic Train Tracks 题意:给定n个点,求有几个锐角三角形. 思路:和UVA 11529是同类的题,枚举一个做原点,然后剩下点根据这个原点进行极角排序,然后利用two pointer去遍历一遍,找出角度小于90度的锐角,然后扣掉这些得到钝角三角形的个数,然后在用总情况去扣掉钝角就是锐角或直角 代码: #include <stdio.h> #include <string.h> #include <math.h> #incl

UVa10815 Andy&#39;s First Dictionary (STL)

链接:http://acm.hust.edu.cn/vjudge/problem/18649分析:set容器应用.set中每个元素最多只出现一次.自定义类型也可以构造set,必须定义“小于”运算符.set中的元素从小到大已排好序. 1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 #include <set> 5 #include <sstream> 6 using n

UVA - 11774 Doom&#39;s Day (规律)

We all know about the legend oftower of Hanoi. It is said that the world will end after finishing the puzzle.What we don't know is another legend about when the world will end which is verifiedby the scientists. It is all about a 3^n * 3^m grid.Initi