2018HDU多校训练一 D Distinct Values
hiaki has an array of nn positive integers. You are told some facts about the array: for every two elements aiai and ajaj in the subarray al..ral..r (l≤i<j≤rl≤i<j≤r), ai≠ajai≠aj holds.
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 TT, indicating the number of test cases. For each test case:
The first line contains two integers nn and mm (1≤n,m≤1051≤n,m≤105) -- the length of the array and the number of facts. Each of the next mm lines contains two integers lili and riri (1≤li≤ri≤n1≤li≤ri≤n).
It is guaranteed that neither the sum of all nn nor the sum of all mm exceeds 106106.
Output
For each test case, output nn 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
题意:给你n组数,没每组有L,R两个数,数组a[n],L ~ R区间内的正整数各部相同,让你输出字典序最小的数组;
题解:先将这些组数据按L1<L2 ,R1>R2 的优先级排序,可以用set<int> 维护还可以用的数字有哪些,开始将1~n全部加入到set中,然后从1 erase去第一个node 的r-1个数字,然后,令ll 为node[1].l rr为node[1].r,对于后面的node分3中情况:
1.如果node[i].r <= r,说明这个区间已经更新过了,且满足字典序最小,数字不同
2.如果node[i].l>r,则为一个新的开始,将a[l]~a[r]的数字加入到set里面,就从set.begin()开始 将*set.begin()赋值给a[node[i].l]到a[node[i].r],每赋值一个,set就删除一个,更新l,r的值
3.如果node[i].l<=r,则和上面2类似,就是区间变为 r~node[i].r;
参考代码为:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=0x3f3f3f3f;
const int maxn=1e5+10;
int T,n,m,flag[maxn];
struct Node{
int l,r;
bool operator < (const Node&b) const
{
return l==b.l? r>b.r : l<b.l;
}
} node[maxn];
set<int> s1;
int main()
{
scanf("%d",&T);
while(T--)
{
s1.clear();
memset(flag,0,sizeof flag);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) s1.insert(i);
for(int i=1;i<=m;i++) scanf("%d%d",&node[i].l,&node[i].r);
sort(node+1,node+1+m);
for(int i=node[1].l,j=1;i<=node[1].r;i++,j++) s1.erase(j),flag[i]=j;
int l=node[1].l,r=node[1].r;
for(int i=2;i<=m;i++)
{
if(node[i].r<=r) continue;
if(node[i].l>r)
{
for(int k=l;k<=r;k++) s1.insert(flag[k]);
for(int k=node[i].l;k<=node[i].r;k++) flag[k]=*s1.begin(),s1.erase(flag[k]);
l=node[i].l,r=node[i].r;
}
else
{
for(int k=l;k<node[i].l;k++) s1.insert(flag[k]);
for(int k=r+1;k<=node[i].r;k++) flag[k]=*s1.begin(),s1.erase(flag[k]);
l=node[i].l,r=node[i].r;
}
}
for (int i=1;i<=n;++i) printf("%d%c",flag[i]==0? 1 : flag[i],i==n? '\n':' ');
}
return 0;
}
2018HDU多校训练一 D Distinct Values的更多相关文章
- 杭电2018暑假多校第一场 D Distinct Values hdu6301 贪心
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 2018HDU多校训练-3-Problem G. Interstellar Travel
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6325 Interstellar Tra ...
- 2018HDU多校训练-3-Problem M. Walking Plan
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6331 Walking Plan Problem Description There are n inte ...
- 2018HDU多校训练-3-Problem F. Grab The Tree
Little Q and Little T are playing a game on a tree. There are n vertices on the tree, labeled by 1,2 ...
- 2018HDU多校训练-3-Problem D. Euler Function
链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 Problem Description In number theory, Euler's toti ...
- 2018HDU多校训练一 K - Time Zone
Chiaki often participates in international competitive programming contests. The time zone becomes a ...
- 2018HDU多校训练一 C -Triangle Partition
Chiaki has 3n3n points p1,p2,-,p3np1,p2,-,p3n. It is guaranteed that no three points are collinear. ...
- 2018HDU多校训练一 A - Maximum Multiple
Given an integer nn, Chiaki would like to find three positive integers xx, yy and zzsuch that: n=x+y ...
- hdu多校1004 Distinct Values
Distinct Values Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): ...
随机推荐
- 简单的私有DockerHub搭建
Docker Hub 目前Docker官方维护了一个公共仓库https://hub.docker.com, 其中已经包括100000+个的镜像.大部分需求都可以通过在 Docker hub中直接下载镜 ...
- 直接引用MrAdvice.dll文件不能实现AOP拦截,教你1分钟解决这个问题
直接引用MrAdvice.dll文件不能实现AOP拦截,教你1分钟解决这个问题.近日工作中,要实现一个功能,那就是业务层方法里面实现自动缓存.编写业务的C#开发人员只关注如何将业务代码编写正确就可以了 ...
- Python 面向对象-下篇
面向对象是一种编程方式,此编程方式的实现是基于对 类 和 对象 的使用 类 是一个模板,模板中包装了多个“函数”供使用(可以讲多函数中公用的变量封装到对象中) 对象,根据模板创建的实例(即:对象),实 ...
- LeetCode18. 四数之和
LeetCode18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值 ...
- nyoj 264-国王的魔镜 (string[-1:-int(str_len/2+1):-1])
264-国王的魔镜 内存限制:64MB 时间限制:3000ms 特判: No 通过数:13 提交数:25 难度:1 题目描述: 国王有一个魔镜,可以把任何接触镜面的东西变成原来的两倍——只是,因为是镜 ...
- C语言|博客作业03
这个作业属于哪个课程 C程序语言设计 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-1/homework/8654 我在这个课程的目标是 ...
- 标准库bufio个人详解
本文是我有通俗的语言写的如果有误请指出. 先看bufio官方文档 https://studygolang.com/pkgdoc文档地址 主要分三部分Reader.Writer.Scanner 分别是读 ...
- DNS name
DNS name 指的反向解析的域名.
- 【NHOI2018】找素数
[题目描述] 素数又称质数,是指一个大于 1 的正整数,如果除了 1 和它本身以外,不能再被其它的数整除,例如:2.3.5.97 等都是素数.2 是最小的素数. 现在,给你 n 个数字,请你从中选取一 ...
- 从Netty EventLoop实现上可以学到什么
本文主要讨论Netty NioEventLoop原理及实践,关于Netty NioEventLoop,首先要知道NioEventLoop是什么,为什么它会是Netty核心Reactor处理器,实现原理 ...