hdu 6301
Distinct Values
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3105 Accepted Submission(s): 1000
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.
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.
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
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <vector> 6 #include <utility> 7 #include <queue> 8 #include <set> 9 using namespace std; 10 typedef long long ll; 11 const int N=1e5+9; 12 int num[N],n,m,pre[N]; 13 int t; 14 set<int>s; 15 int main() 16 { 17 scanf("%d",&t); 18 int l,r; 19 //pre[i]: 在i位置放数时必须要考虑到的最左的位置 20 while(t--) 21 { s.clear(); 22 scanf("%d%d",&n,&m); 23 memset(num,0,sizeof(num)); 24 for(int i=1;i<=n;i++) 25 { pre[i]=i; 26 s.insert(i); 27 } 28 for(int i=0;i<m;i++) 29 { 30 scanf("%d%d",&l,&r); 31 pre[r]=min(pre[r],l); 32 } 33 for(int i=n-1;i>=1;i--){//pre[i]<=pre[i+1] 34 pre[i]=min(pre[i],pre[i+1]);//如:pre[i+1]=1,pre[i]=2,那么pre[i]一定=1 35 } 36 int begin=1; 37 for(int i=1;i<=n;i++){ 38 while(begin<pre[i]){//可以和begin重复 39 s.insert(num[begin]);//插入新的 40 begin++;//同时位置不断的右移 41 } 42 num[i]=*s.begin();//最小的(s里面都是满足题意的) 43 s.erase(num[i]);//避免重复。 44 } 45 for(int i=1;i<=n;i++){ 46 printf("%d%c",num[i],i==n?'\n':' '); 47 } 48 } 49 return 0; 50 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现