题目链接

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. 小红的难题<递推>

    题意:五个数:N,x,y,A,B;N是台阶总数,x,y是每步可以走x或者y步,但是一定要走到A,B台阶上. 思路:学长给的题解,递推,稍微优化一点. >重点在递推 #include<cst ...

  2. Python 之路:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy

    一.Memcached Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负债.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速 ...

  3. Init.rc分析(刘举奎)

    http://www.360doc.com/content/14/0926/20/13253385_412582822.shtml

  4. JavaScript 中实现继承的方式(列举3种在前一章,我们曾经讲解过创建类的最好方式是用构造函数定义属性,用原型定义方法。)

    第一种:对象冒充 function ClassA(sColor) { this.color = sColor; this.sayColor = function () { alert(this.col ...

  5. Java谜题——类谜题(二)

    1.域的隐藏 代码如下: class Base { public String className = "Base"; } class Derived extends Base { ...

  6. CentOS 7 x64 docker 使用点滴

    CentOS 7 安装 docker yum search docker  查看是否 有docker包 yum info docker  版本为1.3.2 yum -y install docker ...

  7. Nginx 虚拟主机下支持Pathinfo并隐藏入口文件的完整配置

    server { listen 80; server_name zuqiu.com; # 设置你的域名 index index.html index.htm index.php; root D:/wn ...

  8. windows2003 IIS6 部署MVC3和MVC4程序

    1.服务器上安装SP2 和 IIS6 2.安装.Net Framework3.5 SP1(完整安装包,包含2.0 2.0SP1,237MB那个安装包) 3.安装.Net Framework4.0 4. ...

  9. hbuilder 手机app开发系列(一)

    最佳答案好水啊,实在看不过眼,首先apicloud是一个框架,hbuidler是ide工具,两者没什么可比性.我来推荐一个国外免费开源的项目吧,Ionic framework,我之所以推荐它是因为它支 ...

  10. (简单) POJ 3368 Frequent values,RMQ。

    Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...