ZOJ (狗狗)1426 Counting Rectangles(暴力)

Counting Rectangles


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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 file 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

The above input file contains two test cases corresponding to Figures 1 and 2 respectively.

题目大意:

  就是说给你n条直线,n<=100,且这n条直线两两相互平行,都是垂直于x轴线或者垂直于y轴的。那么这n条直线中能组成的矩形有多少个?

解题思路:

  直接上来就暴力吧,先开始我们只要将横着的线和竖着的线分别统计进入两个不同的类,然后从这两个类中开始依次判断。乍一看,以为是规律题,画了5组,发现,我们只要先枚举任意两条横向平行的直线,然后在寻找与这两条有交点的纵向的直线,每次增加一条竖着的直线,那么就会给tot贡献(cnt-1)*cnt/2,,多画几个就能推理出来了,然后,就这样每次两条枚举完所有的横向直线。

  第一次用类写,有点手抖,但是还是1Y了。

代码:

  1 # include<cstdio>
  2 # include<iostream>
  3 # include<fstream>
  4 # include<algorithm>
  5 # include<functional>
  6 # include<cstring>
  7 # include<string>
  8 # include<cstdlib>
  9 # include<iomanip>
 10 # include<numeric>
 11 # include<cctype>
 12 # include<cmath>
 13 # include<ctime>
 14 # include<queue>
 15 # include<stack>
 16 # include<list>
 17 # include<set>
 18 # include<map>
 19
 20 using namespace std;
 21
 22 const double PI=4.0*atan(1.0);
 23
 24 typedef long long LL;
 25 typedef unsigned long long ULL;
 26
 27 # define inf 999999999
 28 # define MAX 1000+4
 29
 30 int num;//直线的条数
 31
 32 class rectangle
 33 {
 34 public:
 35     int x1,y1,x2,y2;
 36     void set ( int a,int b,int c,int d )
 37     {
 38         x1 = a;
 39         y1 = b;
 40         x2 = c;
 41         y2 = d;
 42     }
 43 };
 44
 45 rectangle aa[MAX];
 46 rectangle bb[MAX];
 47
 48 int check ( rectangle & a, rectangle & b )
 49 {
 50     return b.y1<=a.y1 && a.y1<=b.y2 && a.x1<=b.x1 && b.x1<=a.x2;
 51 }
 52
 53 int main(void)
 54 {
 55     int t;cin>>t;
 56     while ( t-- )
 57     {
 58         cin>>num;
 59         int  H = 0, S = 0;
 60         for ( int i = 0;i < num;i++ )
 61         {
 62             int x,y,x1,y1;
 63             cin>>x>>y>>x1>>y1;
 64             if ( x==x1 )
 65             {//竖线集
 66                 if ( y > y1 )
 67                 {
 68                     aa[S++].set(x1,y1,x,y);
 69                 }
 70                 else
 71                 {
 72                     aa[S++].set(x,y,x1,y1);
 73                 }
 74             }
 75             else
 76             {//横线集
 77                 if ( x > x1 )
 78                 {
 79                     bb[H++].set(x1,y1,x,y);
 80                 }
 81                 else
 82                 {
 83                     bb[H++].set(x,y,x1,y1);
 84                 }
 85             }
 86
 87         }
 88
 89         int tot = 0;
 90         for ( int i = 0;i < H-1;i++ )
 91         {
 92             for ( int j = i+1;j < H;j++ )
 93             {
 94                 int cnt = 0;
 95                 for ( int k = 0;k < S;k++ )
 96                 {
 97                     if ( check( bb[i],aa[k]) && check( bb[j],aa[k]) )
 98                     {
 99                         cnt++;
100                     }
101                 }
102                 tot+=(cnt-1)*cnt/2;
103             }
104         }
105
106
107         cout<<tot<<endl;
108     }
109
110
111
112
113     return 0;
114 }
时间: 2024-10-16 01:51:04

ZOJ (狗狗)1426 Counting Rectangles(暴力)的相关文章

Project Euler 85 :Counting rectangles 数长方形

Counting rectangles By counting carefully it can be seen that a rectangular grid measuring 3 by 2 contains eighteen rectangles: Although there exists no rectangular grid that contains exactly two million rectangles, find the area of the grid with the

UVA - 10574 Counting Rectangles

Description Problem H Counting Rectangles Input: Standard Input Output:Standard Output Time Limit: 3Seconds   Given n points on the XY plane, count how many regular rectanglesare formed. A rectangle is regular if and only if its sides are all paralle

UVA 10574 - Counting Rectangles(枚举+计数)

10574 - Counting Rectangles 题目链接 题意:给定一些点,求能够成几个矩形 思路:先把点按x排序,再按y排序,然后用O(n^2)的方法找出每条垂直x轴的边,保存这些边两点的y坐标y1, y2.之后把这些边按y1排序,再按y2排序,用O(n)的方法找出有几个连续的y1, y2都相等,那么这些边两两是能构成矩形的,为C2cnt种,然后累加起来就是答案 代码: #include <stdio.h> #include <string.h> #include <

Codeforces 372B. Counting Rectangles is Fun【动态规划,暴力枚举】(lowbit()小用法)

题目大意: 给出一个由0,1构成的矩阵,询问(a,b)到(c,d)两个点之间的只含有0的矩形有多少个. 方法: 由于矩阵不大,最多40*40,而且询问量很大(10^5)由此我们考虑o(1)输出答案,首先用一个四维数组预处理出答案,最后直接输出即可. 令dp[a][b][c][d]为(a,b)到(c,d)两个点之间的只含有0的矩形的数量, 则递推的公式: dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1] 每次计算

zoj 3818 Pretty Poem(暴力处理字符串)2014年牡丹江赛区网络赛

Pretty Poem Time Limit: 2 Seconds      Memory Limit: 65536 KB Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can e

UVA 10574 - Counting Rectangles 计数

Given n points on the XY plane, count how many regular rectangles are formed. A rectangle is regular if and only if its sides are all parallel to the axis.InputThe ?rst line contains the number of tests t (1 ≤ t ≤ 10). Each case contains a single lin

zoj 2976 Light Bulbs(暴力枚举)

Light Bulbs Time Limit: 2 Seconds      Memory Limit: 65536 KB Wildleopard had fallen in love with his girlfriend for 20 years. He wanted to end the long match for their love and get married this year. He bought a new house for his family and hired a

zoj 3817 Chinese Knot(hash+暴力)

题目链接:zoj 3817 Chinese Knot 题目大意:给出四个字符串,对应着同心结的四条边,现在给定一个目标串,可以从任意节点开始移动,问是否可以匹配目标串. 解题思路:用hash将四个字符串的正序和逆序处理出来,然后dfs枚举,每次保留起始位置和移动方向即可. #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace st

CF 372B Counting Rectangles is Fun [dp+数据维护]

题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 即为求(a,b)到(c,d)有多少个由0组成的矩形 对一个矩形来说 dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1]+包含右下角(当前点)的矩形; 重点就在包含右下角(当前点c,d)的矩形,如何计算这个 我们可以暴力扫描,需要nm的复杂度,乘上原有复杂度,,,已经会超过时限