记录一下我一直很怕做的一道离散化题。都卡了大半个月了,今天终于过了。这种离散化的题就一个恶心,不然早就啃过去了。

代码如下:

View Code

  1 /*
  2 ID: lyon.ly1
  3 LANG: C++
  4 TASK: rect1
  5 */
  6
  7 #include <cstring>
  8 #include <iomanip>
  9 #include <cmath>
 10 #include <cstdio>
 11 #include <iostream>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <string>
 15 #include <queue>
 16 #include <ctime>
 17 #include <map>
 18 #include <set>
 19
 20 using namespace std;
 21
 22 #define PB push_back
 23 #define FI first
 24 #define SE second
 25 #define MPR make_pair
 26 #define REP(i, n) for (int i = 0; i < n; i++)
 27 #define REP_1(i, n) for (int i = 1; i <= n; i++)
 28 #define FORI(i, a, b) for (int i = a; i < b; i++)
 29 #define FORD(i, a, b) for (int i = a; i > b; i--)
 30 #define _clr(x) memset(x, 0, sizeof(x))
 31 #define _rst(x) memset(x, -1, sizeof(x))
 32
 33 typedef long long LL;
 34 typedef pair<int, int> PII;
 35 typedef vector<LL> VLL;
 36 typedef vector<PII> VPII;
 37 typedef vector<int> VI;
 38 typedef vector<double> VDBL;
 39 const int N = 2505;
 40 const int hashMod = 1e6 + 5;
 41 const int inf = 0x55555555;
 42 const double eps = 1e-8;
 43 const LL linf = 0x5555555555555555ll;
 44 const double finf = 1e50;
 45 const double pi = acos(-1.0);
 46 const int mod = 1e9 + 7;
 47
 48 FILE *fin = fopen("rect1.in", "r");
 49 FILE *fout = fopen("rect1.out", "w");
 50
 51 struct Rect {
 52     int x1, y1, x2, y2, cl;
 53     Rect(int a = 0, int b = 0, int c = 0, int d = 0, int e = 0) {
 54         if (a > c) swap(a, c);
 55         if (b > d) swap(b, d);
 56         x1 = a, y1 = b, x2 = c, y2 = d, cl = e;
 57     }
 58 } ;
 59
 60 queue<Rect> Q;
 61 int clr[N];
 62
 63 bool belong(int x1, int y1, int x2, int y2, Rect &x) {
 64     return x.x1 <= x1 && x2 <= x.x2 && x.y1 <= y1 && y2 <= x.y2;
 65 }
 66
 67 void insert(Rect x) {
 68     int sz = Q.size();
 69     int X[5], Y[5];
 70     while (sz--) {
 71         Rect tmp = Q.front();
 72 //        printf("!!!          %d %d %d %d %d\n", tmp.x1, tmp.y1, tmp.x2, tmp.y2, tmp.cl);
 73         Q.pop();
 74         if (tmp.x2 <= x.x1 || x.x2 <= tmp.x1) { Q.push(tmp); continue;}
 75         if (tmp.y2 <= x.y1 || x.y2 <= tmp.y1) { Q.push(tmp); continue;}
 76 //        puts("~~~~~~~~~~~~~~~");
 77 //        printf("rect %d %d %d %d %d\n", tmp.x1, tmp.y1, tmp.x2, tmp.y2, tmp.cl);
 78         X[0] = x.x1, X[1] = x.x2;
 79         Y[0] = x.y1, Y[1] = x.y2;
 80         X[2] = tmp.x1, X[3] = tmp.x2;
 81         Y[2] = tmp.y1, Y[3] = tmp.y2;
 82         sort(X, X + 4);
 83         sort(Y, Y + 4);
 84 //        REP(i, 4) {
 85 //            printf("x %d y %d\n", X[i], Y[i]);
 86 //        }
 87         REP(i, 3) {
 88             if (X[i] == X[i + 1]) continue;
 89             REP(j, 3) {
 90                 if (Y[j] == Y[j + 1]) continue;
 91                 if (belong(X[i], Y[j], X[i + 1], Y[j + 1], tmp) && !belong(X[i], Y[j], X[i + 1], Y[j + 1], x)) {
 92                     Q.push(Rect(X[i], Y[j], X[i + 1], Y[j + 1], tmp.cl));
 93 //                    printf("pb %d %d %d %d %d\n", X[i], Y[j], X[i + 1], Y[j + 1], tmp.cl);
 94                 }
 95             }
 96         }
 97 //        puts("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
 98     }
 99     Q.push(x);
100 }
101
102 int main() {
103     int a, b, c, d, e, n;
104 //    freopen("in", "r", stdin);
105     while (~fscanf(fin, "%d%d%d", &a, &b, &n)) {
106         while (Q.size()) Q.pop();
107         _clr(clr);
108         Q.push(Rect(0, 0, a, b, 1));
109         while (n--) {
110             fscanf(fin, "%d%d%d%d%d", &a, &b, &c, &d, &e);
111 //            printf("Rect ~~~~~~~~~~~~~~~~~~     %d %d %d %d %d\n", a, b, c, d, e);
112             insert(Rect(a, b, c, d, e));
113         }
114         while (Q.size()) {
115             Rect tmp = Q.front();
116 //            printf("%d %d %d %d %d\n", tmp.x1, tmp.y1, tmp.x2, tmp.y2, tmp.cl);
117             Q.pop();
118             clr[tmp.cl] += (tmp.x2 - tmp.x1) * (tmp.y2 - tmp.y1);
119         }
120         REP(i, N) {
121             if (clr[i]) {
122                 fprintf(fout, "%d %d\n", i, clr[i]);
123             }
124         }
125     }
126     return 0;
127 }

