FZU 2029 买票问题 树状数组+STL
题目链接:买票问题
思路:优先队列维护忍耐度最低的人在队首,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的更多相关文章
- AcWing 260. 买票 (树状数组+二分)打卡
题目:https://www.acwing.com/problem/content/description/262/ 题意:给定一个队伍,每个人过来的时候可以插队,每个人会输入一个插入到哪个位置,但是 ...
- 树状数组+STL FZU 2029 买票问题
题目传送门 题意:中文题面 分析:隔了一个考试周再做,开始没有什么思路,感觉能用线段树/树状数组维护,树状数组维护最小值不会去写线段树,结果超时.后来发现只要维护前缀几个人以及用优先队列/set维护最 ...
- POJ 2029 (二维树状数组)题解
思路: 大力出奇迹,先用二维树状数组存,然后暴力枚举 算某个矩形区域的值的示意图如下,代码在下面慢慢找... 代码: #include<cstdio> #include<map> ...
- 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 ...
- FZU Problem 2029 买票问题(树状数组)
当我看到题目是5秒的时候,压根没有想树状数组,一直奔着模拟队列去想了,最后也没搞定,赛后看到大神的题解才恍然大悟,原来如此,题目中有明显的暗示,求前n项和,骤然感叹,自己太low... 果然还是要多做 ...
- POJ 2029 Get Many Persimmon Trees (二维树状数组)
Get Many Persimmon Trees Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I ...
- poj 2029 二维树状数组
思路:简单树状数组 #include<map> #include<set> #include<cmath> #include<queue> #inclu ...
- POJ 2029 Get Many Persimmon Trees(DP||二维树状数组)
题目链接 题意 : 给你每个柿子树的位置,给你已知长宽的矩形,让这个矩形包含最多的柿子树.输出数目 思路 :数据不是很大,暴力一下就行,也可以用二维树状数组来做. #include <stdio ...
- FZU 2176 easy problem (DFS序+树状数组)
对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开 ...
随机推荐
- iOS - UIScrollView
前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding> @available(iOS 2.0, ...
- linux学习笔记2-命令总结3
文件搜索命令 1.文件搜索命令 find 2.其他文件搜索命令 grep - 在文件中搜索字串匹配的行并输出 locate - 在文件资料库中查找文件 whereis - 搜索命令所在目录及帮助文档路 ...
- Javascript设计模式之创建对象的灵活性
传统的 /* Anim class */ var Anim = function () {}; Anim.prototype.start = function () { console.log(&qu ...
- golang csv,xls,xlsx
要用到的包: "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform ...
- Oracle数据库和DB2数据库分页SQL的区别举例
--------------------------ORACLE数据库分页SQL举例------------------------------------------------ SELECT * ...
- 20160805_CentOS6_键盘快捷键
1. 系统 -->首选项 --> 键盘快捷键 2. 3.
- 自定义view获取宽高
View在构造函数初始化并未布局处理,此时宽高均为0,待所有控件初始化完毕后,由上级容器对内部各控件进行布局,此时控件才会具有位置与大小属性,可以通过以下方法获取:1.在ondraw()函数中获取,2 ...
- --tags --follow-tags 的区别
--tags All refs under refs/tags are pushed, in addition to refspecs explicitly listed on the comm ...
- The Daligner Overlap Library
/************************************************************************************\ * * * Copyrig ...
- ajax再接触
贴三个目前写的已经工作了的ajax,并且给出备注: 1.这是第一个ajax以及它的接口文档,需要注意的是1.因为返回数据是xml所以多加了contentType:"application/x ...