hdu-6301-贪心

Posted zzqc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu-6301-贪心相关的知识,希望对你有一定的参考价值。

Distinct Values

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


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
 

   比赛时候想到了用set找,又感觉复杂度爆炸,下来想想的话,每个点最多进出一次所以O(N*log(N))还是ok的。

就是排序区间然后枚举所有区间,一段一段的填充,要维护一个set表示当前区间段内所有能写入的数。还要记录下上一个区间。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct node{
 4     int l,r;
 5 }A[100010];
 6 int a[100010];
 7 bool cmp(node A,node B){
 8     if(A.l!=B.l) return A.l<B.l;
 9     return A.r>B.r;
10 }
11 set<int>S;
12 set<int>::iterator it;
13 int main(){
14     int t,n,m,i,j,k;
15     cin>>t;
16     while(t--){
17         S.clear();
18         memset(a,0,sizeof(a));
19         scanf("%d%d",&n,&m);
20         for(i=1;i<=n;++i) S.insert(i);
21         for(i=1;i<=m;++i) scanf("%d%d",&A[i].l,&A[i].r);
22         sort(A+1,A+1+m,cmp);
23         int l=A[1].l,r=A[1].r;
24         for(i=l,j=1;i<=r;++i,++j) a[i]=j,S.erase(j);
25         for(i=2;i<=m;++i){
26             if(A[i].r<=r) continue;
27             if(A[i].l>r){
28                 for(j=l;j<=r;++j) S.insert(a[j]);
29                 for(j=A[i].l;j<=A[i].r;++j){
30                     a[j]=*S.begin();
31                     S.erase(a[j]);
32                 }
33                 l=A[i].l,r=A[i].r;
34             }
35             else{
36                 for(j=l;j<A[i].l;++j) S.insert(a[j]);
37                 for(j=r+1;j<=A[i].r;++j){
38                     a[j]=*S.begin();
39                     S.erase(a[j]);
40                 }
41                 l=A[i].l,r=A[i].r;
42             }
43         }
44         for(i=1;i<=n;++i) printf("%d%c",a[i]==0?1:a[i],i==n?
: );
45     }
46     return 0;
47 }

 













以上是关于hdu-6301-贪心的主要内容,如果未能解决你的问题,请参考以下文章

HDU 6301 Distinct Values

HDU6301-2018ACM暑假多校联合训练1004-Distinct Values

HDU 6301 Distinct Values(优先队列)

贪心算法:划分字母区间

763. 划分字母区间-贪心算法

贪心热门问题8:划分字母区间