POJ2014 Flow Layout

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 3161   Accepted: 2199

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

Mid-Central USA 2004

调节心情用的水题。

纯模拟。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #define LL long long
 7 using namespace std;
 8 const int mxn=500010;
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     while(ch<‘0‘ || ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
12     while(ch>=‘0‘ && ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
13     return x*f;
14 }
15 int limit=0;
16 int now=0;
17 int w=0;
18 int h=0;
19 int last=0;
20 int main(){
21     int x,y;
22     while(1){
23         limit=read();
24         if(!limit)break;
25         w=h=last=now=0;
26         while(1){
27             x=read();y=read();
28             if(x==-1 && y==-1){
29                 printf("%d x %d\n",w,h);
30                 break;
31             }
32             if(now+x<=limit){
33                 now+=x;
34                 w=max(w,now);
35                 h=max(h,last+y);
36             }
37             else{
38                now=x;
39                w=max(now,w);
40                last=h;
41                h=last+y;
42             }
43 //            printf("now: %d %d\n",w,h);
44         }
45     }
46     return 0;
47 }
时间: 2024-08-08 09:25:01

POJ2014 Flow Layout的相关文章

Flow Layout

--------------siwuxie095 将根面板 contentPane 的布局切换为 Flow Layout Flow Layout 即 浮动布局,即 所有的控件都是可以浮动的 不是指 相互叠加,而是指 它们在窗体大小发送变化时之间的相对位置可以改变 如:向 contentPane 中添加 4 个 JButton,当缩放窗体时 对于一个控件,它的大小是由 布局管理器(Layout Manager) 来控制的 但是,有时候某些控件被 布局管理器 设定的大小并不合适, 或 不是所需要的

Collection View 自定义布局(custom flow layout)

Collection view自定义布局 一般我们自定义布局都会新建一个类,继承自UICollectionViewFlowLayout,然后重写几个方法: prepareLayout():当准备开始布局时调用这个方法,可以在这里计算一些属性,比如cell的尺寸. layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]?:在这里返回布局属性. 实例(比较简单的例子,实际开发中可以进行

Border Layout

------------------siwuxie095 根面板 contentPane 的默认布局就是 Border Layout Border Layout 即 边界布局 边界布局 共有 5 个区域: North.West.Center.East.South 「注意:分界线处(即 边界)是 <Unknown position>」 如:向 contentPane 中添加 5 个 JButton 这 5 个区域不仅仅可以用来放置元素,还可以用来放置 其他的容器,再往里面放置元素 如: 在 No

UICollectionView介绍使用

UICollectionView是一种类似于UITableView但又比UITableView功能更强大.更灵活的视图,这是源于它将UICollectionView对cell的布局交给了UICollectionViewLayout,而且允许用户自定义layout来进行布局. 下面是UICollectionView合并内容和布局并生成最终界面的一个流程: 当UICollectionView显示内容时,先从Data source(数据源)获取cell,然后交给UICollectionView.再从U

java学习资料

必须养成好的的编码习惯:缩进(用空格).注释.命名约定. 大小写敏感. 单独的":"代表一条空语句. main函数是我们整个程序的执行入口所以必须是静态公开的. 必须写成这样:  public static void main(String[]args){...} 生成jar包: 在eclipse里,选中要打包的几个文件,右键-Export-写文件名-Next-Next-选main方法的class-finish 在jar包的同一文件夹下,新建一个空文档,写"java -jar

十三、java_GUI

目录: 一.AWT 二.组件和容器 三.布局管理器 四.事件处理 五.java图形 六.window事件 一.AWT AWT(Abstract Window Toolkit)包括了很多类和接口,用于Java Application的GUI(Graphics User Interface 提醒用户界面)编程 GUI的各种元素(如:窗口,按钮,文本框等)由java类来实现 使用AWT锁涉及的类一般在java.awt包及子包中 Container和Component是AWT中的两个核心类 二.组件和容

JAVA_布局管理器

1 /* 范例名称:FlowLayout 用法举例 2 * 源文件名称:TestFlowLayout.java 3 * 要 点: 4 * 1. 布局管理器的概念和作用 5 * 2. FlowLayout的性质及用法 6 */ 7 8 import java.awt.*; 9 10 public class TestFlowLayout { 11 public static void main(String args[]) { 12 Frame f = new Frame("Flow Layout

北大ACM题库习题分类与简介(转载)

在百度文库上找到的,不知是哪位大牛整理的,真的很不错! zz题 目分类 Posted by fishhead at 2007-01-13 12:44:58.0 -------------------------------------------------------------------------------- acm.pku.edu.cn 1. 排序 1423, 1694, 1723, 1727, 1763, 1788, 1828, 1838, 1840, 2201, 2376, 23

Objective-c——UI基础开发第十二天(相册展示)

一.知识点 模仿新特性 UICollectionViewFlowLayout自定义布局 相册 瀑布流(淘宝购物之类的 二.复习 a.UICollectionView 和 tableview共享一套API 不同: 1.实例化collectionView必须传入一个非空的layout布局对象 layout:主要针对 cell的各个属性操控(UICollectionViewLayout 啥都没有只是定义了必须实现的方法/UICollectionViewFlowLayout 是上面的子类) 2.必须要注