Flow Layout-2014

Flow Layout
Time Limit: 1000MS
Memory Limit: 30000K
Total Submissions: 3098
Accepted: 2153

Description

A flow layout manager takes rectangular objects and places them in a rectangular window from left to right. If there isn't enough room in one row for an object, it is placed completely below all the objects in the first row at the left edge, where the order continues from left to right again. Given a set of rectangular dimensions and a maximum window width, you are to write a program that computes the dimensions of the final window after all the rectangles have been placed in it. 

For example, given a window that can be at most 35 units wide, and three rectangles with dimensions 10 x 5, 20 x 12, and 8 x 13, the flow layout manager would create a window that looked like the figures below after each rectangle was added. 

The final dimensions of the resulting window are 30 x 25, since the width of the first row is 10+20 = 30 and the combined height of the first and second rows is 12+13 = 25.

Input

The input consists of one or more sets of data, followed by a final line containing only the value 0. Each data set starts with a line containing an integer, m, 1 <= m <= 80, which is the maximum width of the resulting window. This is followed by at least one and at most 15 lines, each containing the dimensions of one rectangle, width first, then height. The end of the list of rectangles is signaled by the pair -1 -1, which is not counted as the dimensions of an actual rectangle. Each rectangle is between 1 and 80 units wide (inclusive) and between 1 and 100 units high (inclusive).

Output

For each input set print the width of the resulting window, followed by a space, then the lowercase letter "x", followed by a space, then the height of the resulting window.

Sample Input

35
10 5
20 12
8 13
-1 -1
25
10 5
20 13
3 12
-1 -1
15
5 17
5 17
5 17
7 9
7 20
2 10
-1 -1
0

Sample Output

30 x 25
23 x 18
15 x 47

Source

/***********************************************************************
    Copyright (c) 2015,wangzp
    All rights no reserved.
  
    Name: 《Flow Layout》In PEKING UNIVERSITY ACM
    ID:   PROBLEM 2014
    问题简述: 在一个规定的矩形框内放置各种矩形,放置完毕后计算总体的高度和长度。
    算法思想:按照矩形的长度,逐行放置;
              从左到右,如果超过矩形框长度;
              则往下一行从左到右放置,放置过程中设置变量保存好总共的长度和高度就可以了。
    Date: Aug 21, 2015 
 
***********************************************************************/
#include 
   
   
    
    

int main(void)
{
	int total_len,total_hig;
	int sum_len,sum_hig;
	int limit_width;
	int x,y;
	while(scanf("%d",&limit_width),limit_width != 0)
	{
		total_len = 0;
		total_hig = 0;
		sum_len = 0;
		sum_hig = 0;
		while(scanf("%d %d",&x,&y),(x != -1) || (y != -1))
		{
			if (sum_len + x <= limit_width)
			{
				sum_len += x;
			} 
			else
			{
				sum_len = 0;
				sum_len += x;
				total_hig += sum_hig;
				sum_hig = 0;
			}
			if (sum_len > total_len)
			{
				total_len = sum_len;
			}
			if (y > sum_hig)
			{
				sum_hig = y;
			}
		}
		
		total_hig += sum_hig;

		printf("%d x %d\n",total_len,total_hig);
	}
	
	return 0;
}

   
   

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值