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. JS 中构造函数和普通函数的区别

    原来只是随意的了解了下 , 但是最近有点忘了 于是详细了解下 加深下印象. 1.构造函数也是一个普通函数,创建方式和普通函数一样,但构造函数习惯上首字母大写 2.构造函数和普通函数的区别在于:调用方式 ...

  2. 【游记】NOIP2019初赛

    声明 我的游记是一个完整的体系,如果没有阅读过往届文章,阅读可能会受到障碍. ~~~上一篇游记的传送门~~~ 前言 (编辑中) 文章推荐:[游记]NOIP2019复赛

  3. vue.js中ref及$refs的使用及讲解

    关于ref和$refs的用法及讲解,vue.js中文社区( https://cn.vuejs.org/v2/api/#ref )是这么讲解的: ref 被用来给元素或子组件注册引用信息,引用信息将会注 ...

  4. 洛谷 P4124 [CQOI2016]手机号码

    题意简述 求l~r之间不含前导零,至少有三个相邻的相同数字,不同时含有4和8的11位正整数的个数 题解思路 数位DP,注意在l,r位数不够时补至11位 代码 #include <cstdio&g ...

  5. SBT安装及命令行打包spark程序

    1.从https://www.scala-sbt.org/download.html官网上寻找所需要的安装包 可以直接本地下载完扔进去也可以wget路径,在这里我用的是sbt1.2.8版本的,下载到/ ...

  6. SSH原理讲解与实践

    一.简介 SSH全名Secure Socket Shell,安全外壳传输协议.专为远程登录会话和其他网络服务提供安全性的协议 二.加密算法 要了解SSH的原理,就要先知道目前主流的俩种加密算法 2.1 ...

  7. tomcat9 web manager的配置使用

    本地链接tomcat web manager服务时,只需修改tomcat/conf/tomcat-user.xml文件,如图所示: 远程链接tomcat web manager服务时,需要在tomca ...

  8. 解读BloomFilter算法(转载)

    1.介绍 BloomFilter(布隆过滤器)是一种可以高效地判断元素是否在某个集合中的算法. 在很多日常场景中,都大量存在着布隆过滤器的应用.例如:检查单词是否拼写正确.网络爬虫的URL去重.黑名单 ...

  9. 关于《Selenium3自动化测试实战--基于python语言》

    2016年1月,机缘巧合下我出版了<Selenium2自动化测试实战--基于python语言>这本书,当时写书的原因是,大部分讲Selenium的书并不讲编程语言和单元测试框,如果想在项目 ...

  10. 跟我学SpringCloud | 第十五篇:微服务利剑之APM平台(一)Skywalking

    目录 SpringCloud系列教程 | 第十五篇:微服务利剑之APM平台(一)Skywalking 1. Skywalking概述 2. Skywalking主要功能 3. Skywalking主要 ...