码迷,mamicode.com
首页 > 其他好文 > 详细

HDU多校(Distinct Values)

时间:2018-07-23 19:59:30      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:contains   hdu   mini   ber   case   begin   multiple   out   分享图片   

 

Problem Description
Chiaki has an array of n技术分享图片 positive integers. You are told some facts about the array: for every two elements a技术分享图片i技术分享图片技术分享图片 and a技术分享图片j技术分享图片技术分享图片 in the subarray a技术分享图片l..r技术分享图片技术分享图片 (li<jr技术分享图片 ), a技术分享图片i技术分享图片a技术分享图片j技术分享图片技术分享图片 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技术分享图片 (1n,m10技术分享图片5技术分享图片技术分享图片 ) -- the length of the array and the number of facts. Each of the next m技术分享图片 lines contains two integers l技术分享图片i技术分享图片技术分享图片 and r技术分享图片i技术分享图片技术分享图片 (1l技术分享图片i技术分享图片r技术分享图片i技术分享图片n技术分享图片 ).

It is guaranteed that neither the sum of all n技术分享图片 nor the sum of all m技术分享图片 exceeds 10技术分享图片6技术分享图片技术分享图片 .
 

 

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
 
这回的航电多校有多个签到题 好评 
 
  这题就用L,R 两个指针维护这个ans序列
 

while(L < qu[i].l) {
     st.insert(ans[L]);
     L++;
}

这些值可以重新插入

while(R < qu[i].r) {
if (R < qu[i].l-1) R++;
else {
       R++;
      ans[R] = *st.begin();
      st.erase(st.begin());
      }
}

这个其实类似于莫队的区间维护

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e6 + 10;
 4 int t, n, m, ans[maxn];
 5 struct node {
 6     int l, r, flag;
 7 } qu[maxn];
 8 int cmp(node a, node b) {
 9     if (a.l == b.l) return a.r < b.r;
10     return a.l < b.l;
11 }
12 int main() {
13     scanf("%d", &t);
14     while(t--) {
15         scanf("%d%d", &n, &m);
16         for (int i = 0 ; i < m ; i++) {
17             scanf("%d%d", &qu[i].l, &qu[i].r);
18             qu[i].flag = 0;
19         }
20         sort(qu, qu + m, cmp);
21         set<int>st;
22         for (int i = 1 ; i <= n ; i++) {
23             st.insert(i);
24             ans[i] = 1;
25         }
26         for (int i = qu[0].l ; i <= qu[0].r ; i++) {
27             ans[i] = *st.begin();
28             st.erase(st.begin());
29         }
30         int L = qu[0].l, R = qu[0].r;
31         for (int i = 1 ; i < m ; i++) {
32             while(L < qu[i].l) {
33                 st.insert(ans[L]);
34                 L++;
35             }
36             while(R < qu[i].r) {
37                 if (R < qu[i].l-1) R++;
38                 else {
39                     R++;
40                     ans[R] = *st.begin();
41                     st.erase(st.begin());
42                 }
43             }
44         }
45         for (int i = 1 ; i <= n ; i++) {
46             if (i != n)  printf("%d ", ans[i]);
47             else printf("%d\n", ans[i]);
48         }
49     }
50     return 0;
51 }

 

HDU多校(Distinct Values)

标签:contains   hdu   mini   ber   case   begin   multiple   out   分享图片   

原文地址:https://www.cnblogs.com/qldabiaoge/p/9356318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!