hud 5124 lines(思维 + 离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=5124
lines
Each test case begins with an integer N(1≤N≤10^5),indicating the number of lines.
Next N lines contains two integers Xi and Yi(1≤Xi≤Yi≤10^9),describing a line.
3 1 1 1 2 1 3
|
气球编号 0 1 2 3 4 第一次 0 1 -1 0 0 第二次 0 2 -1 -1 0 第三次 0 3 -1 -1 -1 最终 0 3 2 1 0 |
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define max(a, b)(a > b ? a : b)
#define N 100010 int c[N];
int main()
{
int n, a, b, i;
while(scanf("%d", &n), n)
{
memset(c, , sizeof(c));
for(i = ; i <= n ; i++)
{
scanf("%d%d", &a, &b);
c[a]++;
c[b + ] -= ;
}
for(i = ; i <= n ; i++)
c[i + ] += c[i];
printf("%d", c[]);
for(i = ; i <= n ; i++)
printf(" %d", c[i]);
printf("\n");
}
return ;
}
在看这道题
题目大意:给一个线段涂色,n次操作,每次操作从点x涂到y,问最后涂得最多的那个点被涂了几次
与hdu 1556 做法相同,但是Xi 、 Yi(1≤Xi≤Yi≤10^9),数组没法开,所以我们需要想个办法来处理,就是离散化
我们将xi、yi都存在一个数组c里面,然后查找出xi和yi在数组c中出现的位置,用xi、yi各自的位置来代替xi、yi,再
来用上面的方法来写
lower_bound(c, c + 2 * n , a[i]) - c 这个函数返回的值是a[i]在数组c中第一次出现的位置
upper_bound(c, c + 2 * n , a[i]) - c 这个函数返回的值是a[i]在数组c中最后一次出现的位置
代码如下:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ;
typedef long long ll; int a[N], b[N], c[N], used[N]; int cmp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
} int main()
{
int t, n, p, q, x, y;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
memset(used, , sizeof(used));
for(int i = ; i < n ; i++)
{
scanf("%d%d", &p, &q);
a[i] = p;
b[i] = q;
}
for(int i = ; i < n ; i++)
{
c[i] = a[i];
c[i + n] = b[i];
}
qsort(c, * n , sizeof(c[]), cmp);
for(int i = ; i < n ; i++)
{
x = lower_bound(c, c + * n , a[i]) - c;//这个函数用来查找a[i]在c中第一次出现的位置
y = lower_bound(c, c + * n , b[i]) - c;
used[x]++;
used[y + ]--;
}
for(int i = ; i < * n ; i++)
used[i+] += used[i];
qsort(used, * n, sizeof(used[]), cmp);
printf("%d\n", used[ * n - ]);
}
return ;
}
hud 5124 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条线段,然后找出一个被最多条线段覆盖的点,输出覆盖这个点的线段的条数. 思路:可以把一条线段分 ...
- Anton and Lines(思维)
Anton and Lines time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 【扫描线】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 ...
- HDU5124:lines(线段树+离散化)或(离散化思想)
http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines ...
- hdu 5124(区间更新+单点求值+离散化)
lines Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 5792 树状数组+离散化+思维
题目大意: Given a sequence A with length n,count how many quadruple (a,b,c,d) satisfies: a≠b≠c≠d,1≤a< ...
- BestCoder20 1002.lines (hdu 5124) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...
随机推荐
- 全面解析python类的绑定方法与非绑定方法
类中的方法有两类: 绑定方法 非绑定方法 一.绑定方法 1.对象的绑定方法 首先我们明确一个知识点,凡是类中的方法或函数,默认情况下都是绑定给对象使用的.下面,我们通过实例,来慢慢解析绑定方法的应用. ...
- mybatis一对多关联查询+pagehelper->分页错误
mybatis一对多关联查询+pagehelper->分页错误. 现象: 网上其他人遇到的类似问题:https://segmentfault.com/q/1010000009692585 解决: ...
- urllib2异常处理(七)
urllib2 的异常错误处理 在我们用urlopen或opener.open方法发出一个请求时,如果urlopen或opener.open不能处理这个response,就产生错误. 这里主要说的是U ...
- JDK8时间格式转换
来源:https://blog.csdn.net/zhangzijiejiayou/article/details/76597329 LocalDateTime 本地日期时间 LocalDateTim ...
- InputStream转化为String
参考:https://blog.csdn.net/lmy86263/article/details/60479350 eg: InputStream in = PropertiesUtils.cla ...
- <form> 表单提交 return 阻止内容为空事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Windows安装MySQL教程
一.下载MySQL MySQL官网首页 --> Download --> Community --> 选择“ MySQL Community Server” 即:MySQL下载连接 ...
- mybatis使用foreach进行批量插入和删除操作
一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Pa ...
- FILTER:progid:DXImageTransform.Microsoft.Gradient使用
FILTER:progid:DXImageTransform.Microsoft.Gradient使用 语法: filter:progid:DXImageTransform.Microsoft.Gra ...
- jquery中的属性和样式设置
添加属性 $target.attr({"title":"one piece","name":"solgan"}); 为目 ...