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之CMDB
浅谈ITIL TIL即IT基础架构库(Information Technology Infrastructure Library, ITIL,信息技术基础架构库)由英国政府部门CCTA(Central ...
- leetcode738
public class Solution { public int MonotoneIncreasingDigits(int N) { var num = N.ToString(); var len ...
- How to unlock Sample HR database in oracle
For working with tutorial of oracle Introduaction to oracle/sql you need to work on the tables which ...
- python将url转换成二维码
准备环境 python生成二维码,需要依赖包qrcode和PIL(二维码的实质就是一个url.) python3安装PIL:pip install PIL python2安装PIL:pip insta ...
- 第七章 二叉搜索树(b1)BST:查找
- set 续1
--------siwuxie095 三.用 set 实现计算 语法: SET /A expression /A 命令行开关指定等号右边的字符串为待计算的数字表 ...
- 图片放大镜——jQuery插件Cloud Zoom
下载地址:cloud_zoom.rar 图片放大镜效果是一种不错的效果,多应用于电子商务.图片展示等网站,给用户带来更好的体验.实现这种效果的代码不少,今天要给大家介绍的是 Cloud Zoom,它是 ...
- Django基础学习四_数据库的增删改查
今天主要学习两个东西 1.如何对数据库做增删改查 2.如果将数据库中的数据用html的方式返回到前台 一.对数据库中增删改查操作 1.首先需要先见表,见表的方法我们在“http://www.cnblo ...
- zabbix 2.0 安装
2.0环境 采用Centos6.3_64位操作系统 Zabbix安装 Zabbix 2.0 for RHEL5: # rpm -ivh http://repo.zabbix.com/zabbix/2. ...
- 有关jQuery valid 验证多个同name属性的input的问题
今天遇到需要动态添加多个同name属性的input框,用jQuery valid进行验证时,没有指定不同的id,验证只会在第一个显示提示信息,因为jQuery valid显示的提示信息是根据每个inp ...