杭电多校第一场D题

闲来无事,看了下牛客多校的题目,嘴炮了几题,顺手用txt写了这题。先附上题面

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
 
解法大概就是将区间按左端点排序,然后用类似尺取的方法,加上小顶堆,一个个的将数填进去
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 const int maxn = 1e5+7;
 7 struct node{
 8     int l,r;
 9 }a[maxn];
10 int vis[maxn];
11 int ans[maxn];
12 priority_queue <int, vector<int>, greater<int> > q;
13 
14 bool cmp(node a,node b){
15     return a.l < b.l;
16 }
17 
18 int main(){
19     int T;
20     scanf("%d",&T);
21     while(T--){
22         memset(vis,0,sizeof(vis));
23         memset(ans,0,sizeof(ans));
24         while(!q.empty()) q.pop();
25         int n,m;
26         scanf("%d%d",&n,&m);
27         for(int i=1;i<=n;++i) q.push(i);
28         for(int i=0;i<m;++i){
29             scanf("%d%d",&a[i].l,&a[i].r);
30         }
31 
32         sort(a,a+m,cmp);
33         int l = 0,r = 0;
34         for(int i=0;i<m;++i){
35             while(l < a[i].l){
36                 if(ans[l] != 0)
37                     q.push(ans[l]);
38                 ++ l;
39             }
40             while(r <= a[i].r){
41                 if(r >= a[i].l){
42                     ans[r] = q.top();
43                     q.pop();
44                 }
45                 ++ r;
46             }
47         }
48         for(int i=1;i<=n;++i){
49             if(ans[i] == 0)
50                 ans[i] = 1;
51         }
52         for(int i=1;i<=n;++i){
53             if(i != 1) printf(" ");
54             printf("%d",ans[i]);
55         }
56         puts("");
57     }
58     return 0;    
59 }

猜你喜欢

转载自www.cnblogs.com/yZiii/p/9361358.html