HDU 6301 Distinct Values 【贪心 + 优先队列】

探讨了在给定条件下构造满足区间内元素互不相同的字典序最小数列的算法,采用贪心策略和优先队列实现,确保了字典序最小且满足题目条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2018 Multi-University Training Contest 1 1004

Distinct Values

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

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≠ajholds.
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


题意:需要输出 n 个数的一个数列,题目给出 m 个区间,要求数列满足区间内的数不重复,并且要求整个数列的字典序最小

比如第三个样例: 5 2   [1,3] [2,4]

首先安排 1-3 的区间,字典序最小且不重复,当然是 1 2 3了

然后安排 2-4 的区间,2 3 号位已经放了 2 3 了,第四个位置只要不为 2 和 3 即可,那么最放 1,最后没限定,可以放 1

所以答案为 1 2 3 1 1

这道题其实是个贪心的策略,因为如果一个区间被另一个区间包含,那么这个被包含的区间就完全不需要被考虑,为了字典序最小,所以我们应该先处理区间考前的,即起点考前,终点靠后的优先,被包括的忽略,相交的做标记,然后从标记点往后处理才不会TLE,重点是前一个区间与后一个区间不重合的部分,那部分的数字需要回收重复利用才能使字典序最小,我们可以用一个队列存储可以使用的数字,但是每次都把数字排序的话时间太长了,所以使用优先队列,一个小根堆解决问题。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define mem(a,x) memset(a,x,sizeof(a))

priority_queue<int, vector<int>, greater<int> > q;

struct node{
    int l,r;
}a[100005];

bool cmp(node a,node b){
    if(a.l == b.l){
        return a.r < b.r;
    }
    return a.l < b.l;
}

int ans[100005];

int main(){
    int T,n,m;
    scanf("%d",&T);
    while(T--){
        while(!q.empty()){
            q.pop();
        }
        mem(ans,-1);
        scanf("%d %d",&n,&m);
        for(int i = 1;i <= n;i++){
            q.push(i);
        }
        for(int i = 1;i <= m;i++){
            scanf("%d %d",&a[i].l,&a[i].r);
        }
        sort(a + 1,a + 1 + m,cmp);
        int ll = 1,rr = 1;
        for(int i = 1;i <= m;i++){
            if(a[i].l != a[i + 1].l || i == m){
                for(int j = ll;j < a[i].l;j++){
                    if(ans[j] == -1){
                        ans[j] = 1;
                    }else{
                        q.push(ans[j]);
                    }
                }
                for(int j = rr;j <= a[i].r;j++){
                    if(ans[j] == -1){
                        ans[j] = q.top();
                        q.pop();
                    }
                }
                ll = a[i].l;
                rr = max(rr,a[i].r);
            }
        }
        for (int i = 1; i <= n; i++) {
            if (ans[i] == -1) {
                ans[i] = 1;
            }
        }
        for(int i = 1;i <= n;i++){
            if(i == 1){
                if(ans[i] == -1){
                    printf("1");
                }else{
                    printf("%d",ans[i]);
                }
            }else{
                if(ans[i] == -1){
                    printf(" 1");
                }else{
                    printf(" %d",ans[i]);
                }
            }
        }
        printf("\n");
    }
    return 0;
}

 

对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值