HDUOJ6301Distinct Values

HDUOJ6301 Distinct Values

Problem Description

Chiaki has an array of n positive integers. You are told some facts about the array: for every two elements ai and aj in the subarray al..r (l≤i

Input

There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains two integers n and m (1≤n,m≤105) – the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1≤li≤ri≤n).

It is guaranteed that neither the sum of all n nor the sum of all m exceeds 106.

Output

For each test case, output n integers denoting the lexicographically minimal array. Integers should be separated by a single space, and no extra spaces are allowed at the end of lines.

Sample Input

3
2 1
1 2
4 2
1 2
3 4
5 2
1 3
2 4

Sample Output

1 2
1 2 1 2
1 2 3 1 1

题意

对于n个数
有m个区间限制
就是在给定的m个区间内数字不能重复
然后输出这n个数
比如第三个样例
5 2
1 3
2 4
在区间1~3, 2~4内数字不能重复
所以结果为1,2,3,1,1

思路

我想的是
用ear数组存一下这些区间,ear[i]存的是从ear[i]到i这个区间不能相同
比如第三个样例ear数组存的是1,2,1,2,5
然后暴力
就TLE了(职业假笑.jpg)
正确的方法应该是按照每段区间的左端点从左到右排序
然后依次从左往右排上取优先队列首存在ans数组里
此区间与上个区间若有重复部分,那么前一个区间的不重复部位可以在此区间使用

超时的代码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;

#define mem(a, b)  memset(a, b, sizeof(a))
#define ll long long
#define mian main
int ear[100010];
int cun[100010];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        mem(cun, 0);
        int n, m;
        int l, r;
        scanf("%d %d", &n, &m);
        for(int i = 1; i <= n; i ++)
        {
            ear[i] = i;
        }

        for(int i = 0; i < m; i ++)
        {
            //cin>>l>>r;
            scanf("%d %d", &l, &r);
            ear[r] = min(ear[r], l);

        }
        int pos = 1;
        for(int i = 1; i <= n; i ++)
        {
            //cout<<"ear"<<ear[i]<<endl;
            if(ear[i] != i)
            {
                //cout<<'i'<<i<<endl;
                for(int j = ear[i]; j <= i; j++)
                {
                    if(!cun[j])
                    {
                        cun[j] = pos++;
                        //cout<<"cun"<<cun[j]<<endl;
                    }

                    //pos++;
                }
                pos = 1;
            }
        }
        for(int i = 1; i <= n; i ++)
        {
            if(!cun[i])
            {
                //cout<<"gai"<<i<<endl;
                cun[i] = 1;
            }
            if(i != n)
            {
                printf("%d ", cun[i]);
            }

        }
        //cout<<"rerevre"<<endl;
        printf("%d\n", cun[n]);
    }
    return 0;
} 

AC代码

#include<cstdio>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define PI         acos(-1)
#define inf        0x3f3f3f3f
#define EPS        1e-6
#define mem(a, b)  memset(a, b, sizeof(a))
//#define ll long long
#define mian main

struct node
{
    int l, r;
} p[100000 + 10];

bool cmp(node a,node b)
{
    if (a.l == b.l)
    {
        return a.r > b.r;
    }
    return a.l < b.l;
}

priority_queue<int, vector<int>, greater<int> > q;
int ans[100000 + 10];

int main()
{
    int t;
    scanf("%d", &t);
    int n, m;
    while (t--)
    {
        scanf("%d %d", &n, &m);
        for (int i = 1; i <= n; i++)
        {
            q.push(i);
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d", &p[i].l, &p[i].r);
        }
        sort(p, p + m, cmp);
        for(int i = 1; i <= n; i++)
        {
            ans[i] = 1;
        }

        int posr = 0, posl = 0;
        int rr = p[0].l - 1, ll = p[0].l;
        for (int i = 0; i < m; i++)
        {
            if(p[i].l > posl && p[i].r > posr)
            {
                posr = p[i].r;
                posl = p[i].l;
                for(int j = ll; j < posl; j++)
                {
                    q.push(ans[j]);
                }
                for(int j = rr + 1; j <= p[i].r; j++)
                {
                    ans[j] = q.top();
                    q.pop();
                }
                rr = posr;
                ll = posl;
            }
        }
        for(int i = 1; i < n; i++)
        {
            printf("%d ", ans[i]);
        }
        printf("%d\n", ans[n]);
        while(!q.empty())
        {
            q.pop();
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/wuswi0412/article/details/81284766
今日推荐