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. Struts完成用户新增操作

    点击新增客户出现该页面并完成前后台交互 代码逻辑分析: jsp 页面部分代码 <TABLE id=table_1 style="DISPLAY: none" cellSpac ...

  2. Java匹马行天下之J2EE框架开发——Spring—>用IDEA开发Spring程序(01)

    一.心动不如行动 一.创建项目 *注:在IDEA中我创建的Maven项目,不了解Maven的朋友可以看我之前的博客“我们一起走进Maven——知己知彼”,了解Maven后可以看我之前的博客“Maven ...

  3. 手动编译PHP开发环境

    目录 手动编译PHP开发环境 问题复盘 部署环境及配置 目标环境 安装部署环境开始 首先安装PHP 安装mysql 安装nginx 手动编译PHP开发环境 这是一篇来自深夜加班的手稿 问题复盘 你有没 ...

  4. 【算法】【排序】【交换类】快速排序QuickSort

    #include<stdio.h> //快速排序 int main(){ ,,,,,,,,}; +; //基准指针 ; //慢指针 int* j=a; //快指针 int QS(int* ...

  5. HTML之必备meta标签

    meta标签写在HTML的<head>中,推荐每个手机H5页面必加以下的代码: <head> <meta charset="UTF-8"> &l ...

  6. 7.18 collection time random os sys 序列化 subprocess 等模块

    collection模块 namedtuple 具名元组(重要) 应用场景1 # 具名元组 # 想表示坐标点x为1 y为2 z为5的坐标 from collections import namedtu ...

  7. Apache性能测试工具ab使用详解~转载

    Apache自带性能测试工具ab使用详解 一. Apache的下载 1. http://www.apache.org/,进入Apache的官网 2. 将页面拖到最下方“Apache Project L ...

  8. hive explode 行拆列

    创建一张表test_explode,表结构如下 表数据如下: 1.使用explode函数 select explode(friends) as friend from test_explode; 但是 ...

  9. (转)2019年给Java编程初学者的建议(附学习大纲)

    本文链接:https://blog.csdn.net/javajlb/article/details/85920904 1. 引言这是一篇初学者干货,请耐心看完,希望对你有帮助 作为初学者的你,命中了 ...

  10. Genymotion 启动app闪退解决方案

    1.之前安装Genymotion后,无法联网下载模拟器 解决方法:下载ova离线包,导入即可 2.启动app,一直处于闪退状态 解决方案: 进入BIOS----->Configuration-- ...