HDU5124 lines

  离散化 + 树状数组。

  这些东西自己都是刚接触不久的,所以需要多写点题练练手。



  

  

题目描述:

  一维坐标中有N条线段,其中有一个点上面覆盖的线段数是最多的,求该点上面的线段数目。

  这道题和HDU1556特别相似,不过这道题数据比较大,所以要离散化预处理一下数据。

  个人常用的离散化方法:先预存一下数据,然后用数组tmp[]存一下数据,对tmp[]数组排序,然后二分查找原数据在tmp[]数组中的下标,并且把下标作为离散化的数据。

  然后就是树状数组这部分,一维树状数组支持两种操作: 1. 单点更新,区间求和 ; 2 . 区间更新,单点求值。这两种操作的更新和求和这部分是反过来的,前者是对上更新,对下求值,后者是对下更新,对上求值。所以说树状数组比较好实现也容易推广到多维,但是功能不如线段树。

算法:

  用树状数组来存每个点的覆盖次数,覆盖一次即视为该点的数值+1;由于更新的时候是区间更新,所以对[a , b]这个区间覆盖的话,先把[1 , a-1]区间更新-1,然后把[1,b]区间更新+1,所以求最后所有点的最大值即可。

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
using namespace std;
const int maxn = 110000 + 500;
int c[maxn] , n , k;
int L[maxn][2] , tmp[maxn];
int lowbit(int x)
{
    return x & (-x);
}
void update(int x , int num)
{
    while(x > 0) {
        c[x] += num;
        x -= lowbit(x);
    }
}
int getsum(int i)
{
    int res = 0;
    while(i <= k) {
        res += c[i];
        i += lowbit(i);
    }
    return res;
}
int binary_Search(int a[] , int l , int r , int x)
{
    int m = (l + r) >> 1;
    while(l <= r) {
        if(a[m] == x)
            return m;
        if(a[m] < x)
            l = m + 1;
        if(a[m] > x)
            r = m;
        m = (l + r) >> 1;
    }
    return -1;
}
int main()
{
    int T , i , j;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(c , 0 , sizeof(c));
        for(i = 1 , k = 0 ; i <= n ; i++) {
            scanf("%d %d" , &L[i][0] , &L[i][1]);
            tmp[++k] = L[i][0];
            tmp[++k] = L[i][1];
        }
        sort(tmp + 1 , tmp + k +1);
        for(i = 1 ; i <= n ; i++) {
            for(j = 0 ; j <= 1 ; j++) {
                int pos = binary_Search(tmp , 1 , k , L[i][j]);
                L[i][j] = pos;
            }
        }
        for(i = 1 ; i <= n ; i++) {
            update(L[i][0] - 1 , -1);
            update(L[i][1] , 1);
        }
        int max = 1;
        for(i = 1 ; i <= k ; i++) {
            if(getsum(max) < getsum(i))
                max = i;
        }
        printf("%d\n",getsum(max));
    }
    return 0;
}

  

  

时间: 2024-10-27 08:31:18

HDU5124 lines的相关文章

hdu5124——lines

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

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顺次编号的废

lines(最大区间和)

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

【POJ 3668】Game of Lines

Game of Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6555   Accepted: 2439 Description Farmer John has challenged Bessie to the following game: FJ has a board with dots marked at N (2 ≤ N ≤ 200) distinct lattice points. Dot i ha

HDOJ 5031 Lines

枚举角度DFS.... Lines Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 479    Accepted Submission(s): 140 Problem Description You play a game with your friend. He draws several lines on the paper wi

[POJ 1269]Intersecting Lines

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16311   Accepted: 7040 Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three

[Grid Layout] Describe a grid layout using named grid lines

We can use named grid lines to describe our grid layout. Let’s see how to apply this to our grid-template-columns and grid-template-rows, and how to refer to these from our grid items. Syntax like: grid-template-columns: [left-sidebar-start] 1fr [mai

rviz学习笔记(二)——Markers: Points and Lines (C++) 点和线

一.在using_marker/src中编写点和线代码 vim ~/catkin_ws/src/using_marker/src/points_and_lines.cpp 编写代码,其中有注释 #include <ros/ros.h> #include <visualization_msgs/Marker.h> #include <cmath> int main( int argc, char** argv ) { //创建一个发布器 ros::init(argc, a