题目链接:买票问题

思路:优先队列维护忍耐度最低的人在队首,leave操作ok.
vis数组记录从1到n的编号的人们是不是在队列中,top维护队首的人的编号。pop操作搞定。
然后,check操作就是在vis数组中查找当前编号之前有多少个为1的数,树状数组大法好。
啊...数据虽然很大,但是t<=100000,所以可以用map把所有的编号标记为1-100000之间的...

dbug全程:开始树状数组求和错误,检查模板没错,后猛然想起,树状数组每次更新i处的值,所有小于maxn的i + lowbit(i)都要更新,所以,sum()函数里,while(i<=maxn) 而不是当前最大位置num。然后发现RE,用mp数组映射的时候,mp[int] = num++; 显然mp数组越界了。果断该成map。大概这就是map的好处吧...然后TLE,我拒绝相信!然后发现队列没有清空,直接开在main函数了,AC之。尝试把队列开成全局变量,然后main函数里清空,依然TLE。

大概,STL用的太多了吧..........

附代码:

/*
不想敲题,不想敲题,这道题真丑~躺在地上打滚中...
思路:优先队列维护忍耐度最低的人在队首,leave操作ok.
vis数组记录从1到n的编号的人们是不是在队列中,top维护队首的人的编号。pop操作搞定。
然后,check操作就是在vis数组中查找当前编号之前有多少个为1的数,树状数组大法好。
啊...数据虽然很大,但是t<=100000,所以可以用map把所有的编号标记为1-100000之间的...
讲真,为什么dangshixiangbuchulaine...
*/ #include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 100005
#include <map>
#include <queue>
using namespace std; char op[10];
int vis[maxn];
map<int, int>mp;
int a[maxn];
int num; struct Node {
int id, endu;
friend bool operator < (Node a, Node b) {
return a.endu > b.endu; // 从小到大
}
Node(int id, int endu) {
this->id = id, this->endu = endu;
}
}; int lowbit(int x) {
return x & (-x);
} int sum(int id) {
int ans = 0;
while(id > 0) {
ans += a[id];
id -= lowbit(id);
}
return ans;
} void update(int id, int x) {
while(id <= maxn) {
a[id] += x;
id += lowbit(id);
}
} int main() {
int n;
while(~scanf("%d", &n)) {
mp.clear();
num = 1; // 编号
memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a));
priority_queue<Node> que;
int top = 0; for (int i=0; i<n; ++i) {
scanf("%s", op);
if (op[0] == 'a') {
int id, endu;
scanf("%d%d", &id, &endu);
mp[id] = num++;
id = mp[id];
vis[id] = 1;
update(id, 1);
Node temp(id, endu);
que.push(temp);
}
else if (op[0] == 'l') {
while(!que.empty() && !vis[que.top().id])
que.pop();
if (!que.empty() && vis[que.top().id]) {
int lid = que.top().id;
que.pop();
vis[lid] = 0;
update(lid, -1);
}
}
else if (op[0] == 'p') {
while(!vis[top] && top<num) top++;
if (top < num && vis[top]) {
vis[top] = 0;
update(top, -1);
}
}
else {
int tid, tendu;
scanf("%d%d", &tid, &tendu);
tid = mp[tid];
if (vis[tid]) {
int tsum = sum(tid);
tsum -= 1;
printf("%d\n", tsum);
if (tsum > tendu) {
vis[tid] = 0;
update(tid, -1);
}
}
}
}
}
return 0;
}

不然,把struct改成pair好了...

附代码:

/*
不想敲题,不想敲题,这道题真丑~躺在地上打滚中...
思路:优先队列维护忍耐度最低的人在队首,leave操作ok.
vis数组记录从1到n的编号的人们是不是在队列中,top维护队首的人的编号。pop操作搞定。
然后,check操作就是在vis数组中查找当前编号之前有多少个为1的数,树状数组大法好。
啊...数据虽然很大,但是t<=100000,所以可以用map把所有的编号标记为1-100000之间的...
讲真,为什么dangshixiangbuchulaine...
*/ #include <stdio.h>
#include <string.h>
#include <iostream>
#define maxn 100005
#include <map>
#include <queue>
using namespace std; char op[10];
int vis[maxn];
map<int, int>mp;
int a[maxn];
int num; typedef pair<int, int>Per; int lowbit(int x) {
return x & (-x);
} int sum(int id) {
int ans = 0;
while(id > 0) {
ans += a[id];
id -= lowbit(id);
}
return ans;
} void update(int id, int x) {
while(id <= maxn) {
a[id] += x;
id += lowbit(id);
}
} int main() {
int n;
while(~scanf("%d", &n)) {
mp.clear();
num = 1; // 编号
memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a));
priority_queue<Per, vector<Per>, greater<Per> > que; // 从小到大
int top = 0;
Per per; for (int i=0; i<n; ++i) {
scanf("%s", op);
if (op[0] == 'a') {
int id, endu;
scanf("%d%d", &id, &endu);
mp[id] = num++;
id = mp[id];
vis[id] = 1;
update(id, 1);
per = make_pair(endu, id);
que.push(per);
}
else if (op[0] == 'l') {
while(!que.empty() && !vis[que.top().second])
que.pop();
if (!que.empty() && vis[que.top().second]) {
int lid = que.top().second;
que.pop();
vis[lid] = 0;
update(lid, -1);
}
}
else if (op[0] == 'p') {
while(!vis[top] && top<num) top++;
if (top < num && vis[top]) {
vis[top] = 0;
update(top, -1);
}
}
else {
int tid, tendu;
scanf("%d%d", &tid, &tendu);
tid = mp[tid];
if (vis[tid]) {
int tsum = sum(tid);
tsum -= 1;
printf("%d\n", tsum);
if (tsum > tendu) {
vis[tid] = 0;
update(tid, -1);
}
}
}
}
}
return 0;
}

  

