UVALive 3959 Rectangular Polygons (排序贪心)
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
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个小时写两题,后面的时间都在搞一个题. 两个人搞还是有点蛋疼,有几道比较简单的题都没开.
##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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 most
for(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;
}
UVALive 3959 Rectangular Polygons (排序贪心)的更多相关文章
- BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心
BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...
- CodeForces 1294B Collecting Packages(排序+贪心)
http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否 ...
- POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)
题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...
- poj1456 结构体排序+贪心
题意:给出很多商品,每个商品有价值和出售期限,只能在期限内出售才能获取利润,每一个单位时间只能出售一种商品,问最多能获得多少利润. 只需要按照优先价值大的,其次时间长的排序所有物品,然后贪心选择,从它 ...
- poj-2376 Cleaning Shifts (排序+贪心)
http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...
- UVaLive 6609 Meeting Room Arrangement (贪心,区间不相交)
题意:给定 n 个区间,让你选出最多的区间,使得每个区间不相交. 析:贪心题,贪心策略是按右端点排序,然后按着选即可. 代码如下: #pragma comment(linker, "/STA ...
- vijos 1605 双栈排序 - 贪心 - 二分图
题目传送门 传送门I 传送门II 题目大意 双栈排序,问最小字典序操作序列. 不能发现两个数$a_{j}, a_{k}\ \ (j < k)$不能放在同一个栈的充分必要条件时存在一个$i$使得$ ...
- 2019.01.20 bzoj5158 Alice&Bob(拓扑排序+贪心)
传送门 短代码简单题. 题意简述:对于一个序列XXX,定义其两个伴随序列a,ba,ba,b,aia_iai表示以第iii个数结尾的最长上升子序列长度,bib_ibi表示以第iii个数开头的最长下降 ...
- HDU 4857 逃生(反向建边的拓扑排序+贪心思想)
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
随机推荐
- POJ 2114 - Boatherds
原题地址:http://poj.org/problem?id=2114 题目大意: 给定一棵点数为\(n~(n \le 10000)\)的无根树,路径上有权值,给出m组询问($m \le 100$), ...
- 用VS2005开发WinCE程序调试图文教程
一.WinCE 模拟器通过ActiveSync 6.1(即Windows Mobile设备中心)连接P 1.启动WinCE模拟器 命令行: start .\DeviceEmulator.exe WI ...
- CodeForces Round #297 Div.2 E (中途相遇法)
当时打比赛的时候卡在D题了,没有看E.现在看来E还是不难的. 将n个数排序后,其实不排序也是可以的,只是排序能快一半的时间. 枚举前一半能得到多少种和,放到map里面: 然后在后一半数中枚举,然后在m ...
- HDU 4324 (拓扑排序) Triangle LOVE
因为题目说了,两个人之间总有一个人喜欢另一个人,而且不会有两个人互相喜欢.所以只要所给的图中有一个环,那么一定存在一个三元环. 所以用拓扑排序判断一下图中是否有环就行了. #include <c ...
- UVa 11019 (AC自动机 二维模式串匹配) Matrix Matcher
就向书上说得那样,如果模式串P的第i行出现在文本串T的第r行第c列,则cnt[r-i][c]++; 还有个很棘手的问题就是模式串中可能会有相同的串,所以用repr[i]来记录第i个模式串P[i]第一次 ...
- highCharts图表应用-实现多种图表的显示
在数据统计和分析业务中,有时需要在一个图表中将柱状图.饼状图.曲线图的都体现出来,即可以从柱状图中看出具体数据.又能从曲线图中看出变化趋势,还能从饼状图中看出各部分数据比重.highCharts可以轻 ...
- 中文分词系列(一) 双数组Tire树(DART)详解
1 双数组Tire树简介 双数组Tire树是Tire树的升级版,Tire取自英文Retrieval中的一部分,即检索树,又称作字典树或者键树.下面简单介绍一下Tire树. 1.1 Tire树 Trie ...
- Excel 之查找与替换
Excel查找与替换 1,一旦学会查找,替换就简单了. 2,查找下面有一个选项,里面有对查找的范围进行限制 3,你可以选中一个区域,然后再查找,查找只会在你选择的区域里面进行 4,查找只能找特定值,而 ...
- Scala下载安装配置(Mac)
---恢复内容开始--- 1.访问scala的官网这里下载最新版的scala. 2.解压缩文件包,可将其移动至/usr/local/share下 1 mv /download/scalapath /u ...
- 顶 兼容各种浏览器js折叠菜单
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...