问题 D: Distinct Values

时间限制: 1 Sec  内存限制: 128 MB
提交: 13  解决: 5
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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≠aj holds.
Chiaki would like to find a lexicographically minimal array which meets the facts.

输入

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.

输出

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.

样例输入

3
2 1
1 2
4 2
1 2
3 4
5 2
1 3
2 4

样例输出

1 2
1 2 1 2
1 2 3 1 1

思路:把一个区间的端点中值较大的一个放在前面,向后压倒!  详见代码:

AC代码:

#include <bits/stdc++.h>
using namespace std;
vector<int> ans;
priority_queue<int,vector<int>,greater<int> > store;
int t,n,m,l,r,a[];
int main()
{
scanf("%d",&t);
while(t--)
{
ans.clear();
while(!store.empty()) store.pop();
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
a[i]=i;
store.push(i);
}
while(m--)
{
scanf("%d %d",&l,&r);
a[l]=max(a[l],r);
}
int cnt=,now=;
for(int i=;i<=n;i++)
{
if(a[i]<now) continue;
while(now>a[cnt+] && cnt+<now)
{
store.push(ans[cnt]);
cnt++;
}
while(now<=a[i])
{
ans.push_back(store.top());
store.pop();
now++;
}
}
for(int i=;i<ans.size();i++)
{
if(i==) printf("%d",ans[i]);
else printf(" %d",ans[i]);
}
printf("\n");
}
return ;
}

Distinct Values(贪心)的更多相关文章

  1. 2018 Multi-University Training Contest 1 Distinct Values 【贪心 + set】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6301 Distinct Values Time Limit: 4000/2000 MS (Java/Ot ...

  2. hdu 6301 Distinct Values (贪心)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  3. HDU6301 Distinct Values (多校第一场1004) (贪心)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. 杭电2018暑假多校第一场 D Distinct Values hdu6301 贪心

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  5. hdu多校1004 Distinct Values

    Distinct Values Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): ...

  6. hdu 6301 Distinct Values (2018 Multi-University Training Contest 1 1004)

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. HDU 多校对抗赛 D Distinct Values

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  8. hdu 6301 Distinct Values (思维+set)

    hdu 6301 Distinct Values 题目传送门 题意: 给你m个区间,让你求出一个长度为n的区间且满足在这些区间的数不重复, 并且要求字典序最小 思路: 如果我们已经求出这个序列了,你会 ...

  9. hdu 6301 Distinct Values(贪心)题解

    题意:长为n的串,给你m个区间,这些区间内元素不重复,问这样的串字典序最小为? 思路:用set保存当前能插入的元素,这样就能直接插入最小元素了.对操作按l排序,因为排过的不用排,所以两个指针L,R是一 ...

随机推荐

  1. 【原】spring+springmvc+mybatis整合

    整合框架的代码结构: 最全约束: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  2. C# 写 LeetCode easy #9 Palindrome Number

    9.Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it ...

  3. LeetCode: 383 Ransom Note(easy)

    题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...

  4. .net实现IHttpModule接口顾虑器

    这篇文章主要介绍了C#使用IHttpModule接口修改http输出的方法,涉及C#操作IHttpModule接口的相关技巧,非常具有实用价值,需要的朋友可以参考下   本文实例讲述了C#使用IHtt ...

  5. MS SQL Server的LTRIM,RTRIM和TRIM函数

    在MS SQL Server 2017有了一个新函数TRIM,整合以前版本LTRIM和RTRIM. 这几个函数都是去除字符串头部后尾部的空格. DECLARE @str NVARCHAR(MAX) = ...

  6. Weekly Contest 113

    949. Largest Time for Given Digits (string::compare) Given an array of 4 digits, return the largest ...

  7. <!--[if !IE]> 的用法

    除IE外都可识别

  8. uoj#351. 新年的叶子(概率期望)

    传送门 数学还是太差了,想了半天都没想出来 首先有一个定理,如果直径(这里考虑经过的点数)为奇数,所有直径有同一个中点,如果直径为偶数,所有直径有同一条最中间的边.这个可以用反证法,假设不成立的话直径 ...

  9. 解决Idea项目启动报错:程序包javax.servlet.http不存在

    报错信息 在没有使用maven的时候,web项目从远程仓库获取下以后,起一次启动往往会报错javax.servlet.http程序包找不到,随之而来的java基础包都将不能使用,报错信息如下: 解决方 ...

  10. 查看java版本

    我们可以使用 -version 参数来查看当前 Java 的运行版本,命令如下:java -version 以上代码实例输出结果为: java version “1.6.0_13”Java(TM) S ...