FZU 2029 买票问题 树状数组+STL的更多相关文章

  1. AcWing 260. 买票 (树状数组+二分)打卡

    题目:https://www.acwing.com/problem/content/description/262/ 题意:给定一个队伍,每个人过来的时候可以插队,每个人会输入一个插入到哪个位置,但是 ...

  2. 树状数组+STL FZU 2029 买票问题

    题目传送门 题意:中文题面 分析:隔了一个考试周再做,开始没有什么思路,感觉能用线段树/树状数组维护,树状数组维护最小值不会去写线段树,结果超时.后来发现只要维护前缀几个人以及用优先队列/set维护最 ...

  3. POJ 2029 (二维树状数组)题解

    思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...

  4. FZU 2277 Change(dfs序+树状数组)

    Problem Description There is a rooted tree with n nodes, number from 1-n. Root’s number is 1.Each no ...

  5. FZU Problem 2029 买票问题(树状数组)

    当我看到题目是5秒的时候,压根没有想树状数组,一直奔着模拟队列去想了,最后也没搞定,赛后看到大神的题解才恍然大悟,原来如此,题目中有明显的暗示,求前n项和,骤然感叹,自己太low... 果然还是要多做 ...

  6. POJ 2029 Get Many Persimmon Trees (二维树状数组)

    Get Many Persimmon Trees Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I ...

  7. poj 2029 二维树状数组

    思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...

  8. POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)

    题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...

  9. FZU 2176 easy problem (DFS序+树状数组)

    对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开 ...

随机推荐

  1. FTP常用故障代码注解

    FTP错误列表 出处:http://bbs.enet.com.cn/UserControl?act=13&threadID 作者: |秒杀』| 详细的FTP错误列表 Restart marke ...

  2. 展讯DTS路径及编译

    DTS路径:/kernel/arch/arm/boot/dts 如何查找修改当前TP的DTS配置(分辨率)的文件:1.查找make file,找关键字都包含CONFIG_MACH,在/kernel/a ...

  3. Python学习(13)函数

    目录 Python 函数 函数调用 匿名函数 return语句 变量作用域 Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复 ...

  4. Mysql 命令行工具

    1.Mysql命令行工具分为两类:服务端命令行工具和客户端命令行工具. 2.服务端工具 mysql_install_db:建库工具 mysqld_safe:Mysql服务的启动工具,mysqld_sa ...

  5. MyEclipse8.5启动无法选择工作空间的问题

    方法一:打开Window---Preferences---General---Startup and Shutdown,勾选Prompt for workspace on startup 选项,再次登 ...

  6. RPC的学习 & gprotobuf 和 thrift的比较

    参考 http://blog.csdn.net/pi9nc/article/details/17336663 集成libevent,google protobuf的RPC框架 RPC(Remote P ...

  7. 如何定位摄像机,使物体在屏幕上始终具有相同的像素宽度和高度?(threes)

    from How to position the camera so that the object always has the same pixel width and height on the ...

  8. C#_接口

    .Net提供了接口,这个不同于Class或者Struct的类型定义.接口有些情况,看似和抽象类一样,因此有些人认为在.Net可以完全用接口来替换抽象类.其实不然,接口和抽象类各有长处和缺陷,因此往往在 ...

  9. UIButton 一个控件 实现 左图标右文本的效果

    UIButton 一个控件 实现 左图标右文本的效果 如图,我们要实现一个 左边图标右边文本的效果,一般 可以考虑是 UIImageView + UILabel 不过,其实一个UIButton就可以搞 ...

  10. semantic-ui dropdown is not a function

    按照semantic-ui官网示例,编写了如下示例,却不见效果. <div class="ui secondary menu"> <a class="i ...