HUD-5124-lines
题目链接
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的更多相关文章
- hud 5124 lines(思维 + 离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines Problem Description: John has several lines. ...
- hdoj 5124 lines【线段树+离散化】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:给你n段区间,他们有重合的点A,问你重合最多次的点A重合多少次 题解:对区间离散化后,维护 ...
- hdu 5124 lines
http://acm.hdu.edu.cn/showproblem.php?pid=5124 题意:给你n条线段,然后找出一个被最多条线段覆盖的点,输出覆盖这个点的线段的条数. 思路:可以把一条线段分 ...
- 【扫描线】HDU 5124 lines
http://acm.hdu.edu.cn/showproblem.php?pid=5124 [题意] 在数轴x上,每次操作都覆盖一个区间的所有点,问被覆盖次数最多的点是覆盖了多少次 [思路] 最简单 ...
- (树状数组+离散化)lines--hdu --5124
http://acm.hdu.edu.cn/showproblem.php?pid=5124 lines Time Limit: 5000/2500 MS (Java/Others) Memor ...
- BestCoder20 1002.lines (hdu 5124) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...
- UI相关教程:HUD、UMG和Widget
转自:http://aigo.iteye.com/blog/2258612 蓝图脚本来处理 ================================================== 用UM ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- HUD——1083 Courses
HUD——1083 Courses Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- WEBGL学习【十四】利用HUD技术在网页上方显示三维物体
关键点: <!--实现原理:要保证这两个canvas相互重叠;z-index表示了两个画布的上下层关系--> <!--是WEBGL的三维图形Canvas(主要用于绘制三维场景)--& ...
随机推荐
- git版本控制(一)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- GPU
GPU主要是进行计算机图形这种大运算量的图形处理器,包括顶点设置.光影.像素操作.对CPU发出的数据和指令,进行着色,材质填充,渲染. 在没有GPU的系统中,3D游戏中物体移动时的坐标转换与光源处理, ...
- ds18b20再硬件设计部分的注意事项
1.DS18B20的供电方式: ps:寄生电源不是实际的电源器件,而是一种供电方式,即通过数据线供电. 如下面的两张图片所示,分别为外部供电模式下单只和多只DS18B20测温系统的典型电路连接图. ( ...
- 理解MySQL——索引与优化(转)
转自:http://www.cnblogs.com/hustcat/archive/2009/10/28/1591648.html 写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库 ...
- iOS之Nib和Xib以及storyboard(故事版)
本文转发至:http://blog.csdn.net/tonny_guan/article/details/8542789 nib.xib与故事板 如果大家使用过苹果的官方资料,一定会发现某些资料上会 ...
- 毕业论文评审意见、导师意见范文、模板_Smile~风_百度空间
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- 简单的字符串比较题 POJ 1936
Description You have devised a new encryption technique which encodes a message by inserting between ...
- random 函数
Random()在Delphi中,有一随机函数,是这样定义的:function Random [ ( Range: Integer) ]; 其中,参数Range为一整数,该函数返回值也为整数,其范围为 ...
- collectionviewcell 添加删除按钮 响应区域的问题
在collectionviewcell 的右上角添加了一个删除按钮,但是发现只有cell和删除按钮重合的区域才会响应点击事件 后来doctor 李说这是iOS 事件响应链的机制(http://www. ...
- RoundedImageView使用吐槽心得(RoundedImageView与Glide加载图片,第一次加载无法圆角问题)
最近使用的时候发现一个问题, RoundedImageView与Glide搭配使用的时候,第一次加载图片(内存中没有),后图片无法圆角,后来尝试各种改,最后想到了一个办法,就是让Glide加载图片的 ...