Codeforces 46D Parking Lot(贪心模拟)

Codeforces 46D Parking Lot

题目链接

开线段树专题开出了这题。。看似要用区间合并求连续最大区间,其实不需要。因为询问才100个,直接set暴力去模拟即可,每次车进来就从左往右找到一个合适位置

代码:

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

const int N = 100005;
int L, b, f, n;
set<int> g;
set<int>::iterator u, v;
int save[N][2];

int main() {
	scanf("%d%d%d", &L, &b, &f);
	scanf("%d", &n);
	g.insert(-b); g.insert(L + f);
	int op, x;
	for (int i = 1; i <= n; i++) {
		scanf("%d%d", &op, &x);
		if (op == 1) {
			int flag = 1;
			for (u = g.begin(); u != g.end(); u++) {
				v = u; v++;
				if (v != g.end() && *u + b + x + f <= *v) {
					g.insert(*u + b); g.insert(*u + b + x);
					save[i][0] = *u + b; save[i][1] = *u + b + x;
					printf("%d\n", *u + b);
					flag = 0;
					break;
				}
				u++;
			}
			if (flag) printf("-1\n");
		} else {
			g.erase(save[x][0]);
			g.erase(save[x][1]);
		}
	}
	return 0;
}
时间: 2024-08-20 13:49:01

Codeforces 46D Parking Lot(贪心模拟)的相关文章

Codeforces 46D Parking Lot(线段树)

题目链接:Codeforces 46D Parking Lot 题目大意:一个街道,长为N,每辆车停进来的时候必须和前面间隔B米,和后面间隔F米,现在用两种操作,1是停进来一个长为x的车,2是第x辆车开走. 解题思路:区间合并,建一颗长度为N + B + F的线段树,然后每次停车进去的时候都查询x + B + F的区间,然后修改的时候只修改x的长度. #include <cstdio> #include <cstring> #include <algorithm> us

[贪心+模拟] zoj 3829 Known Notation

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science.

Codeforces Round #300——C贪心——Tourist&#39;s Notes

Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level. On the i-th day height was equal to some integer hi. The tourist pick smooth enough route for his hike, me

POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索类题目][0033贪心模拟]

/** POJ 1979 BFS */ #include <stdio.h> #include <string.h> #include <iostream> #include <queue> using namespace std; const int N = 20 + 5; int mp[N][N]; int sx,sy; int n, m; int vis[3000]; int dirx[] = {0, 1, 0, -1}; int diry[] = {

Codeforces 475C Kamal-ol-molk&#39;s Painting 模拟

题目链接:点击打开链接 题意:给定n*m的矩阵 X代表有色 .代表无色 用一个x*y的矩阵形刷子去涂色. 刷子每次可以→或↓移动任意步. 若能够染出给定的矩阵,则输出最小的刷子的面积 若不能输出-1 思路: 先找到连续最小的x,y 因为至少一个边界和x或y相等,所以枚举(x,i) 和 (i,y)就可以了. #pragma comment(linker, "/STACK:102400000,102400000") #include <stdio.h> #include <

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

hdu 4915 Parenthese sequence (贪心+模拟)

题目大意: 一个序列中有左括号和右括号,还有问号,问号可以任意转换成左右括号. 问这个序列有多少种情况的转变使得这个序列变成合法的括号匹配序列. 思路分析: 首先我们分析一下,如何使得一个序列是合法的括号匹配序列. 我们很容易想到的是用栈模拟匹配过程. 当遇到左括号就进栈,当遇到右括号就让栈顶的左括号出栈. 那么在模拟的过程中,造成这个序列的不合法的原因只有当右括号来的时候,此时的栈已经为空. 这里补充一句,一旦一个序列给定,那么这里面的问号有多少变成左括号,多少变成右括号,是一定的. 看完以上

Codeforces 30D King&#39;s Problem? 模拟

首先将n个点排序,找出排序后的K,然后分情况讨论. 当 k == n+1时,显然是 k->1->n || k->n->1这两种的较小值,因为三角形的两边之和大于第三边. 当1 <= k && k <= n 时: 1 , k -> 1 -> n+1 -> k+1 ->n  ||  k -> n -> n+1 -> k-1 -> 1,当k+1 || k-1 不存在时将对应步骤忽略. 2 , k - > 1

1002 Game (贪心模拟)

1002 Game (贪心模拟)Accepts: 572 Submissions: 6218 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description 度度熊在玩一个好玩的游戏. 游戏的主人公站在一根数轴上,他可以在数轴上任意移动,对于每次移动,他可以选择往左或往右走一格或两格. 现在他要依次完成 n 个任务,对于任务 i,只要他处于区间 [ai,bi]