2018多校 1004 Distinct Values【补题】

Distinct Values

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3285    Accepted Submission(s): 1064

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<j≤r), ai≠aj holds.
Chiaki would like to find a lexicographically minimal array which meets the facts.

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

 

 

开始之前,先说一句,杜教牛逼!

好了,补题~

题目意思是,让你构造一段字符串,满足以下规则:

1,构造出的数字尽量小,比如第一组样例答案是数字12,只能使用1 ~ 9;

2,题目给出一些区间,要求这个区间内的数字不能重复

 

思路是 ; 

第一步,记录区间,并且处理掉无效的区间,正常都是想到用结构体数组或者pair来装,但是杜神用了一个方法,一个数组,下标作为区间的右边界,值记录左边界,然后取最小值,这样,就解决了区间覆盖的问题,数组要初始化为pre[i] = i。

第二部,开始按照贪心策略去填数字,代码中的pl变量很重要,记录的是上一次回收到的位置,例如2 4区间

然后杜神还用了set来取当前可用的最小值,和优先队列使用方法一样,但是好像简单一点。

O(n)扫过去答案就出来了。

【rep什么的真好用】

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())

const int N = 100100;

int main (){
    int T;
    scanf("%d",&T);
    int m,n;
    int pre[N];
    int ret[N];
    int l,r;
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i = 0;i <= n;i++) pre[i] = i;
        for(int i = 0;i < m;i++){
            scanf("%d %d",&l,&r);
            pre[r] = min(pre[r],l);
        }
//        cout << endl;
//        rep(i,1,n + 1) cout << i << " " << pre[i] << endl;
        per(i,1,n) pre[i] = min (pre[i],pre[i + 1]);
//        cout  << endl;
//        rep(i,1,n + 1) cout << i << " " << pre[i] << endl;
        
        int pl = 1;
        set<int> val;
        rep(i,1,n + 1) val.insert(i); // 填充set 
        rep(i,1,n + 1){
            while(pl < pre[i]){ // pl为上一次回收到的位置 
//                cout << "pl : " << pl << " ret[pl] : " << ret[pl] << endl;
                val.insert(ret[pl]);
                pl++;
            } // 回收可用数字 
            ret[i] = *val.begin(); // 当前位置永远从set中提取最小的答案 
            val.erase(ret[i]);
        }
        rep(i,1,n+1) printf("%d%c",ret[i]," \n"[i==n]);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值