CSU-2116 Polyline Simplification

Description

Mapping applications often represent the boundaries of countries, cities, etc. as polylines, which are connected sequences of line segments. Since fine details have to be shown when the user zooms into the map, these polylines often contain a very large number of segments. When the user zooms out, however, these fine details are not important and it is wasteful to process and draw the polylines with so many segments. In this problem, we consider a particular polyline simplification algorithm designed to approximate the original polyline with a polyline with fewer segments.

A polyline with n segments is described by n + 1 points \(p_0=(x_0,y_0),...,p_n=(x_n,y_n)\), with the ith line segment being pi−1pi. The polyline can be simplified by removing an interior point \(p_i\)(1≤i≤n−1), so that the line segments $p_{i−1} p_i $and \(p_ip_{i+1}\) are replaced by the line segment \(p_{i − 1}p_{i + 1}\). To select the point to be removed, we examine the area of the triangle formed by \(p_{i−1},p_i\),and $ p_{i+1}$(the area is 0 if the three points are colinear), and choose the point pi such that the area of the triangle is smallest. Ties are broken by choosing the point with the lowest index. This can be applied again to the resulting polyline, until the desired number m of line segments is reached.

Consider the example below.

The original polyline is shown at the top. The area of the triangle formed by p2,p3,and p4 is considered (middle), and p3 is removed if the area is the smallest among all such triangles. The resulting polyline after p3 is removed is shown at the bottom.

Input

The first line of input contains two integers n(2≤n≤200000) and m(1≤m<n)m(1≤m<n). The next n + 1 lines specify p0,...,pn. Each point is given by its x and y coordinates which are integers between −5000 and 5000 inclusive. You may assume that the given points are strictly increasing in lexicographical order. That is, \(xi<x_{i+1}\) , or \(xi=x_{i+1}\) and \(yi<y_{i+1}\) for all 0≤i<n .

Output

Print on the kth line the index of the point removed in the kth step of the algorithm described above (use the index in the original polyline).

Sample Input

10 7
0 0
1 10
2 20
25 17
32 19
33 5
40 10
50 13
65 27
75 22
85 17

Sample Output

1
9
6

题解

题意:给定n+1个点,每个点可以与它左右两个点形成一个三角形,每次删去最小的那个三角形的中间那个点,直到剩下m+1个点,求被删除点的编号。

这是一道模拟题,需要用到叉积(向量积)计算三角形面积

\[S_{ABC}= \frac{|\overrightarrow{AB} \times \overrightarrow{AC}|}{2}
\]

带入三点坐标化简一下即可,由于题目中所给坐标均为整数,所以我们可以存不除2的结果,这样都为整数可以用int存。我们用set维护一下最小三角形的面积,该三角形中间点是第几个点。用数组模拟一下双向链表维护一下这个点左右各是哪个点,删除这个点时更新一下就好了,记得如果该三角形不是第一个或者最后一个三角形的话还要分别更新一下新形成的三角形的面积。

#include<bits/stdc++.h>
#define maxn 200050
using namespace std;
struct point {
int x, y;
} a[maxn];
int l[maxn], r[maxn];
int area[maxn];
struct node {
int val, id;
node(int val = 0, int id = 0): val(val), id(id) {}
bool operator < (const node &a) const {
if (val == a.val) return id < a.id;
else return val < a.val;
}
};
int calc1(int x) {
int p1 = l[x], p2 = x, p3 = r[x];
return abs(a[p1].x * a[p2].y + a[p2].x * a[p3].y + a[p3].x * a[p1].y - a[p1].x * a[p3].y - a[p2].x * a[p1].y - a[p3].x * a[p2].y);
}
set<node> s;
int main() {
int n, m;
scanf("%d%d", &n, &m);
int k = n - m;
for (int i = 0; i <= n; i++) {
scanf("%d%d", &a[i].x, &a[i].y);
}
r[0] = 1;
l[n] = n - 1;
for (int i = 1; i < n; i++) {
l[i] = i - 1; r[i] = i + 1;
area[i] = calc1(i);
s.insert(node(area[i], i));
}
for (int i = 1; i <= k; i++) {
set<node>::iterator it = s.begin();
int now = (*it).id;
printf("%d\n", now);
s.erase(it);
l[r[now]] = l[now];
r[l[now]] = r[now];
if (l[now] > 0) {
it = s.find(node(area[l[now]], l[now]));
s.erase(it);
area[l[now]] = calc1(l[now]);
s.insert(node(area[l[now]], l[now]));
}
if (r[now] < n) {
it = s.find(node(area[r[now]], r[now]));
s.erase(it);
area[r[now]] = calc1(r[now]);
s.insert(node(area[r[now]], r[now]));
}
}
return 0;
}
/**********************************************************************
Problem: 2116
User: Artoriax
Language: C++
Result: AC
Time:392 ms
Memory:15308 kb
**********************************************************************/

