任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301

Distinct Values

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

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
 
Source
 

题意概括:

需要求一个长度为 N 的序列,要求满足给出的 M 的子区间内不能出现重复的数,要求字典序最小。

解题思路:

一开始直接双指针模拟,但是果断WA,原因就是当前子区间不能的数不一定是连续的,需要记录前面不能使用的数。

后来用了一个标记数组记录所有不能用的数,TL,想想也知道,查询和维护需要时间复杂度太高。

那有什么可以快速维护这些当前可以使用的数,并且把已经使用过的数删除掉的呢?

C++ STL 里 的 set (可以说功能十分强大了,自动升序排序,可查询,删除)

那么如果知道了当前区间不能时候用什么数,能使用什么数,剩下的就是贪心策略了,尽可能地把需要满足条件子区间扩大(大的子区间满足了它所包含的小的子区间肯定也是满足的)。

用一个数组记录当前序列位置 x 所在的区间的最左值即可。

根据这个最左值 pre[x] 就可以判断当前以求的答案序列里 < pre[x] 的数都是可以用来填充当前子区间的。

AC code:

 #include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<set>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 1e5+;
int num[MAXN];
int pre[MAXN];
int ans[MAXN];
set<int>ss;
int N, M;
int main()
{
int l, r;
int T_case;
scanf("%d", &T_case);
while(T_case--){
ss.clear();
scanf("%d %d", &N, &M); //初始化
for(int i = ; i <= N; i++){pre[i] = i; ss.insert(i);}
for(int i = ; i <= M; i++){scanf("%d %d", &l, &r); pre[r] = min(pre[r], l);}
for(int i = N-; i >= ; i--) pre[i] = min(pre[i], pre[i+]);
int p = ;
for(int i = ; i <= N; i++){
while(p < pre[i]){ss.insert(ans[p]);p++;}
ans[i] = *ss.begin();
ss.erase(ans[i]);
} for(int i = ; i <= N; i++){printf("%d", ans[i]);if(i < N) printf(" ");}
puts("");
}
return ;
}

2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】的更多相关文章

  1. 2018 Multi-University Training Contest 1 Distinct Values(set)

    题意: t组数据,每组数据给定n,m, 表示有m个约束,每个约束包含 x,y ,代表区间 [x, y] 里的数字不能相同. 让你用所有的正整数构成一个长度为 n 的区间,使得这个区间元素顺序的字典序最 ...

  2. Distinct Values(贪心)

    问题 D: Distinct Values 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 5[提交] [状态] [讨论版] [命题人:admin] 题目描述 Chiaki ...

  3. 2018 Nowcoder Multi-University Training Contest 2

    目录 Contest Info Solutions A. run D. monrey G. transform H. travel I. car J. farm Contest Info Practi ...

  4. 2018 杭电多校1 - Distinct Values

    题目链接 Problem Description Chiaki has an array of n positive integers. You are told some facts about t ...

  5. 2018 Nowcoder Multi-University Training Contest 1

    Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...

  6. 2018 Nowcoder Multi-University Training Contest 5

    Practice Link A. gpa 题意: 有\(n\)门课程,每门课程的学分为\(s_i\),绩点为\(c_i\),要求最多删除\(k\)门课程,使得gpa最高. gpa计算方式如下: \[ ...

  7. 2018 Nowcoder Multi-University Training Contest 10

    Practice Link J. Rikka with Nickname 题意: 给出\(n\)个字符串,要求依次合并两个串\(s, t\),满足将\(t\)合并到\(s\)中变成\(r\),使得\( ...

  8. 2019 Multi-University Training Contest 1 - 1009 - String - 贪心

    不知道错在哪里. 是要把atop改成stop!两个弄混了.感谢自造样例. #include<bits/stdc++.h> using namespace std; typedef long ...

  9. 2015 Multi-University Training Contest 6 hdu 5360 Hiking

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

随机推荐

  1. 第五章 使用java实现面向对象异常

    第五章 异常 一.异常概述 概述:异常是在程序的运行过程中所发生的不正常的事件,他会中断正在运行的程序 二.异常处理 1.关键字:try catch finally throw throws 2.Tr ...

  2. 限流(四)nginx接入层限流

    一.nginx限流模块 接入层指的是请求流量的入口,我们可以在这里做很多控制,比如:负载均衡,缓存,限流等. nginx中针对限流有两个模块可以处理: 1)ngx_http_limit_req_mod ...

  3. javaEE Design Patter(2)详解3个设计模式

    Factory (bean creating) Proxy(Agent)(Aop)书有~ Templete(springMVC Facelets)//此处及以上忽略 工厂模式(Factory) 工厂模 ...

  4. 关于使用flying-saucer-pdf,实现xhtml2pdf

    @author Guoguo 2013.11.24 关于flying-saucer-pdf 是一个XML/CSS渲染器,flying-saucer-pdf工具以XML标准文件作为输入,CSS进行排版. ...

  5. VS2013 C++ 动态链接库的生成

    原文:http://www.cnblogs.com/djiankuo/p/5092025.html 这个东西搞了好几天,现在终于没有问题了,其实现在想来还是微软做的东西好用啊,在这里点个赞!!! LL ...

  6. 安卓基础干货(六):安卓Activity的学习

    AndroidManifest文件中的几个细节 结论: 1.一个应用程序可以创建多个桌面图标 2.创建快捷图标的方法: <intent-filter> //应用程序的入口 <acti ...

  7. Python爬虫教程-33-scrapy shell 的使用

    本篇详细介绍 scrapy shell 的使用,也介绍了使用 xpath 进行精确查找 Python爬虫教程-33-scrapy shell 的使用 scrapy shell 的使用 条件:我们需要先 ...

  8. Mac系统丢失管理员

    今天把osx变成平行用户,提示说要重启,但是一进来,我靠,自己的管理员用户变成了普通用户.我靠,有这么搞的嘛? 不能安装软件,不能https,不能保存密码,什么也不能做了. 网上一查,找到了几个解决方 ...

  9. libgdx for eclipse开发环境搭建

    1.安装jdk1.7以上 2.下载libgdx1.2.0 下载地址:https://libgdx.badlogicgames.com/releases 3.下载项目创建工具(老版本的) 下载地址:ht ...

  10. “云中论道”之——使用开源技术和Azure公有云服务快速搭建云端IoT解决方案(上)

    “云中论道”技术课堂第一课开讲啦!微软各路技术咖们齐聚一堂,为大家带来干货不断!作为“云中论道“课堂的开课之作,我们首先邀请到了微软Azure专家级的架构师:槐长清,他为我们带来了关于“使用开源技术和 ...