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. [系列] Go - chan 通道

    目录 概述 声明 chan 写入 chan 读取 chan 关闭 chan 示例 推荐阅读 概述 原来分享基础语法的时候,还未分享过 chan 通道,这次把它补上. chan 可以理解为队列,遵循先进 ...

  2. mysql是如何实现事务隔离以及MVCC详解

    提到事务,你肯定会想到ACID(Atomicity.Consistency.Isolation.Durability,即原子性.一致性.隔离性.持久性),我们就来说说其中I,也就是"隔离性& ...

  3. 前端本地proxy跨域代理配置

    等了好久的接口,总算拿到了,结果却发现用本地localhost:9712去请求接口的时候,出现了跨域错误,而这个时候我们就需要进行下跨域配置了. 首先,找到项目中名为webpack.config.js ...

  4. Java统计代码行数

    package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...

  5. js 共有和私有

    //共有 var SunHang = function(){ var name = "ssss"; this.name = "hhhhh"; function ...

  6. Scala函数式编程(三)

    Scala既是一门面向对象(OOP)语言,又是一门函数式编程(FP)语言.作为一门支持函数式编程的语言,Scala鼓励面向表达式编程(EOP)模型.简单来说,EOP中每个语句都有返回值.这一模式很明显 ...

  7. 减谈迷宫C++

    今天老师让做了个迷宫问题,我一看到就发现和我之前写过的一个程序是一样 的,但是在后来编写的时候有一个地方搞错了,最后下课了我还是没有正确的编写好,然后今天回来之后自己有看了一下,现在已经解决了. #i ...

  8. DC-2靶机

    DC-2 靶机获取:http://www.five86.com/ 靶机IP:192.168.43.197(arp-scan l) 攻击机器IP:192.168.43.199 在hosts文件里添加:1 ...

  9. Java内存映射,上G大文件轻松处理

    内存映射文件(Memory-mapped File),指的是将一段虚拟内存逐字节映射于一个文件,使得应用程序处理文件如同访问主内存(但在真正使用到这些数据前却不会消耗物理内存,也不会有读写磁盘的操作) ...

  10. cs231n---CNN架构

    1 LeNet-5 (1998) 第一个被提出的卷积网络架构,深度较浅,用于手写数字识别. 2 AlexNet (2012) 架构为: CONV1 ->MAX POOL1 ->NORM1 ...