[SGU 532]Building Foundation[几何]
来源:互联网 发布:ubuntu u盘系统 编辑:程序博客网 时间:2024/04/30 16:13
题目链接:[SGU 532]Building Foundation[几何]
题意分析:
给出n个要么平行于x轴要么平行于y轴的线段,问:当前给出的图中一共有多少个矩形?
解题思路:
枚举竖直方向上的线段,然后根据每次枚举的两个线段确定它们之间能有多少个合格水平线段,可以先用二分大致找出范围,然后具体细找符合要求的线段,假设有x个,那么当前这两条竖直线段和这些水平线段能构成的矩形数为:x * (x - 1) / 2。最终答案就是将所有加起来即可。
个人感受:
看到找矩形就方了= =没有想到的就是枚举完可行的水平线段后答案可以那么计算。。。。。。
具体代码如下:
#include<algorithm>#include<cctype>#include<cmath>#include<cstdio>#include<cstring>#include<iomanip>#include<iostream>#include<map>#include<queue>#include<set>#include<sstream>#include<stack>#include<string>#define lowbit(x) (x & (-x))#define root 1, n, 1#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 1#define pii pair<int, int>#define ll long long#define pr(x) cout << #x << " = " << (x) << '\n';using namespace std;const int INF = 0x7f7f7f7f;const int MAXN = 611;struct P { int mi, mx, x; P(int a, int b, int c): mi(a), mx(b), x(c){} bool operator < (const P &t)const { return x < t.x; }};vector<P> hor, ver;int main(){ #ifdef LOCAL freopen("C:\\Users\\apple\\Desktop\\in.txt", "r", stdin); #endif int x1, y1, x2, y2; int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) ver.push_back(P(min(y1,y2),max(y1,y2), x1)); else hor.push_back(P(min(x1,x2), max(x1,x2), y1)); } sort(hor.begin(), hor.end()); ll ans = 0; for (int i = 0; i < ver.size(); ++i) { for (int j = i + 1; j < ver.size(); ++j) { int l = min(ver[i].x, ver[j].x); int r = max(ver[i].x, ver[j].x); int up = min(ver[i].mx, ver[j].mx); int down = max(ver[i].mi, ver[j].mi); if (l == r || up == down) continue; //pr(l);pr(r);pr(up);pr(down); //cout << "begin:\n"; int lef = lower_bound(hor.begin(), hor.end(), P(0, 0, down)) - hor.begin(); int rig = upper_bound(hor.begin(), hor.end(), P(0, 0, up)) - hor.begin(); ll ret = 0; for (int k = lef; k < rig; ++k) { P &t = hor[k]; // cout << t.x << ',' << t.mi << ',' << t.mx << endl; if (t.x <= up && t.x >= down && t.mi <= l && t.mx >= r) ++ret; } ans += ret * (ret - 1) / 2; } } printf("%lld\n", ans); return 0;}
0 0
- [SGU 532]Building Foundation[几何]
- SGU 532 Building Foundation(枚举)
- SGU 532 Building Foundation(组合计数)
- SGU 467 Chessmaster 几何
- SGU 110 计算几何
- Building Foundation 模拟题
- SGU 120 Arhipelago(计算几何)
- SGU 110 Dungeon(计算几何)
- SGU 129 Inheritance(计算几何)
- SGU 120 Archipelago (简单几何)
- sgu-110 Dungeon(计算几何)
- SGU 120 Archipelago (计算几何)
- sgu 465 Fire Station Building 三分
- sgu 110 Dungeon 三维计算几何
- SGU 110 Dungeon(计算几何)
- SGU 120 Archipelago(计算几何)
- SGU 124 Broken line(计算几何)
- SGU 136 Erasing Edges(计算几何)
- 小白也玩数学建模--引
- entity resolution
- 通过shell脚本来搭建python开发环境
- 对Java事物的理解
- iOS学习笔记-iOS中的类方法和实例方法
- [SGU 532]Building Foundation[几何]
- Android基础之本地广播及文件存储和SharedPreference存储
- UISearchController VS UISearchBar and UISearchDisplayController
- FAT32与NTFS区别
- HDU-1443 Joseph
- 3.Git远程仓库添加及克隆
- 模板的实例化和具体化
- 2015 Asia - Jakarta D - An ICPC Problem without Statement
- 1025. PAT Ranking (25)