HDU - 6301 - Distinct Values (set/思路)

版权声明:沃斯里德小浩浩啊 https://blog.csdn.net/Healer66/article/details/86553091

题意:

长为n的数组,指定区间内的数字不能重复,求最小字典序的数组

思路:

保存以某下标开始的最长区间,每次更新即可

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int last[maxn];
int ans[maxn];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i = 1; i <=n ;i++)
            last[i] = i;
        int  l,r;
        for(int i = 1; i <=m ; i++)
        {
            cin>>l>>r;
            last[l] = max(last[l],r);//最大边界
        }
        set<int> vis;
        for(int i = 1; i <= n; i++)
            vis.insert(i);
        l = 1; r = 0;//是当前更新的右边界,
        for(int i = 1; i <= n;i++)
        {
            if(r > last[i]) continue;//已经更新,直接忽略
            while(l < i)//每次添加一个元素
            {
                vis.insert(ans[l]);
                l++;
            }
            while( r < last[i])//一直更新至最大边界
            {
                ans[++r] = *vis.begin();
                vis.erase(ans[r]);
            }
        }
          for (int i = 1; i <= n; i++)
			printf("%d%c",ans[i],i==n?'\n':' ');
    }
    return  0;
}

猜你喜欢

转载自blog.csdn.net/Healer66/article/details/86553091