HDU6301.Distinct Values

这个题就是给你区间要求区间内的数都不相同,然后要求是字典序最小,直接贪心走一遍,但是自己写的时候,思路没有错,初始化写挫了。。。

将区间按左端点小的排序,如果相同就按右端点大的排序,因为右端点大的肯定满足右端点小的。然后直接标记数组记录当前区间已有的数,然后将没有的数字填到里面。注意初始化就可以了。

代码:

 //1004-6301-字典序最小的序列,贪心策略,标记当前段出现过的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cassert>
using namespace std;
typedef long long ll;
const int maxn=1e5+; struct node{
int l,r;
bool operator< (const node &a) const {
if(l==a.l) return r>a.r;
else return l<a.l;
} }a[maxn]; int cnt[maxn],ans[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--){
int n,m;scanf("%d%d",&n,&m);
memset(cnt,,sizeof(cnt));
memset(ans,,sizeof(ans));
for(int i=;i<m;i++)
scanf("%d%d",&a[i].l,&a[i].r);
sort(a,a+m);
for(int i=a[].l;i<=a[].r;i++)
ans[i]=i-a[].l+;
int num,L=a[].l,R=a[].r;
for(int i=;i<m;i++){
if(a[i].r<=R) continue;
num=;
if(a[i].l<=R){
for(int j=a[i].l;j<=R;j++)
cnt[ans[j]]=i;
for(int j=R+;j<=a[i].r;j++){
while(cnt[num]==i)++num;
ans[j]=num++;
}
L=a[i].l,R=a[i].r;
}
else{
for(int j=a[i].l;j<=a[i].r;j++)
ans[j]=num++;
L=a[i].l,R=a[i].r;
}
}
for(int i=;i<n;i++){
if(ans[i])printf("%d ",ans[i]);
else printf("1 ");
}
if(ans[n])printf("%d\n",ans[n]);
else printf("1\n");
}
return ;
}

HDU 6301.Distinct Values-贪心、构造字典序最小的数列 (2018 Multi-University Training Contest 1 1004)的更多相关文章

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

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

  2. hdu 6301 Distinct Values (贪心)

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

  3. HDU 6301 Distinct Values

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6301 多校contest1 题目大意是有一个长度为N的数组,给出M个"事实",每个 ...

  4. hdu 6301 Distinct Values (双指针,水题)

    大意: 给定m个区间, 求构造一个长n且字典序最小的序列, 使得每个区间内的数各不相同 求出每个位置为左端点时向右延伸最大距离, 然后双指针, 每次从set中取最小 #include <iost ...

  5. 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 ...

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

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

  7. BZOJ 1640 [Usaco2007 Nov]Best Cow Line 队列变换:贪心【字典序最小】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1640 题意: 给你一个长度为n的字符串. 你可以将原串的首字母或尾字母移动到新串的末尾. ...

  8. HDU 1385 Minimum Transport Cost (输出字典序最小路径)【最短路】

    <题目链接> 题目大意:给你一张图,有n个点,每个点都有需要缴的税,两个直接相连点之间的道路也有需要花费的费用.现在进行多次询问,给定起点和终点,输出给定起点和终点之间最少花费是多少,并且 ...

  9. 贪心算法——字典序最小问题,Saruman‘s Army

    题目描述 Best Cow Line (POJ 3617) 给定长度为N的字符串S,要构造一个长度为N字符串T.T是一个空串,反复执行下列任意操作: 从S的头部删除一个字符,加到T的尾部: 从S的尾部 ...

随机推荐

  1. linux udp c/s

    一.UDP C/S编程的步骤如下图所示 二.与TCP C/S通信的区别在于:服务端没有设置监听和等待连接的过程.客户端没有连接服务端的过程.基于UDP的通信时不可靠地,面向无连接的,发送的数据无法确切 ...

  2. 项目中自己觉得比较好的Erlang语法

    1.Lists 中处理合并Key相同的Tuple CashInfo1 = [{?PAY_TYPE_YUANBAO, NeedYuanBao + OldNeedYuanbao}|lists:keydel ...

  3. CSS3 3D圆形设计教程

    http://www.htmleaf.com/ziliaoku/qianduanjiaocheng/201502061338.html

  4. 《Cracking the Coding Interview》——第5章:位操作——题目8

    2014-03-19 06:33 题目:用一个byte数组来模拟WxH的屏幕,每个二进制位表示一个像素.请设计一个画水平线的函数. 解法:一个点一个点地画就可以了.如果要优化的话,其实可以把中间整字节 ...

  5. jekens介绍及服务搭建

    https://blog.csdn.net/achuo/article/details/51086599 https://blog.csdn.net/qq_37372007/article/detai ...

  6. 验证表单的js代码段

     JS常用功能 转载自:http://blog.csdn.net/kalision/article/details/12516103 引用js文件: <script src="js/d ...

  7. Tensorflow实现LSTM识别MINIST

    import tensorflow as tf import numpy as np from tensorflow.contrib import rnn from tensorflow.exampl ...

  8. Day4 自定义控件/ListView/RecyclerView

    创建自定义控件 引入布局 在新增的title.xml中创建一个自定义的标题栏: <LinearLayout xmlns:android="http://schemas.android. ...

  9. PHP页面跳转总结

    一.使用php内置函数:header()函数 <?php$url='./test.php'; header("Location:$url"); ?> 注意Locatio ...

  10. Linux学习15_CentOS6.5下netcat工具安装教程

    1.下载 下载地址:http://sourceforge.net/projects/netcat/files/netcat/0.7.1/ 下载的是netcat-0.7.1.tar.gz版本 2.拷贝 ...