——written by Lyon

转载于:https://www.cnblogs.com/LyonLys/archive/2012/12/13/USACO_rect1_Lyon.html

USACO Shaping Regions(离散化)相关推荐

  1. usaco Shaping Regions

    这就是usaco 前面的windows area的变形. /* ID:jinbo wu TASK:rect1 LANG:C++ */ #include<iostream> #include ...

  2. ural1147 Shaping Regions

    Shaping Regions Time limit: 0.5 second Memory limit: 64 MB N opaque rectangles (1 ≤ N ≤ 1000) of var ...

  3. usaco Section 3.1 Shaping Regions -- 矩形切割

    听说这道题用矩形切割做,于是学习了2004薛矛的oi论文. 我的做法是:弄一个白纸集合,开始里面只有一张白纸(0,0) (A,B).然后把输入的N个矩形倒序地,每一个都与白纸集合中的所有白纸依次切割. ...

  4. 算法学习之——矩形切割思想

    算法学习之--矩形切割思想                                                      MPS [定义 Define]   对于求解若干个矩形的面积的交集 ...

  5. 博客模板:xiu-v7-0阿里百秀主题-去域名限制

    <![endif]--> 公交车司机终于在众人的指责中将座位让给了老太太 </div> </div> </figure> 白嫖博客 你已登录为 : 13 ...

  6. 训练技巧《Must Know Tips/Tricks in Deep Neural Networks (by Xiu-Shen Wei)》学习笔记

    原 <Must Know Tips/Tricks in Deep Neural Networks (by Xiu-Shen Wei)>学习笔记 2019年01月19日 22:20:40 咸 ...

  7. 当前进度,已学算法,未做题目

    当前学习 CSP-S备战中 已学算法 从初一开始算起,按照时间顺序排序 算法简称 算法全称 备注 无 高精度 比较基础的算法 d e p t h f i r s t s e a r c h ( D F ...

  8. usaco Picture(离散化求线段周长)

    usac前面有一题是递归求矩形覆盖面积的,学到不少东西 离散化 把所有矩形离散化(就是将整个平面分成许多"竖条"或"横条",对其操作),每个矩形都由四条边组成, ...

  9. USACO 3.3.2 Shopping Offers解题报告

    写在前面:因为之前没写的C++的USACO Training的解题报告太多--所以就不写了,要是想要代码可以联系我:xiedong_1993@foxmail.com 这题就是传说中的五维背包,其实写起 ...

最新文章

  1. C++中map的用法
  2. 皮一皮:原来微信备注还有这个用...
  3. WebGoat系列实验Cross-Site Scripting (XSS)
  4. 环境污染,拿什么来保障食品安全?
  5. Maven工程 报 Diamond types are not supported at language level ‘5‘
  6. Linux性能优化之“关闭Ctrl+Alt+Del”
  7. java 日历工具_java中强大的时间处理工具:Calendar类(日历类)
  8. 安装VS2008关于解决磁盘已满问题方案.
  9. bzoj 1257: [CQOI2007]余数之和sum
  10. 挑战程序设计竞赛(第2版)1.6.1题
  11. 在电脑上如何剪辑音乐?
  12. 刷好老毛子系统进不了老毛子系统后台的解决办法
  13. UART串口协议时序图
  14. 关于Google Chrome浏览器离线安装包下载方法
  15. 利用浏览器检查获取网页视频
  16. vue设置页面背景色
  17. java 生成证书图片_java生成自定义证书图片1 - 制作证书word模板
  18. bcc钱包地址生成linux,从Bcc到xdp原理分析
  19. 【文献阅读笔记】KAM Theory Meets Statistical Learning Theory: Hamiltonian Neural Networks with Non-Zero Trai
  20. 小程序开发——模板与配置

热门文章

  1. 区块链用AI和大数据改变行业现状
  2. HTML5 如何实现拖放'N'拖放
  3. 2017-9-13:学习笔记
  4. Java克隆(Clone)的应用
  5. bitcask存储引擎
  6. 总结八个好用的Python爬虫技巧
  7. js 异步for each
  8. 《计算机科学概论》—第2章2.2节位置记数法
  9. 转换营销阵地 特步的世界杯身影
  10. Flask实例教程四