题目链接

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-5289<two pointers>

    题意: 求一个数列中存在多少个区间,每个区间内的数的差不超过k; 思路:two_pointers; #include<iostream> #include<cstdio> #i ...

  2. CentOS 6.3下NTP服务安装和配置

    测试环境: NTPserver 192.168.1.252 NTPclient 192.168.1.251 准备工作: 关闭selinux: vi /etc/selinux/config SELINU ...

  3. pscs6

    http://wenku.baidu.com/link?url=cO03xdo_GQDdFdeYTDD36ZrjeHarUu4IN-fSEoFAnDXmd5W0yKvzkNWY_vOKKIaKbCdB ...

  4. python赋值和拷贝----一切皆对象,参数皆引用

    摘要: 1 python中的一切事物皆为对象,并且规定参数的传递都是对象的引用. 2  python参数传递都是"传对象引用"方式.实际上相当于c++中传值和传引用的结合. 3 如 ...

  5. @Resource @Autowired 区别

    spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...

  6. phpcms 细节

    Phpcms V9采用if语句判断当前栏目高亮.判断分类信息是否过期 10月05, 2013 by SJY 在用PC V9建站的时候,很多朋友会想到Phpcms V9判定当前栏目,让当前栏目高亮的功能 ...

  7. [算法] kmp实现

    字符串查找是经典场景,也是面试中最常见的一道题. 说来惭愧,毕业3年了,才明白了kmp算法的实现,以前一直以为这类算法是基础,工作中中不会碰到[也的确没有碰到过...] 但是,对这些基本算法结构的理解 ...

  8. JOptionPane的使用

    最近在做swing程序中遇到使用消息提示框的,JOptionPane类其中封装了很多的方法. 很方便的,于是就简单的整理了一下. 1.1 showMessageDialog 显示一个带有OK 按钮的模 ...

  9. 日志文件 统计 网站PV IP

    1. 安装rrdtool yum install rrdtool 2. 创建 rrdtool 数据库 rrdtool create /opt/local/rrdtool/jicki.rrd -s 30 ...

  10. Codeforces#362

    A题 题意:给定一串数列,t,t+s,t+s+1,t+2s,t+2s+1......问某一个数是否是数列当中的 题意:只需判断(x-t)与(x-t-1)能否整除s即可,注意起始时的判断 #includ ...