C++——【USACO 5.3.2】——Window Area

44 篇文章 0 订阅

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

窗口


您刚刚被分配了实现一个窗口接口的项目。这个窗口接口相当简单,幸运的是,您不必显示实际的窗口。有5个基本操作:


创建一个窗口

把窗户放到顶层

把窗户放到底层

关闭一个窗口

输出窗口的可见百分比。


在输入中,操作出现如下格式:

创建窗口: w ( I , x , y , x , y )

把窗口置顶: t ( I )

把窗口置底: b ( I )

关闭窗口 : d ( I )

输出可见部分的百分比 : s ( I )

I是每个窗口的唯一标识符,它是一个字符。这个字可以是a ~ z 、 A ~ Z 、0 ~ 9的任何一个。输入中不会出现额外的空格。

(x,y)和(X,Y)是窗口的对角。当一个窗口被创建时,它被放在顶层。您不能创建一个已经在使用的标识符的窗口,但是您可以关闭一个窗口,然后创建一个带有被关闭窗口的标识符的新窗口。坐标将是正整数,所有的窗口大小非零(x != X和y != Y),坐标在1和32767之间。

项目名称: window

输入格式

输入文件由一系列命令组成,由您的解释器执行。他们将被列在每一行。当没有更多输入时终止程序

示例输入(文件window.in)

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

输出格式

只用于s()命令的输出行。当然,可能会有几个s()命令(但不超过500),因此输出应该是一个百分比序列,每一行,说明可见的窗口百分比。百分比应该四舍五入到小数点后三位。

样例输出(文件window.out)

49.167



——一定要注意,窗口的范围为 [ x , X) , [ y , Y ),最开始被这里阴到了。。。。。

参考了一下某位da lao的code


/*
ID : mcdonne1
LANG : C++
TASK : window
*/

#pragma GCC optimize("O3")

#include <iostream>
#include <fstream>
#include <iomanip>

#define top() \
for (int i = 1; i < sum; i++) if (win[i].name == s[2]) swap (win[i], win[i + 1])

#define bottom() \
for (int i = sum; i > 1; i--) if (win[i].name == s[2]) swap (win[i], win[i - 1])

using namespace std;

struct node {
	char name;
	int minx, maxx, miny, maxy;
}win[65];

int sum;
double area, real;

void count (int x1, int x2, int y1, int y2, int begin) {
	for (int i = begin; i <= sum; i++) {
		if (win[i].maxy <= y1 or win[i].miny >= y2 or win[i].maxx <= x1 or win[i].minx >= x2) continue;
		if (win[i].minx > x1) {count (x1, win[i].minx, y1, y2, i + 1); x1 = win[i].minx;}
		if (win[i].maxx < x2) {count (win[i].maxx, x2, y1, y2, i + 1); x2 = win[i].maxx;}
		if (win[i].miny > y1) {count (x1, x2, y1, win[i].miny, i + 1); y1 = win[i].miny;}
		if (win[i].maxy < y2) {count (x1, x2, win[i].maxy, y2, i + 1); y2 = win[i].maxy;}
		return;
	}
	area += (x2 - x1) * (y2 - y1);
}

inline void question (char name) {
	for (int i = 1; i <= sum; i++)
		if (win[i].name == name) {
			area = 0;
			real = (win[i].maxx - win[i].minx) * (win[i].maxy - win[i].miny);
			count (win[i].minx, win[i].maxx, win[i].miny, win[i].maxy, i + 1);
			cout<<fixed<<setprecision(3)<<(area / real) * 100<<endl;
			return;
		}
}

int main () {
	freopen ("window.in", "r", stdin);
	freopen ("window.out", "w", stdout);
	cout.setf(ios::fixed);
	string s;
	while (cin>>s) {
		if (s[0] == 'w') {
			int a[4] = {0, 0, 0, 0};
			for (int i = 4, j = 0; j <= 3; j++) {
				while (!isdigit(s[i])) i++;
				while (isdigit(s[i])) a[j] = a[j] * 10 + s[i++] - 48;
			}
			if (a[0] > a[2]) swap (a[0], a[2]);
			if (a[1] > a[3]) swap (a[1], a[3]);
			win[++sum] = (node){s[2], a[0], a[2], a[1], a[3]};
		}
		else if (s[0] == 't') {top();}
		else if (s[0] == 'b') {bottom();}
		else if (s[0] == 'd') {top(); --sum;}
		else question (s[2]);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值