hdu 6301

Distinct Values

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


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 (li<jr), aiajholds.
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 (1n,m105) -- the length of the array and the number of facts. Each of the next m lines contains two integers li and ri (1lirin).

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
 
Source
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #include <utility>
 7 #include <queue>
 8 #include <set>
 9 using namespace std;
10 typedef long long ll;
11 const int N=1e5+9;
12 int num[N],n,m,pre[N];
13 int t;
14 set<int>s;
15 int main()
16 {
17     scanf("%d",&t);
18     int l,r;
19     //pre[i]: 在i位置放数时必须要考虑到的最左的位置
20     while(t--)
21     {   s.clear();
22         scanf("%d%d",&n,&m);
23         memset(num,0,sizeof(num));
24         for(int i=1;i<=n;i++) 
25         {  pre[i]=i;
26            s.insert(i);
27         }
28         for(int i=0;i<m;i++)
29         {
30             scanf("%d%d",&l,&r);
31             pre[r]=min(pre[r],l);
32         }
33         for(int i=n-1;i>=1;i--){//pre[i]<=pre[i+1]
34             pre[i]=min(pre[i],pre[i+1]);//如:pre[i+1]=1,pre[i]=2,那么pre[i]一定=1
35         }
36         int begin=1;
37         for(int i=1;i<=n;i++){
38             while(begin<pre[i]){//可以和begin重复
39                 s.insert(num[begin]);//插入新的
40                 begin++;//同时位置不断的右移
41             }
42                 num[i]=*s.begin();//最小的(s里面都是满足题意的)
43                 s.erase(num[i]);//避免重复。
44         }
45         for(int i=1;i<=n;i++){
46                printf("%d%c",num[i],i==n?'\n':' ');
47         }
48     }
49     return 0;
50 }
 

猜你喜欢

转载自www.cnblogs.com/tingtin/p/9363505.html