您的位置:首页 > 其它

FZU 2029 买票问题

2013-11-03 09:50 218 查看
好久没有做题了,开始做这道题时也不知道是咋想的,竟然有链表去实现,弄了好大时候也没弄出来,最后网上看了一下思路,

豁然开朗,用数组就可以实现,总体来说这题用的知识有map<int,int>映射:把输入的x映射为1,2,3...;还有就是树状数组

求"check x y"的值,再加上一个flag数组标记x是否在队列里边就OK了!具体看代码实现吧…………

#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define N 100005
int n, a
, num, top;
struct node{
int x, y;
node(int x, int y):x(x), y(y){}
node(){}
};

bool operator < (node a, node b){
return a.y > b.y;
}

int lowbit(int x){
return x & (-x);
}

void update(int x, int v){
while( x <= n ){
a[x] += v;
x += lowbit(x);
}
}

int sum(int x){
int ans = 0;
while( x > 0 ){
ans += a[x];
x -= lowbit(x);
}
return ans;
}

int main()
{
int t, x, y;
char ss[8];
while(~scanf("%d", &t)){
n = t;
top = 0, num = 1;
map<int, int> m;
bool flag
;
priority_queue<node> q;
memset(a, 0, sizeof(a));
memset(flag, false, sizeof(flag));
while(t--){
scanf("%s", ss);
if(ss[0] == 'l') {
while( !q.empty() && !flag[q.top().x] ) q.pop();
if( !q.empty() ) {
int k = q.top().x; q.pop();
flag[ k ] = false;
update(k, -1);
}
}
else if(ss[0] == 'p') {
while( top < num && !flag[top] ) top++;
if( top + 1 > num ) continue;
update( top, -1 );
flag[top] = false;
top++;
}
else if( ss[0] == 'a' ){
scanf("%d%d", &x, &y);
q.push(node(num, y));
flag[ m[x] = num ] = true;
num++;
}
else {
scanf("%d%d", &x, &y);
x = m[x];
if( !flag[x] ) continue;
int yy = x + sum(x-1) - 1;
printf("%d\n", yy);
if( yy > y ){
update( x, -1 );
flag[x] = false;
}
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: