usaco5.3.2 Window Area

一 原题

Window Area
IV Balkan Olympiad

You've just be assigned the project of implemented a windowing interface. This windowing interface is fairly simple, and fortunately, you don't have to display the actual windows. There are 5 basic operations:

  1. Create a window
  2. Bring a window to the top
  3. Put a window to the bottom
  4. Destroy a window
  5. Output what percentage of a window is visible (i.e., isn't covered by windows above it).

In the input, the operations appear in the following format:

  • Create window: w(I,x,y,X,Y)
  • Bring window to top: t(I)
  • Put window on bottom: b(I)
  • Destroy window: d(I)
  • Output percentage visible: s(I)
The I is a unique identifier for each window, which is one character. The character can be any of 'a'..'z', 'A'..'Z', and '0'..'9'. No extra spaces will appear in the input.

(x,y) and (X,Y) are opposite corners of the window. When a window is created, it is put `on top'. You can't create a window with an identifier that is already in use, but you can destroy a window and then create a new one with the identifier of the destroyed window. Coordinates will be positive integers, and all windows will be of non-zero area (x != X and y != Y). The x and y coordinates are between 1 and 32767 inclusive.

PROGRAM NAME: window

INPUT FORMAT

The input file consists of a sequence of commands to your interpreter. They will be listed one per line. Terminate the program when no more input is available

SAMPLE INPUT (file window.in)

w(a,10,132,20,12)
w(b,8,76,124,15)
s(a)

OUTPUT FORMAT

Output lines only for the s() commands. Of course, there might be several s() commands (but no more than 500) so the output should be a sequence of percentages, one per line, stating the percentage of the windows that are visible. The percentages should be rounded to 3 decimal places.

SAMPLE OUTPUT (file window.out)

49.167


二 分析

维护一个windows的序列,在每次创建,置顶,置底,删除操作后都更新序列,这里我用的是naive的更新方法,每次更新时间复杂度是O(n)的,n是序列长度。
遇到需要输出的时候,使用漂浮法算出可见面积就行啦。


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: window
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4188 KB]
   Test 2: TEST OK [0.000 secs, 4188 KB]
   Test 3: TEST OK [0.000 secs, 4188 KB]
   Test 4: TEST OK [0.000 secs, 4188 KB]
   Test 5: TEST OK [0.000 secs, 4188 KB]
   Test 6: TEST OK [0.000 secs, 4188 KB]
   Test 7: TEST OK [0.000 secs, 4188 KB]
   Test 8: TEST OK [0.000 secs, 4188 KB]
   Test 9: TEST OK [0.000 secs, 4188 KB]
   Test 10: TEST OK [0.000 secs, 4188 KB]
   Test 11: TEST OK [0.000 secs, 4188 KB]

All tests OK.

Your program ('window') produced all correct answers! This is your submission #3 for this problem. Congratulations!


AC代码:
/*
ID:maxkibb3
LANG:C++
PROB:window
*/

#include<cstdio>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;

struct Window {
	char id;
	int x1, y1, x2, y2;

	double area() {
		return (double)abs((x2 - x1) * (y2 - y1));
	}
};

Window mk_window(char _id, int _x1,
		int _y1, int _x2, int _y2) {
	Window ret;
	ret.id = _id;
	ret.x1 = _x1;
	ret.y1 = _y1;
	ret.x2 = _x2;
	ret.y2 = _y2;
	return ret;
}

vector<Window> windows;

int find_window(char _id) {
	for(int i = 0; i < windows.size(); i++) {
		if(_id == windows[i].id) return i;
	}
	printf("Shall not see this!\n");
	return -1;
}

void create(char *_s) {
	int pos[4];
	int cnt = 0;
	int len = strlen(_s);
	for(int i = 2; i < len; i++) {
		if(_s[i] == ',') {
			pos[cnt++] = i;
			_s[i] = '\0';
		}
		if(_s[i] == ')')
			_s[i] = '\0';
	}
	char id = _s[2];
	int x1 = atoi(_s + pos[0] + 1);
	int y1 = atoi(_s + pos[1] + 1);
	int x2 = atoi(_s + pos[2] + 1);
	int y2 = atoi(_s + pos[3] + 1);
	if(x1 > x2) swap(x1, x2);
	if(y1 < y2) swap(y1, y2);
	windows.push_back(mk_window(id, x1, y1, x2, y2));
}

void top(char *_s) {
	char id = _s[2];
	int idx = find_window(id);
    Window w = windows[idx];
    windows.erase(windows.begin() + idx);
    windows.push_back(w);
}

void bottom(char *_s) {
	char id = _s[2];
	int idx = find_window(id);
    Window w = windows[idx];
    windows.erase(windows.begin() + idx);
    windows.insert(windows.begin(), w);
}

void destroy(char *_s) {
	char id = _s[2];
	int idx = find_window(id);
	windows.erase(windows.begin() + idx);
}

double up(Window _w, int _idx) {
	if(_w.x1 > _w.x2 || _w.y1 < _w.y2) return 0;
	if(_idx == windows.size()) return _w.area();
	double ret = 0;
	Window w = windows[_idx];
	ret += up(mk_window('_', _w.x1, _w.y1,
		min(_w.x2, w.x1), _w.y2), _idx + 1);
	ret += up(mk_window('_', max(_w.x1, w.x2),
		_w.y1, _w.x2, _w.y2), _idx + 1);
	ret += up(mk_window('_', max(_w.x1, w.x1), _w.y1,
		min(w.x2, _w.x2), max(_w.y2, w.y1)), _idx + 1);
	ret += up(mk_window('_', max(_w.x1, w.x1),
		min(_w.y1, w.y2), min(_w.x2, w.x2), _w.y2), _idx + 1);
	return ret;
}

void calc(char *_s) {
	char id = _s[2];
	int idx = find_window(id);
	double seen = up(windows[idx], idx + 1);
	printf("%.3f\n", seen / windows[idx].area() * 100);
}

void solve() {
	char s[50];
	while(scanf("%s", s) != -1) {
		if(s[0] == 'w')
			create(s);
		else if(s[0] == 't')
			top(s);
		else if(s[0] == 'b')
			bottom(s);
		else if(s[0] == 'd')
			destroy(s);
		else
			calc(s);
	}
}

int main() {
	freopen("window.in", "r", stdin);
	freopen("window.out", "w", stdout);
	solve();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值