Rectangular Polygons

题目链接:

http://acm.hust.edu.cn/vjudge/contest/129733#problem/G

Description

In this problem, we will help the Faculty of Civil Engineering. They need a software to analyze ground plans of buildings. Specifically, your task is to detect outlines of a building when all of its corners are given.
You may assume that each building is a rectangular polygon with each of its sides being parallel either with X or Y axis. Therefore, each of its vertex angles is exactly either 90 or 270 degrees.

Input

The input contains several buildings. The description of each building starts with a single positive integer N , the number of corners (polygon vertices), 1 <= N <= 1000 . Then there are N pairs of integer numbers Xi, Yi giving coordinates of individual corners, | Xi|,| Yi| <= 10000 .
You may assume that all corners are listed and no two of them have the same coordinates. The polygon does always exist, it is closed, its sides do not intersect or touch (except neighboring sides, of course), and it contains no "holes" inside. In other words, the outline is formed by one closed line. The order of corners in the input file may be arbitrary.
There is an empty line after each building, then the next one is described. After the last building, there is a single zero that signals the end of input.

Output

For each building, output one line containing N characters without any whitespace between them. The characters should be uppercase letters that specify directions of individual walls (sides) when the building outline is followed. " N" stands for North (the positive direction of the Y axis), `` E" for East (the positive direction of the X axis), "W" for West, and " S" for South. The "walk" should start in the vertex that has been given first in the input and always proceed in the clockwise direction.

Sample Input

4
0 0
2 2
0 2
2 0
6
1 1
2 2
0 1
1 0
0 2
2 0
0

Sample Output

NESW
WNESWN

Source

2016-HUST-线下组队赛-2

题意:

给出平面上N个点,构造一个每条边都平行于轴的多边形.
从起点#1开始顺时针输出行进方向.

题解:

观察结果图形:所有边一定平行轴. 那么对于y坐标相同的若干点#1~#m,有若干推论:(均可以反证)
首先,m的一定是偶数,否者多出来的点不能正常连边.
然后,这m个点一定有 m/2 条边,否则剩下的边不能正常连边.
最后,这m个点的连边方式一定是:(1,2),(3,4)...(m-1,m),否则会出现交叉或多余点.
对于x坐标相同的点也是如此.
对所有点按x为第一优先级排序后,可以将所有竖边连起来;同理得到所有横边.

现在已经得到关于最后结果的图模型,问题是怎么按要求输出:
直接按从#1开始或许有点麻烦,这里可以从最左上角的点开始往右走,这样保证了输出的轨迹一定是顺时针.
当遍历到#1时才开始输出,计数n次输出即结束. 所以最多遍历2次.

这题瞎搞了两小时,写了160+行结果TLE... 赛时想得太复杂,各种xjb删点删边瞎搞一通.
今天真是打得太挫了,2个小时写两题,后面的时间都在搞一个题. 两个人搞还是有点蛋疼,有几道比较简单的题都没开.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <list>
#define LL long long
#define eps 1e-8
#define maxn 1010
#define mod 100000007
#define inf 0x3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;int n;
struct Point {int id;int x, y;
}p[maxn];
int g[maxn][4];bool cmp1(Point a, Point b) {if(a.y == b.y) return a.x < b.x;return a.y < b.y;
}
bool cmp2(Point a, Point b) {if(a.x == b.x) return a.y < b.y;return a.x < b.x;
}int main(int argc, char const *argv[])
{//IN;while(scanf("%d", &n) != EOF && n){memset(g, 0, sizeof(g));int cur = 0, cx = inf, cy = -inf; //top-left mostfor(int i=1; i<=n; i++) {scanf("%d %d", &p[i].x,&p[i].y);p[i].id = i;if(p[i].y > cy) cx = p[i].x, cy = p[i].y, cur = i;if(p[i].y == cy && p[i].x < cx) cx = p[i].x, cur = i;}sort(p+1, p+1+n, cmp1);for(int i=1; i<=n; i+=2) {g[p[i].id][1] = p[i+1].id;g[p[i+1].id][3] = p[i].id;}sort(p+1, p+1+n, cmp2);for(int i=1; i<=n; i+=2) {g[p[i].id][0] = p[i+1].id;g[p[i+1].id][2] = p[i].id;}int order = 0;bool flag = 0;int cnt = 0;while(1) {if(cur == 1) flag = 1;char ans = 0;int tmp = cur;if(!order) {if(g[tmp][1]) cur = g[tmp][1], ans = 'E';if(g[tmp][3]) cur = g[tmp][3], ans = 'W';} else {if(g[tmp][0]) cur = g[tmp][0], ans = 'N';if(g[tmp][2]) cur = g[tmp][2], ans = 'S';}order = !order;if(flag) putchar(ans), cnt++;if(cnt >= n) break;}putchar('\n');}return 0;
}

转载于:https://www.cnblogs.com/Sunshine-tcf/p/5797769.html

