UVA 11020 Efficient Solutions+multiset的应用

题目链接:点击进入

首先来讲,很容易看到我们其实只要维护优势人群的集合;如果加入一个新的人,我们首先看一下优势人群中是否有人会让这个人失去优势,如果没有,则将这个人插入集合中,但要注意到这个人的插入可能会让其它的人失去优势。所以要求这个集合要能支持快速查询和修改操作;而multiset恰好能能满足这个需要。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;

struct Point
{
    int a,b;

    ///Set中的元素按x进行排序
    bool operator <(const Point& rhs) const
    {
          return a<rhs.a||(a==rhs.a&&b<rhs.b);
    }
};

multiset<Point>S;
multiset<Point>::iterator it;

int main()
{
    //freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    for(int Case=1;Case<=T;Case++)
    {
        if(Case!=1)
          printf("\n");
        printf("Case #%d:\n",Case);

        int n,a,b;
        scanf("%d",&n);
        S.clear();

        while(n--)
        {
            scanf("%d%d",&a,&b);
            Point P=(Point){a,b};
            it = S.lower_bound(P); ///在红黑树中查找第一个小于P的元素
            if(it==S.begin()|| (--it)->b >=b ) ///如果P也具有优势
            {
                S.insert(P);
                it = S.upper_bound(P); ///it以后的元素都会被影响
                while(it!=S.end()&&it->b >=b) S.erase(it++); ///删除掉失去优势的人
            }
            printf("%d\n",S.size());
        }
    }
  return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-05 17:47:44

UVA 11020 Efficient Solutions+multiset的应用的相关文章

UVA 11020 - Efficient Solutions(set)

UVA 11020 - Efficient Solutions 题目链接 题意:每个人有两个属性值(x, y),对于每一个人(x,y)而言,当有另一个人(x', y'),如果他们的属性值满足x' < x, y' <= y或x' <= x, y' < y的话,这个人会失去优势,每次添加一个人,并输出当前优势人个数 思路:由于每个人失去优势后,不可能再得到优势,所以失去优势就可以当成删去这些点,这样的话,就可以用一个multiset来维护点集,每次加入一个点,利用lowerbound,

UVA - 11020 - Efficient Solutions (multiset实现BST)

Efficient Solutions 题目传送:Efficient Solutions AC代码: #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include <cstdio> #include <cctype> #include <string>

UVA - 11020 Efficient Solutions(Multiset)

本题利用multiset解决.根据题意,如果我们用P(x,y)表示一个人,因为人可以相同,所以用multiset.我们会发现,如果所有人群都是有优势的,那么这些点呈现一个递减的趋势.如果刚刚插入一个人,他是否有优势该如何判断呢?只需要看他左边相邻的点的y坐标是否比他小即可.而如果这个人是有优势的,那么需要先把这个人插入到集合中,然后从upper_bound(P)开始,逐个删除没有优势的点,注意删除时候应写为s.erase(it++),因为删除时候会导致指针向左移动一位,因此还需要it++.这样,

uva 11020 Efficient Solutions

题意:给你n个人,有两个属性x.y,如果不存在另外一个人x2,y2满足 x2<=x,y2<y 或者 x2<x,y2<=y,那么就称这个人是有优势的,每次给你一个人得信息,问你当前有优势的人的人数是多少? 思路:刘汝佳训练指南P228 mutiset+lower_bound+upper_bound 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<s

UVA 11020(Efficient Solutions-multiset的lower_bound)

C++的multiset,可重集: S.lower_bound() 指向迭代器的第一个ai>=k的元素 S.upper_bound() 指向迭代器的第一个ai>k的元素 本题可化为:有n个点坐标(a,b) 一开始平面上没点,每次向其中加一个点,问每次有多少个点,没有在它左下角(不包括本点)(x'<x,y'<=y||x'<=x,y'<y) 如果P.a<A.a||(P.a==A.a&&P.b<A.b),则另P<A 显然一个点一次不符合条件,

UVA 110020 Efficient Solutions (STL)

把一个人看出一个二维的点,优势的点就是就原点为左下角,这个点为右上角的矩形,包含除了右上角以外边界,其他任意地方不存在点. 那么所有有优势的点将会形成一条下凹的曲线. 因为可能有重点,用multiset,按照x优先,相同时再比较y的顺序排序,动态维护满足条件的总人数. 当新加的P点的y坐标大于左边的点的时候没有优势,忽略,用lower_bound判断一下. 当新加的P点有优势,但是可能使得其他的的点失去优势,依次把后面的点不满足条件的点删除. 红黑树好复杂. #include<bits/stdc

UVa11020 &#183; Efficient Solutions

题目:http://uva.onlinejudge.org/external/110/11020.pdf Problem IEfficient SolutionsInput: Standard Input Output: Standard Output "Our marriage ceremonies are solemn, sobermoments of reflection; also regret, disagreement,argument and mutual recriminatio

UVA - 12005 Find Solutions (最小因子分解)

Description  Find Solutions  Look at the following equation: c = ab - + 1 Now given the value of c, how many possible values of and a and b are there (a and b must be positive integers)? That is you will have to find the number of pairs (a, b) which

uva 125 Numbering Paths(warshall算法)

uva 125 Numbering Paths Description Download as PDF Background Problems that process input and generate a simple yes'' orno" answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general ef