poj 1693 模拟
2016-07-18 15:49
288 查看
Counting Rectangles
Description
We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example, the number of rectangles in the Figures 1 and 2 are 5 and 0 respectively.
There are many intersection points in the figure. An intersection point is a point shared by at least two segments. The input line segments are such that each intersection point comes from the intersection of exactly one horizontal segment and one vertical
segment.
Input
The first line of the input contains a single number M, which is the number of test cases in the file (1 <= M <= 10), and the rest of the file consists of the data of the test cases. Each test case begins with a line containing s (1 <= s <= 100), the number
of line segments in the figure. It follows by s lines, each containing x and y coordinates of two end points of a segment respectively. The coordinates are integers in the range of 0 to 1000.
Output
The output for each test case is the number of all different rectangles in the figure described by the test case. The output for each test case must be written on a separate line.
Sample Input
Sample Output
题意:就是给出一些平行于坐标的线,求围成的矩形
题解:就是随机抽取纵线,然后用横线去截取,可以自己用草稿纸推出公式temp*(temp-1)/ 2
注意上叙随机二字就好了
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1072 | Accepted: 557 |
We are given a figure consisting of only horizontal and vertical line segments. Our goal is to count the number of all different rectangles formed by these segments. As an example, the number of rectangles in the Figures 1 and 2 are 5 and 0 respectively.
There are many intersection points in the figure. An intersection point is a point shared by at least two segments. The input line segments are such that each intersection point comes from the intersection of exactly one horizontal segment and one vertical
segment.
Input
The first line of the input contains a single number M, which is the number of test cases in the file (1 <= M <= 10), and the rest of the file consists of the data of the test cases. Each test case begins with a line containing s (1 <= s <= 100), the number
of line segments in the figure. It follows by s lines, each containing x and y coordinates of two end points of a segment respectively. The coordinates are integers in the range of 0 to 1000.
Output
The output for each test case is the number of all different rectangles in the figure described by the test case. The output for each test case must be written on a separate line.
Sample Input
2 6 0 0 0 20 0 10 25 10 20 10 20 20 0 0 10 0 10 0 10 20 0 20 20 20 3 5 0 5 20 15 5 15 25 0 10 25 10
Sample Output
5 0
题意:就是给出一些平行于坐标的线,求围成的矩形
题解:就是随机抽取纵线,然后用横线去截取,可以自己用草稿纸推出公式temp*(temp-1)/ 2
注意上叙随机二字就好了
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct X { int x; int y1,y2; }vert[105]; int num1; struct Y { int y; int x1,x2; }horiz[105]; int num2; int main() { int m,n; int t1_x,t2_x,t1_y,t2_y; scanf("%d",&m); while(m--) { scanf("%d",&n); num1=num2=0; while(n--){ scanf("%d%d%d%d",&t1_x,&t1_y,&t2_x,&t2_y); if(t1_x==t2_x){ vert[num1].x=t1_x; vert[num1].y1=min(t1_y,t2_y); vert[num1++].y2=max(t1_y,t2_y); } else{ horiz[num2].y=t1_y; horiz[num2].x1=min(t1_x,t2_x); horiz[num2++].x2=max(t1_x,t2_x); } } int ans=0,temp; for(int i=0;i<num1-1;i++){ for(int j=i+1;j<num1;j++){ temp=0; if((vert[i].y1>vert[j].y2)||(vert[i].y2<vert[j].y1)) continue; int max_y=min(vert[i].y2,vert[j].y2); int min_y=max(vert[i].y1,vert[j].y1); int min_x=min(vert[i].x,vert[j].x); int max_x=max(vert[i].x,vert[j].x); for(int k=0;k<num2;k++){ if(horiz[k].y<=max_y&&horiz[k].y>=min_y) if(horiz[k].x1<=min_x&&horiz[k].x2>=max_x) temp++; } ans+=temp*(temp-1)/2; } } printf("%d\n",ans); } return 0; }
相关文章推荐
- 【杭电-oj】-1002-A + B Problem II(大数相加)
- Ubuntu 12 安装 MySQL 5.6.26 及 问题汇总
- Failed to load the JNI library "E:\JDK6.0\bin\client\jvm.dll"
- 移动端1px解决方案
- liunx命令之:命令链接ftp服务器
- 挪威石油基金将起诉大众汽车
- 【杭电oj】1997 - 汉诺塔VII(递归,思维)
- CentOS 下PHP5.3升级到PHP5.4步骤
- Invert Binary Tree
- 带权并查集 poj 1182
- Ubuntu 12 修改当前用户密码:new password is too simple
- Ubuntu 12 编译安装 PHP 5.4 及 问题汇总
- 关于Spring中的<context:annotation-config/>配置(开启注解)
- 水过杭电OJ hdu1004
- O(n) 方法求数列极差
- 【Xml配置文件的数据读取】
- 二维码的生成 lizbqrencode遇到的问题
- 如何设置PL/SQL语句的快捷键
- 中国豪车热度不减
- SQL注入全过程深入分析