5124 lines

· 题意: 给n条线段,求某点上最多覆盖多少条线段。

·hdu上有题解:

1002 lines
我们可以将一条线段[xi,yi]分为两个端点xi和(yi)+1,在xi时该点会新加入一条线段,同样的,在(yi)+1时该点会减少一条线段,因此对于2n个端点进行排序,令xi为价值1,yi为价值-1,问题转化成了最大区间和,因为1一定在-1之前,因此问题变成最大前缀和,我们寻找最大值就是答案,另外的,这题可以用离散化后线段树来做。复杂度为排序的复杂度即nlgn,另外如果用第一种做法数组应是2n,而不是n,由于各种非确定性因素我在小数据就已经设了n=10W的点。

·排序一开始写成了sort(f,f+n,cmd);  其中n忘了乘2,WA了一发。。。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <string.h>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stack>
 9 #include <stdlib.h>
10 #include <map>
11 using namespace std;
12 #define LL long long
13 #define sf(a) scanf("%d",&(a));
14 #define N 500010
15
16 struct LNode{
17     int x,flag;
18 }f[N];
19 int cmd(struct LNode x,struct LNode y){
20     if(x.x==y.x) return x.flag>y.flag;
21     return x.x<y.x;
22 }
23 int main()
24 {
25     int n,t;
26     scanf("%d",&t);
27     while(t--){
28         //memset(f,0,sizeof(f));
29         scanf("%d",&n);
30         for(int i=0;i<2*n;i=i+2) {
31             int x,y;
32             scanf("%d %d",&x,&y);
33             f[i].x=x;f[i].flag=1;
34             f[i+1].x=y+1;f[i+1].flag=2;
35         }
36         sort(f,f+2*n,cmd);
37         int maxc=-1;int num=0;
38         for(int i=0;i<n*2;i++){
39             if(f[i].flag==1) num++;
40             else num--;
41             if(num > maxc) maxc = num;
42         }
43         printf("%d\n",maxc);
44     }
45
46     return 0;
47 }
时间: 2024-10-14 20:08:42

5124 lines的相关文章

【扫描线】HDU 5124 lines

http://acm.hdu.edu.cn/showproblem.php?pid=5124 [题意] 在数轴x上,每次操作都覆盖一个区间的所有点,问被覆盖次数最多的点是覆盖了多少次 [思路] 最简单的扫描线,左右端点分别排序,遇到左端点sum++更新最值,遇到右端点sun--更新最值 [Accepted] 1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[100010],b[100010]; 4 int t,n; 5 bool

HDU 5124 lines(BestCoder Round #20)

Problem Description: John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input: The first line contains a single integer T(1≤T≤100)(the data for 

hdu 5124 lines (线段树+离散化)

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 620    Accepted Submission(s): 288 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which

HDU 5124 lines

lines Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 515    Accepted Submission(s): 241 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which i

hdu 5124 lines(Bestcoder Round #20)

lines                                                                Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 309    Accepted Submission(s): 145 Problem Description John has several line

[思路题] hdu 5124 lines

题意: 给n个区间 [x,y],问被最多覆盖的点,被覆盖了多少次. 思路: 一个很巧妙的方法.好像原来有接触过. 就是如果给你[1,3]就used[1]++,used[4]--. 然后从左到又过一遍出现的点 依次累加每次加的时候取最大值. 然后这题需要用到map离散化. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #inclu

BestCoder20 1002.lines (hdu 5124) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段的左端点和右端点.设 A 表示最多线段覆盖的点(当然这个 A 可以有多个啦,但这个无关紧要).现在需要求的是 A 被多少条线段覆盖. 我是不会做啦.......一开始还看不懂题目= = 以下是按照题解做的, 题解中的最大区间和,实际上就是求最大连续子序列的和. 1 #include <iostrea

HDU5124:lines(线段树+离散化)或(离散化思想)

http://acm.hdu.edu.cn/showproblem.php?pid=5124 Problem Description John has several lines. The lines are covered on the X axis. Let A is a point which is covered by the most lines. John wants to know how many lines cover A. Input The first line conta

[BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线

1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1806  Solved: 773[Submit][Status][Discuss] Description Farmer John打算将电话线引到自己的农场,但电信公司并不打算为他提供免费服务.于是,FJ必须为此向电信公司支付一定的费用. FJ的农场周围分布着N(1 <= N <= 1,000)根按1..N顺次编号的废