题目链接

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. Kali-linux安装之后的简单设置(转)

    1.更新软件源: 先备份软件源文件 cp /etc/apt/sources.list /etc/apt/sources.list.bak 修改sources.list文件: leafpad /etc/ ...

  2. manecher

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> us ...

  3. git版本控制(一)

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Android网络开发之Volley--Volley自定义Request

    1.自定义一个解析Json的Request,这里使用JackSon框架来解析Json.你也可以自定义一个解析XML的Request,或者使用FastSon来解析Json. 2.我们首先来看一下Stri ...

  5. grep过滤搜索

    cat /proc/2666/maps | busybox grep libumcpart.so

  6. docker-compose 所带来的方便

    docker-compose 是一款开源的docker 简化复杂容器环境的管理工具 . docker-compose 在结合Swarm 与 docker 进程化容器部署可以很方便的部署一套环境. 具体 ...

  7. Java模拟post提交表单数据

    package test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOExcep ...

  8. STL优先队列的使用

    STL中有一个优先队列的容器可以使用. [头文件] queue 队列容器 vector 向量容器 [操作] 优先级队列支持的操作 q.empty()         如果队列为空,则返回true,否则 ...

  9. html5 WebSocket 与 PHP socket 聊天室原理

    html js <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  10. tls session resumption

    http://stackoverflow.com/questions/12318325/resume-tls-connection-in-java As long as you use the sam ...