hdu 5124 离散化+头尾标记+快速读入+STL----map ,set

题目是要判断被覆盖最多次的点的覆盖次数,我们可以将一条直线的首点标记为1,尾点+1标记为-1,那么如果一个点在这条直线中,那么这点的前缀和中就会有这条直线贡献的值1,否则不会有,我们只需要去掉重点离散化后做好标记处理即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#define MAX 100007

using namespace std;

int mark[MAX<<1];
int lef[MAX];
int rig[MAX];

void scan ( int &x )
{
    char c;
    x = 0;
    c = getchar();
    while ( c<'0' || c >'9' ) c = getchar();
    while ( c >='0' && c <= '9' )
    {
        x = x*10 + c - 48;
        c = getchar();
    }
}

int main ( )
{
    int t,n,size;
    scanf ( "%d" , &t );
    while ( t-- )
    {
        scanf ( "%d" , &n );
        memset ( mark , 0 , sizeof ( mark ) );
        map<int,int> mp;
        set<int> s;
        for ( int i = 1 ; i <= n ; i++ )
        {
            scan ( lef[i] );
           // scanf ( "%d" , &lef[i] );
            s.insert ( lef[i] );
            scan ( rig[i] );
           // scanf ( "%d" , & rig[i] );
            s.insert ( rig[i]+1 );
        }
        int cnt = 1;
        set<int>::iterator it = s.begin();
        for ( ; it!=s.end();it++)
            mp[*it] = cnt++;
        for ( int i = 1 ; i <= n ; i++ )
        {
            mark[mp[lef[i]]]++;
            mark[mp[rig[i]+1]]--;
        }
        mark[0] = 0;
        int ans = 0;
        for ( int i = 1 ; i < cnt ; i++ )
            mark[i] = mark[i-1]+mark[i];
        for ( int i = 1 ; i < cnt ; i++ )
            ans = max ( mark[i] , ans );
        printf ( "%d\n" , ans );
    }
}

你可能感兴趣的:(C++,离散化)