UVALive 3959 Rectangular Polygons (排序贪心)相关推荐

  1. LeetCode 2611. 老鼠和奶酪:排序 + 贪心

    [LetMeFly]2611.老鼠和奶酪:排序 + 贪心 力扣题目链接:https://leetcode.cn/problems/mice-and-cheese/ 有两只老鼠和 n 块不同类型的奶酪, ...

  2. POJ3687拓扑排序+贪心

    题意:       给你n个求,他们的重量是1-n(并不是说1号求的重量是1...),然后给你m组关系a,b,表示a的重量小于b的重量,然后让你输出满足要求的前提下每个球的重量,要求字典序最小. 思路 ...

  3. [蓝桥杯][算法提高VIP]线段和点(排序+贪心)

    题目描述 有n个点和m个区间,点和区间的端点全部是整数,对于点a和区间[b,c],若a> =b且a< =c,称点a满足区间[b,c]. 求最小的点的子集,使得所有区间都被满足. 数据规模和 ...

  4. USACO2.1【bfs,排序,贪心,dfs,位运算】

    前言 开始刷USACO的题了. 正题 刷前必备技能:OI常识,bfs,dfs,位运算,基础贪心,快速排序. T1:城堡 TheCastleThe CastleTheCastle 评测记录: https ...

  5. ACM学习历程—HihoCoder1309任务分配(排序 贪心)

    http://hihocoder.com/problemset/problem/1309 题目大意是给定n个任务的起始时间,求问最少需要多少台机器. 有一个贪心的策略就是,如果说对于一个任务结束,必然 ...

  6. BZOJ2535: [Noi2010]Plane 航空管制2(拓扑排序 贪心)

    题意 题目链接 Sol 非常妙的一道题. 首先不难想到拓扑排序,但是直接对原图按\(k\)从小到大拓扑排序是错的.因为当前的\(k\)大并不意味着后面的点\(k\)也大 但是在反图上按\(k\)从大到 ...

  7. leetcode-1833. 雪糕的最大数量(排序+贪心)

    题目链接:https://leetcode.cn/problems/maximum-ice-cream-bars/ 思路 直观想法 在给定的硬币情况下,花最小的钱,买最多的雪糕,一眼贪心. 吐槽一句: ...

  8. 贪心算法解决活动安排-Python实现(排序+贪心选择)

    贪心算法解决活动安排 问题 问题概述 分析问题 解决问题 编程 编程流程以及数据类型选择 发现问题以及解决 最终实现 总结 程序缺陷以及完善 解题心路历程 问题 问题概述 设有n个活动的集合E={1, ...

  9. A. String Reconstruction(三种解法,排序贪心或跳步或并查集)

    这题看了下有很多解法,一步一步来 Ⅰ . 区 间 贪 心 排 序 \color{Red}Ⅰ.区间贪心排序 Ⅰ.区间贪心排序 这是自己想的,而且似乎没看到有人用?(明明相对最容易想到) 把 每 个 字 ...

最新文章

  1. CentOS 6.3 运维监控之Cacti 监控主机系统(二)
  2. Android动画之Animator
  3. golang查找重复行
  4. 在 eclipse 中 设置 jvm 的 运行时目录
  5. 假定某一个数据列表是一个班级的计算机课程,若需要利用形参直接访问实参,则应把形参变量说明为()参数。A.指针B.引用C.传值D.常值...
  6. 周末给女友讲了遍加密算法,没想到...
  7. html倒计时timer,js如何使用定时器实现倒计时功能
  8. X86汇编语言从实模式到保护模式16:特权级和特权级保护
  9. python装饰器详解-Python装饰器基础概念与用法详解
  10. hbase倒序查询_hbase过滤器汇总【查询汇总】
  11. MySQL的快速修复
  12. Java之父评价C语言之父,Java之父评价C语言之父:C语言撑起了一切
  13. 输入一行字符,统计其中的英文字符、数字字符、空格和其他字符的个数。
  14. android 陀螺仪传感器性能损耗,传感器专题(3)——加速度/陀螺仪传感器
  15. 计算机毕业设计Java心理健康管理系统(源码+系统+mysql数据库+Lw文档)
  16. 从面试官的视角来提升面试者的必须具备的IT技能
  17. HDU6331Problem M. Walking Plan
  18. 数字电视音视频马赛克和不同步现象原因
  19. n! 结果尾数中零的数量
  20. 计算机动画关键技术,计算机动画关键技术综述ppt课件.ppt

热门文章

  1. jpeg编码学习笔记
  2. 台式电脑没鼠标怎么移动光标_不用鼠标用键盘,怎么移动光标?
  3. 管理人员巡店用表-生鲜部经理及主管每日检查事项
  4. hdwiki 编码规范
  5. 工欲善其事必先利其器–SimpleTestBed
  6. linux 图片修复,照片修复精灵软件-照片修复精灵手机版下载v1.0.1-Linux公社
  7. 天才少年稚晖君 | 【保姆级教程】个人深度学习工作站配置指南
  8. AHRS和INS的区别
  9. 设置python程序开机自启动
  10. 宝塔部署网站无法访问