CSU-2116 Polyline Simplification的更多相关文章

  1. 中南大学2019年ACM寒假集训前期训练题集(基础题)

    先写一部分,持续到更新完. A: 寒衣调 Description 男从戎,女守家.一夜,狼烟四起,男战死沙场.从此一道黄泉,两地离别.最后,女终于在等待中老去逝去.逝去的最后是换尽一生等到的相逢和团圆 ...

  2. ArcEngine开发各种几何错误代码

    E_GEOMETRY_AMBIGUOUSPARTTYPE - Static variable in interface com.esri.arcgis.geometry.esriGeometryErr ...

  3. psimpl_v7_win32_demo

    psimpl - generic n-dimensional polyline simplification 通用N维折线简化程序 Author - Elmar de Koning 作者 - Elma ...

  4. [svg 翻译教程]Polyline(折线)polygon(多边形)

    原文: http://tutorials.jenkov.com/svg/polygon-element.html Polyline 虽然说这个 元素我没用过,但是还是蛮强大的,也翻译下 示例 < ...

  5. [javascript svg fill stroke stroke-width points polygon属性讲解] svg fill stroke stroke-width points polygon绘制多边形属性并且演示polyline和polygon区别讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  6. [javascript svg fill stroke stroke-width points polyline 属性讲解] svg fill stroke stroke-width points polyline 绘制折线属性讲解

    <!DOCTYPE html> <html lang='zh-cn'> <head> <title>Insert you title</title ...

  7. csu 1812: 三角形和矩形 凸包

    传送门:csu 1812: 三角形和矩形 思路:首先,求出三角形的在矩形区域的顶点,矩形在三角形区域的顶点.然后求出所有的交点.这些点构成一个凸包,求凸包面积就OK了. /************** ...

  8. CSU 1503 点到圆弧的距离(2014湖南省程序设计竞赛A题)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 解题报告:分两种情况就可以了,第一种是那个点跟圆心的连线在那段扇形的圆弧范围内,这 ...

  9. CSU 1120 病毒(DP)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1120 解题报告:dp,用一个串去更新另一个串,递推方程是: if(b[i] > a ...

随机推荐

  1. 虚拟机中Ubuntu安装vmtools

    1.解压vmtools文件为VMWARETO.TGZ VMtools文件一般在系统桌面,如果没有可以点击左上方的"虚拟机-安装VMware Tools"即可出现在桌面,也可以通过U ...

  2. 关于wp8.1 runtime模式下面的摄像头调用拍照问题和应用生命周期问题

    现在的msdn文档,还找不到详细的wp8.1的摄像头拍照文档,只有一个序列拍照,类似九连拍的文档,而且这文档感觉就是windows8.1搬过来应付的,wp8.1模式,只要有一个地方处理不好,手机就会死 ...

  3. Missing map from Nullable`1 to String. Create using Mapper.CreateMap<Nullable`1, String>. 解决办法

    这是一个叫做AutoMapper的插件,主要功能是让两个类的内容进行映射,最常见的例子就是EF查询出的内容映射到一个实体类上去然后返回这个实体类例如: Mapper.CreateMap(); 如果这时 ...

  4. 题解 CF734A 【Anton and Danik】

    本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...

  5. Windows8.1任务栏取消oneDrive图标

    Windows8.1任务栏会有oneDrive图标,用不着,想取消,方法如下:

  6. Oracle小技巧_不同表空间不同用户导入导出数据dmp

    [博主注:数据库方面 ITPUB网站及博客内容非常丰富]   转载来源ITPUB 博客 经常有人会问:原来的数据在USERS表空间里面,我想把它IMP进APP表空间,我已经修改了目的用户的默认表空间, ...

  7. 在Drupal7中创建web接口

    Services 模块允许您从一个主要模块后端配置和管理区域启用您 Drupal 站点上自定义构建的内容服务器和服务.该模块中包含的服务允许您调用内容,从 Drupal 的默认和分配的 File.Co ...

  8. Java之JDK的下载与安装,java环境变量的配置,Editplus的下载与使用

    JRE(Java Runtime Environment Java运行环境) 包括Java虚拟机(JVM Java Virtual Machine)和Java程序所需的核心类库等,如果想要运行一个开发 ...

  9. Bootstrap历练实例:响应式布局

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. for in 和 for of的区别详解

    for in 和 for of 相对于大家肯定都不陌生,都是用来遍历属性的没错.那么先看下面的一个例子: 例1 const obj = { a: 1, b: 2, c: 3 } for (let i ...