BNU - Choosing a camera - 数据结构 (线段树、队列)

本文介绍了如何使用线段树和优先队列解决相机选择问题。线段树用于存储和更新所有相机信息,而优先队列则用于存储相机并在查询时按特定条件出队。只有当相机的参数满足大于或大于等于其余相机的条件时,它才被认为是不过时的。在这些不过时的相机中,选择价格最低且出现最早的相机。
摘要由CSDN通过智能技术生成

线段树存的是更新到当前所有的相机,队列则不管3721都把出现的camera塞进去。 

最后,在询问那里判断。 一个一个出队,询问这个相机是否有用。

队列是用了优先队列。


题意是:相机有两个参数和价格。

一个参数大于,一个参数大于等于剩下的所有相机,这个相机才不过时。

我们需要在不过时的相机里面挑选价格最便宜的。 价格相等的时候挑选最早出现的。

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define maxn 10000
#define inf 999999999
vector <int> x, y;
bool type[maxn];
#define lson l, m, rt << 1
#define rson m + 1, r , rt << 1 | 1
class Tree{
    public :
	int val[maxn << 2];
	void build(int l, int r, int rt){
		val[rt] = 0;
		if(l == r) return ;
		int m = (l + r) >> 1;
		build(lson); build(rson);
	}
	void update(int pos, int cc, int l, int r, int rt){
		if(l == r){
			val[rt] = cc;
			return ;
		}
		int m = (l + r) >> 1;
		if(pos <= m) update(pos, cc, lson);
		else update(pos, cc, rson);
		val[rt] = max(val[rt << 1] , val[rt << 1 | 1]);
	}
	int find_max(int L, int R, int l, int r, int rt){
		if(L <= l && r <= R){
			return val[rt];
		}int m = (l + r) >> 1;
		int ret = 0;
		if(L <= m) ret = max(ret, find_max(L,R, lson));
		if(r > m) ret = max(ret, find_max(L,R, rson));
		return ret;
	}
};
struct Node{
	int p, cost, id;
	int ra;
	Node(){}
	Node(int xx, int yy, int cc, int idd):p(xx), ra(yy), cost(cc), id(idd) {
	}
}q[maxn];
struct cmp{
    bool operator ()(const Node & aa, const Node & bb){
        if(bb.cost == aa.cost) return aa.id > bb.id;
        return aa.cost > bb.cost;
    }
};
priority_queue < Node, vector< Node >, cmp > qu;

//promise the node in the tree is always the efficient ones
//find the smallest --- use Min
char op[10];
int ans[maxn], anssub;
int main(){
	int T,n;
	scanf("%d",&T);
	while(T --){
	    x.clear(), y.clear();
		scanf("%d",&n); double ka;
		for(int i = 1; i <= n; i ++){
			scanf("%s",op);
			if(op[0] == 'P'){
				scanf("%d%lf%d", &q[i].p, &ka, &q[i].cost);
				q[i].ra = ka * 1000000 + 1e-8;
				x.push_back( q[i].p );
				y.push_back( q[i].ra );
				type[i] = 0;
			}else{
				type[i] = 1;
			}
			q[i]. id = i;
		}
		sort(x.begin(), x.end());
		sort(y.begin(), y.end());
		int nx = unique(x.begin(), x.end()) - x.begin();
		int ny = unique(y.begin(), y.end()) - y.begin();

		x.erase( unique(x.begin(), x.end()) , x.end());
		y.erase( unique(y.begin(), y.end()) , y.end());

		Tree a, b;
		a.build(0, nx, 1);
		b.build(0, ny, 1);
        while(!qu.empty()) qu.pop();
        anssub = 0;
        //printf("%d  %d size \n", x.size(), y.size());
		for(int i = 1; i <= n; i ++){
			if(type[i] == 1){//query
                while(!qu.empty()){
                    int vx = qu.top().p;
                    int vy = qu.top(). ra;

                    //printf("%d  %d  %d now to deal\n", vx, vy, qu.top().id);
                    if(vx < b.find_max(vy, ny, 0, ny, 1) || vy < a.find_max(vx, nx, 0, nx, 1)){
                        qu.pop();
                        //printf(" kakka %d\n", qu.top().id);
                        continue;
                    }
                    break;
                }
                if(qu.empty()) ans[anssub ++] = -1;
                else {
                    ans[anssub ++] = qu.top().id;
                }
			}else{
                int vx = lower_bound(x.begin(), x.end(),q[i].p) - x.begin();
                int vy = lower_bound(y.begin(), y.end(),q[i].ra) - y.begin();
                a.update(vx, vy, 0, nx, 1);
                b.update(vy, vx, 0, ny, 1);
                qu.push(Node(vx , vy , q[i].cost, i));
			}
		}
		if(anssub > 0){
            for(int i = 0; i < anssub - 1; i ++){
                printf("%d ",ans[i]);
            }
            printf("%d\n",ans[anssub - 1]);
		}
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值