1. 程式人生 > >Distinct Values(不同價值)

Distinct Values(不同價值)

題目連結
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 (1≤i≤r,1≤j≤r,j>i), ai≠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 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
題意分析:


給出一個數組長度和資料組數,每組資料有個l和r,al~ar中不能有任何兩個資料是相等的,且都為整數,故應從1開始放入,使用迭代器set或者優先數列都是可以的。
注意:
哇,這題提交的時候真的是費了我不少力氣
首先,杭電是不吃“bit/stdc++.h”這個萬能標頭檔案的,其次,杭電還不吃min和max的小函式(不能快捷使用min和max),所以一直編譯錯誤….還有,題目上說的資料是10的6次方,陣列要開的足夠大才能過….不多說了,看程式碼吧..
程式碼篇:

#include <iostream>
#include <set>
#include <cstdio>
using namespace std; int a[10010],pre[10010]; int main() { int n,m,i,l,r,t; cin >> t; while(t--) { int flag=1; set<int>s; cin >> n >> m; for(i=1; i<=n; i++) { pre[i]=i;///初始化為1.2.3..... s.insert(i);///set裡面也是 } while(m--) { cin >> l >> r; pre[r]=(pre[r]<l)?pre[r]:l;///進入小的 } for(i=n-1; i>=1; i--) pre[i]=(pre[i]<pre[i+1])?pre[i]:pre[i+1];///左界的推移 for(i=1; i<=n; i++) { while(flag < pre[i]) { s.insert(a[flag]); flag++; } a[i]=*s.begin();///賦值 s.erase(a[i]);///擦除 } cout << a[1]; for(i=2; i<=n; i++) { printf(" %d",a[i]); } cout << endl; } return 0; }