题目链接:

id=3168">POJ 3168 Barn Expansion

题意:抽象出来就是给出n个矩形的坐标是(左下角和右上角的坐标,矩形的边都是平行x,y轴),问有几个矩形和其它矩形没有接触(仅仅存在边接触或者点接触,不存在有公共面积)。

思路:把边分成两类,平行x轴和平行y轴。对边进行排序。然后for一遍推断是否有相交就可以

AC代码:

#include <stdio.h>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std; struct node {
int mark;
int d,xx,yy;
node() {}
node(int _d,int _xx,int _yy,int _mark) {
d=_d,xx=_xx,yy=_yy,mark=_mark;
}
}; vector<node> sx,sy;
bool vis[25010];
bool cmp(node a,node b) {
if(a.d!=b.d) return a.d<b.d;
else if(a.xx!=b.xx) return a.xx<b.xx;
else return a.yy<b.yy;
} int main() {
int n;
int i,j,k;
int a,b,c,d;
while(scanf("%d",&n)!=EOF) {
sx.clear();
sy.clear();
memset(vis,false,sizeof vis);
for(i=0; i<n; i++) {
scanf("%d %d %d %d",&a,&b,&c,&d);
sy.push_back(node(b,a,c,i));
sy.push_back(node(d,a,c,i));
sx.push_back(node(a,b,d,i));
sx.push_back(node(c,b,d,i));
}
int sz1,sz2;
sz1=sy.size();
sz2=sx.size();
sort(sx.begin(),sx.end(),cmp);
sort(sy.begin(),sy.end(),cmp); //竖 y<yy
int up;
up=sy[0].yy;
for(i=1;i<sz1;i++){
if(sy[i-1].d == sy[i].d){
if(up >= sy[i].xx){
vis[sy[i].mark]=vis[sy[i-1].mark]=true;
}
}
else up=sy[i].yy;
up=max(sy[i].yy,up);
}
up=sx[0].yy;
for(i=1;i<sz2;i++){
if(sx[i-1].d == sx[i].d){
if(up >= sx[i].xx){
vis[sx[i].mark]=vis[sx[i-1].mark]=true;
}
}
else up=sx[i].yy;
up=max(sx[i].yy,up);
}
int ans=0;
for(i=0;i<n;i++){
if(!vis[i]) ans++;
}
printf("%d\n",ans);
}
return 0;
}
/*
5
0 2 2 7
3 5 5 8
4 2 6 4
6 1 8 6
0 0 8 1 4
2 1 3 2
2 2 3 3
3 3 4 4
4 1 5 2 9
0 0 1 1
1 0 2 1
2 0 3 1
0 1 1 2
1 1 2 2
2 1 3 2
0 2 1 3
1 2 2 3
2 2 3 3 6
0 2 2 7
3 5 5 8
4 2 6 4
6 1 8 6
0 0 8 1
4 5 5 6 3
1 1 6 6
6 2 7 3
6 5 8 7
*/

最新文章

  1. svn常用命令
  2. sqlmap --dns-domain模拟实践
  3. cms 常用标签
  4. Reflector 已经out了,试试ILSpy
  5. code vs 1506 传话
  6. javascript的坑
  7. html简单框架网页制作
  8. Topcoder SRM 584 DIV1 600
  9. maven + appium + testng + java之pom.xml
  10. js的数组申明
  11. 用PHP操作http中Etag、lastModified和Expires标签
  12. haslayout和BFC
  13. 转: Python 运算符与用法
  14. Best Time to Buy and Sell Stock II 解答
  15. 笔记:利用Cocos2dx 3.3 lua 做一个动作类游戏(一)
  16. linux内核链表---挑战常规思维
  17. read()和write()
  18. Windows系统下查看某一进程下所有线程的dos命令
  19. Python paramiko 修改源码实现用户命令抓取
  20. codeblocks调试

热门文章

  1. activiti自己定义流程之Spring整合activiti-modeler实例(七):任务列表展示
  2. java 清单文件
  3. TCP 中出现RST的情况
  4. JavaWeb response对象常用操作
  5. 20-spring学习-Spring MVC基本操作
  6. spring spel表达式语言
  7. MongoDB 日志太大怎么办?
  8. springmvc sitemesh json问题
  9. ios开发-Object-C可变参数函数
  10. iOS开发:icon和启动图尺寸(转)