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 <algorithm>
using namespace std;

const int N = 5005;
int t, n;
struct Point {
    int x, y;
    void read() {
    scanf("%d%d", &x, &y);
    }
    bool operator < (const Point a) const {
    if (x != a.x)
        return x < a.x;
    return y < a.y;
    }
} p[N];

struct Edge {
    int y1, y2;
    Edge(int y1 = 0, int y2 = 0) {
    this->y1 = y1;
    this->y2 = y2;
    }
    bool operator < (const Edge a) const {
    if (y1 != a.y1)
        return y1 < a.y1;
    return y2 < a.y2;
    }
} e[N * N / 2];

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        p[i].read();
    sort(p, p + n);
    int en = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
        if (p[j].x != p[i].x) break;
        e[en++] = Edge(p[i].y, p[j].y);
        }
    }
    sort(e, e + en);
    long long cnt = 1, ans = 0;
    for (int i = 1; i < en; i++) {
        if (e[i].y1 == e[i - 1].y1 && e[i].y2 == e[i - 1].y2)
        cnt++;
        else {
        ans += cnt * (cnt - 1) / 2;
        cnt = 1;
        }
    }
    ans += cnt * (cnt - 1) / 2;
    printf("Case %d: %lld\n", ++cas, ans);
    }
    return 0;
}

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

时间: 2024-07-29 04:21:33

UVA 10574 - Counting Rectangles(枚举+计数)的相关文章

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 计数

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

uva 294 - Divisors(枚举+计数)

题目连接:uva 294 - Divisors 题目大意:给出一个范围L~U,问说在该范围中因子数最多的数是多少. 解题思路:枚举L~U中的数,将数分解成质因子,利用乘法原理求总因子数. #include <cstdio> #include <cstring> #include <cmath> int countFactor (int x) { int ans = 1; int m = sqrt(x+0.5); for (int i = 2; i <= m; i+

uva 1436 - Counting heaps(计数)

题目链接:uva 1436 - Counting heaps 题目大意:给出一个树的形状,现在为这棵树标号,保证根节点的标号值比子节点的标号值大,问有多少种标号树. 解题思路:和村名排队的思路是一只的uva11174,最后问题只和树德结构有直接关系,f(root)=(s(root)?1)!(s(1)?s(2)???s(n) 但是给定的取模数不是质数,所以不能用逆元做,只能将分子分母分别拆分成质因子,然后对质因子进制约分.因为最后的答案一定是正整数,所以对于每个质因子,分子分解出的因子个数一定大于

UVA 12075 - Counting Triangles(容斥原理计数)

题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对于两点,求他们的gcd - 1,得到的就是他们之间有多少个点,那么情况数就可以求了,然后还是利用容斥原理去计数,然后累加出答案 代码: #include <stdio.h> #include <string.h> #include <algorithm> using nam

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

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,

uva 11123 - Counting Trapizoid(容斥+几何)

题目链接:uva 11123 - Counting Trapizoid 题目大意:给定若干个点,问有多少种梯形,不包括矩形,梯形的面积必须为正数.因为是点的集合,所以不会优重复的点. 解题思路:枚举两两点,求出该条直线,包括斜率k,偏移值c,以及长度l.已知梯形的性质,一对对边平行,也就是说一对平行但是不相等的边. 所以将所有线段按照斜率排序,假设对于某一斜率,有m条边,那么这m条边可以组成的含平行对边的四边形有C(2m),要求梯形还要减掉长度相同以及共线的情况,分别对应的是l相同和c相同,但是

uva 269 - Counting Patterns(构造)

题目链接:uva 269 - Counting Patterns 题目大意:给出n和k,要求找出满足的序列,要求为n元组,由-k到k组成,并且和为0.求出所有满足的元组个数,并且对于左移,右移,水平翻转,每个元素取相反数相同的视为一种,用字典序最大的表示,输出按照字典序最小的输出. 解题思路:因为表示的时候按照字典序最大的表示,一开始枚举开头的位置,那么在后面的数的绝对值就不会大于该数.最后判断一下,如果该序列不是最优的表示方法,就不是该情况. #include <cstdio> #inclu