UVa 11020 Efficient Solutions (BST)

题意:给按顺序给定 n 个人群,用x和y来描述,如果有没有任何一个x' < x y' <= y 或 x '<= x y' <= y,那么这个群体就是优势群体,

让你求出每放入一个人群,已经知道的群体有几个优势群体。

析:首先我们知道的是,如果某个群体失去了优势,那么该群体就不可能再获得优势,然后我们把已经得到的优势群体按x 从小到大排序,

那么得到曲线是一个向下的也就是严格递减的,所以我们就可以用multiset来维护所有的优势群体,然后我们考虑每加入一个群体,

如果在坐标上画出来的满足该要求,那么就是有优势,然后再删掉后面没有优势的。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
  int x, y;
  bool operator < (const Node &p) const{
    return x < p.x || (x == p.x && y < p.y);
  }
};
multiset<Node> sets;
multiset<Node> :: iterator it;

int main(){
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    if(kase > 1)   printf("
");
    sets.clear();
    printf("Case #%d:
", kase);
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
      int x, y;
      scanf("%d %d", &x, &y);
      it = sets.lower_bound((Node){x, y});
      if(it == sets.begin() || (--it)->y > y){
        sets.insert((Node){x, y});
        it = sets.upper_bound((Node){x, y});
        while(it != sets.end() && it->y >= y)  it = sets.erase(it);
      }
      printf("%d
", sets.size());
    }
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/6737762.html