Distinct Values

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

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
 
题意:题目要求的是构造一个数组在区间内各点的值不相同,且构造的数组字典序要最小

分析:由题意可以得到,终点相同的区间他们的起点可以简化成几个区间内最小的起点

而要使值都不相同,我们首先要知道的是每个点的起始点,然后看这个点的区间内已经填了哪些值,我们再填除了这些值外最小的点,每个点的起始点就是其所在区间的起始点

求出每个点的起始点后,我们只需要用个值记录下这个区间前面的点出现过的值

在这里我们用set存每出现过的值,首先把所有可能的值加入set(用set的好处是他自己排序,第一个为最小的),然后在第一个区间一个个依次加入,每加入一个我们删去一个

接下来的区间用记录的值与区间的起始点比较,如果小于点的起始点的值,证明前面的点没出现过再加入我们的集合,然后取set的第一个值就行了

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e5+10;
const int mod = 1e9 + 7;
typedef long long ll;
ll pre[maxn], ret[maxn];
int main() {
std::ios::sync_with_stdio(false);
ll T, n, m;
cin >> T;
while( T -- ) {
cin >> n >> m;
for( ll i = 1; i <= n; i ++ ) {
pre[i] = i;
}
for( ll i = 0, le, ri; i < m; i ++ ) {
cin >> le >> ri;
pre[ri] = min( pre[ri], le ); //相同终点的区间起点取小的
}
for( ll i = n-1; i >= 1; i -- ) {
pre[i] = min( pre[i], pre[i+1] );//将处在区间内的点的起点定义为区间的起点
//debug(pre[i]);
}
ll p1 = 1; //代表数字用到哪一个点的,前面填充好,后面通过p1对应到前面的点
set<ll> s;
for( ll i = 1; i <= n; i ++ ) {
s.insert(i);
}
for( ll i = 1; i <= n; i ++ ) {
//debug(i);
while( p1 < pre[i] ) { //p1根据区间不同不断更新,当小于i点的区间起点时p1进行更新
s.insert(ret[p1]);
// debug(ret[p1]);
p1 ++;
}
ret[i] = *s.begin();
debug(ret[i]);
s.erase(ret[i]);
}
for( ll i = 1; i <= n; i ++ ) {
if( i != n ) {
cout << ret[i] << " ";
} else {
cout << ret[i] << endl;
}
}
}
return 0;
}

  

杭电2018暑假多校第一场 D Distinct Values hdu6301 贪心的更多相关文章

  1. Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...

  2. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...

  3. 牛客暑假多校第一场J-Different Integers

    一.题目描述: 链接:https://www.nowcoder.com/acm/contest/139/JGiven a sequence of integers a1, a2, ..., an an ...

  4. 牛客暑假多校第一场 J Different Integers

    题意:给你一个数组, q次询问, 每次询问都会有1个[l, r] 求 区间[1,l] 和 [r, n] 中 数字的种类是多少. 解法1, 莫队暴力: 代码: #include<bits/stdc ...

  5. 2019牛客多校第一场 I Points Division(动态规划+线段树)

    2019牛客多校第一场 I Points Division(动态规划+线段树) 传送门:https://ac.nowcoder.com/acm/contest/881/I 题意: 给你n个点,每个点有 ...

  6. 牛客多校第一场 B Inergratiion

    牛客多校第一场 B Inergratiion 传送门:https://ac.nowcoder.com/acm/contest/881/B 题意: 给你一个 [求值为多少 题解: 根据线代的知识 我们可 ...

  7. HDU6581 Vacation (HDU2019多校第一场1004)

    HDU6581 Vacation (HDU2019多校第一场1004) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6581 题意: 给你n+1辆汽车, ...

  8. 2019年牛客多校第一场B题Integration 数学

    2019年牛客多校第一场B题 Integration 题意 给出一个公式,求值 思路 明显的化简公式题,公式是分母连乘形式,这个时候要想到拆分,那如何拆分母呢,自然是裂项,此时有很多项裂项,我们不妨从 ...

  9. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

随机推荐

  1. Python基础总结之第十一天开始【再深入一下函数,重新认识一下】(新手可相互督促)

    感谢最近大家的关注,希望我的学习笔记对大家有帮助!也感谢各位的评论和推荐,请多多指教. 在重新认识函数之前,我们先看两个函数.一个是我们在前面笔记经常用到的print()  :另一个是input() ...

  2. Bean Validation完结篇:你必须关注的边边角角(约束级联、自定义约束、自定义校验器、国际化失败消息...)

    每篇一句 没有任何技术方案会是一种银弹,任何东西都是有利弊的 相关阅读 [小家Java]深入了解数据校验:Java Bean Validation 2.0(JSR303.JSR349.JSR380)H ...

  3. jplayer

    简介 jplayer是个用JavaScript写的完全免费和开源的媒体库(media library).作为jQuery插件的一员,使用jPlayer可以在你的网页上轻松加入跨平台的音乐和视频 使用方 ...

  4. ubuntu中设置python默认版本

    看/usr/bin中的Python文件,发现该文件是python2.7的链接文件 于是直接删掉这个软链接,然后重新创建python2.6的链接文件: 1 rm /usr/bin/python 2 ln ...

  5. python log 设置

    # -*- coding: utf-8 -*- import loggingfrom logging.handlers import TimedRotatingFileHandler  # 按时间处理 ...

  6. Flink 从0到1学习 —— Flink 中如何管理配置?

    前言 如果你了解 Apache Flink 的话,那么你应该熟悉该如何像 Flink 发送数据或者如何从 Flink 获取数据.但是在某些情况下,我们需要将配置数据发送到 Flink 集群并从中接收一 ...

  7. Elasticsearch实战 | 必要的时候,还得空间换时间!

    1.应用场景 实时数据流通过kafka后,根据业务需求,一部分直接借助kafka-connector入Elasticsearch不同的索引中. 另外一部分,则需要先做聚类.分类处理,将聚合出的分类结果 ...

  8. jQuery发送Ajax请求以及出现的问题

    普通jQuery的Ajax请求代码如下: $.ajax({ type: 'POST', url: "http://xxx/yyy/zzz/sendVerifyCode", data ...

  9. SQLServer数据库处于恢复挂起状态的解决办法

    一.总结 如果数据库处于一个恢复挂起的状态,并且对数据库做脱机和分离的操作,报出数据库文件不可访问的错误,可能是因为数据库的数据文件和日志文件在数据库正常连接的情况下,文件所在的磁盘脱机了,导致数据库 ...

  10. Redis集群环境下的键值空间监听事件实现方案

    一直想记录工作中遇到的问题和解决的方法,奈何没有找到一方乐土,最近经常反思,是否需要记录平时的点滴,后台还是决定下定决心记录一些,以便以后用到的时候找不着,实现这样的一个功能主要也是业务所需要的. 需 ...