题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5124

这题题目做的好悲催,比赛时题目意思不理解,也没有深究了,赛后又看了很久没有看懂,问了很多才搞懂,我有一种想哭的冲动,我一直把

这题[x,y]这个线段看成了一个坐标,我想哭了,由于x,y坐标带来的惯性,,本来题目告诉你了【x,y】这条线段,

这是一个区间,是数轴,是一维坐标,而一直把它看做一个二维坐标,悲剧了。

官方解析

1002 lines
我们可以将一条线段[xi,yi]分为两个端点xi和(yi)+1,在xi时该点会新加入一条线段,同样的,在(yi)+1时该点会减少一条线段,
因此对于2n个端点进行排序,令xi为价值1,yi为价值-1,问题转化成了最大区间和,因为1一定在-1之前,因此问题变成最大前缀和,
我们寻找最大值就是答案,另外的,这题可以用离散化后线段树来做。复杂度为排序的复杂度即nlgn,另外如果用第一种做法数组应
是2n,而不是n,由于各种非确定性因素我在小数据就已经设了n=10W的点。 代码

#include<stdio.h>
#include<algorithm>
using namespace std;

pair<int,int> a[200010];//相当于结构体作用。

int main(void)
{
int t,n,ans,k,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
n=n*2;
for(i=0;i<n;i++)
{
scanf("%d",&a[i].first);
a[i].second=1;
scanf("%d",&a[++i].first);
a[i].first++;
a[i].second=-1;
}
sort(a,a+n);
ans=0; k=0;
for(i=0;i<n;i++)
{
k=k+a[i].second;
ans=max(ans,k);
}
printf("%d\n",ans);
}
return 0;
}


离散化,首先把所有的坐标映射到x轴上,然后将坐标压缩。怎么压缩呢?

可以用一个v数组,直接存贮位置就可以了,那么这样就完成了坐标的压缩。

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <map>
#include <vector>

using namespace std;

#define MAXN 200005

typedef pair<int,int> PII;

int x[MAXN];
PII p[MAXN];
int v[MAXN];

int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int cnt = 0;
for(int i = 0; i<n; i++)
{
scanf("%d %d",&p[i].first,&p[i].second);
x[cnt++] = p[i].first;
x[cnt++] = p[i].second;
}
sort(x,x+cnt);
printf("%d\n",cnt);
printf("%d\n",x);
cnt = unique(x,x+cnt)-x;
printf("%d\n",cnt);
for(int i = 0; i<cnt; i++)
{
v[i] = 0;
}
int sum = 0;
for(int i = 0; i<n; i++)
{
int l = lower_bound(x,x+cnt,p[i].first)-x;
int r = lower_bound(x,x+cnt,p[i].second)-x;
v[l]++;
v[r+1]--;
}
int s = 0;
int mx = 0;
for(int i = 0; i<cnt; i++)
{
s+=v[i];
mx = max(mx,s);
}
printf("%d\n",mx);
}
return 0;
}

当然不一定非得把坐标映射到x轴上,vector <pair<int,int> > v; v[i].first 用来把坐标排序,起到了映射的作用。然后数组的下标就起到了压缩的作用。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

vector <pair<int,int> > v;

int main ()
{
int T;
cin >> T;
while (T--)
{
v.clear();
int N;
scanf("%d",&N);
for (int i = 0; i < N; i++)
{
int x;
scanf("%d",&x);
v.push_back(make_pair(x,1));
scanf("%d",&x);
v.push_back(make_pair(x + 1,-1));
}
sort(v.begin(), v.end());
int ans = 0;
int maxn = 0;
for (int i = 0; i < v.size(); i++)
{
ans += v[i].second;
maxn = max(maxn,ans);
}
cout << maxn << endl;
}
}

												

HUD-5124-lines的更多相关文章

  1. hud 5124 lines(思维 + 离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines   Problem Description: John has several lines. ...

  2. hdoj 5124 lines【线段树+离散化】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:给你n段区间,他们有重合的点A,问你重合最多次的点A重合多少次 题解:对区间离散化后,维护 ...

  3. hdu 5124 lines

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:给你n条线段,然后找出一个被最多条线段覆盖的点,输出覆盖这个点的线段的条数. 思路:可以把一条线段分 ...

  4. 【扫描线】HDU 5124 lines

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 [题意] 在数轴x上,每次操作都覆盖一个区间的所有点,问被覆盖次数最多的点是覆盖了多少次 [思路] 最简单 ...

  5. (树状数组+离散化)lines--hdu --5124

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines Time Limit: 5000/2500 MS (Java/Others)    Memor ...

  6. BestCoder20 1002.lines (hdu 5124) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...

  7. UI相关教程:HUD、UMG和Widget

    转自:http://aigo.iteye.com/blog/2258612 蓝图脚本来处理 ================================================== 用UM ...

  8. HDU5124:lines(线段树+离散化)或(离散化思想)

    http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...

  9. HUD——1083 Courses

    HUD——1083   Courses Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  10. WEBGL学习【十四】利用HUD技术在网页上方显示三维物体

    关键点: <!--实现原理:要保证这两个canvas相互重叠;z-index表示了两个画布的上下层关系--> <!--是WEBGL的三维图形Canvas(主要用于绘制三维场景)--& ...

随机推荐

  1. HDU 1160 FatMouse's Speed 动态规划 记录路径的最长上升子序列变形

    题目大意:输入数据直到文件结束,每行两个数据 体重M 和 速度V,将其排列得到一个序列,要求为:体重越大 速度越低(相等则不符合条件).求这种序列最长的长度,并输出路径.答案不唯一,输出任意一种就好了 ...

  2. 生日蛋糕(DFS)

    题意: Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体.   设从下往上数第i(1 <= i <= M)层蛋糕 ...

  3. UIView的alpha、hidden、opaque 深入

    转载自:http://blog.csdn.net/wzzvictory/article/details/10076323 UIView的这几个属性让我困惑了好一阵子,通过翻看官方文档和stackove ...

  4. VS下载地址

    http://www.iplaysoft.com/vs2015.html   Microsoft Visual Studio(简称VS)是美国微软公司的开发工具包系列产品.Visual Studio ...

  5. 基于POI的Excel导入导出(JAVA实现)

    今天做了个excel的导入导出功能,在这记录下. 首先现在相关poi的相关jar包,资源链接:http://download.csdn.net/detail/opening_world/9663247 ...

  6. Left/Right/Inner Join用法和区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  7. go get 代理设置

    前提: 假设安装好git 我的FQ方式(也可以使用别的方式): 使用 ishadowsocks方式FQ 临时设置Windows下代理: 在控制台执行如下命令,后面的的代理值根据你具体的代理进行设置 s ...

  8. Hibernate查询之API查询

    Hibernate在检索数据上,可以使用SQL.HQL和官方API进行查询,本人主要利用API进行相关查询的小demo. 话不多少直接上demo. demo1:基本查询 /** * 默认不加任何条件的 ...

  9. 转 java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException

    转自:http://blog.csdn.net/zb0567/article/details/7893063 java.lang.ClassNotFoundException: org.apache. ...

  10. JAVA语法题

    import java.util.*; public class Birthdays { public static void main(String[] args){ Map<Friends, ...