# A math problem

Your math teacher gave you the following problem:
There are n segments on the x-axis, [l1;r1],[l2;r2],…,[ln;rn]. The segment [l;r] includes the bounds, i.e. it is a set of such x that l≤x≤r. The length of the segment [l;r] is equal to r−l.
Two segments [a;b] and [c;d] have a common point (intersect) if there exists x that a≤x≤b and c≤x≤d. For example, [2;5] and [3;10] have a common point, but [5;6] and [1;4] don’t have.
You should add one segment, which has at least one common point with each of the given segments and as short as possible (i.e. has minimal length). The required segment can degenerate to be a point (i.e a segment with length zero). The added segment may or may not be among the given n segments.
In other words, you need to find a segment [a;b], such that [a;b] and every [li;ri] have a common point for each i, and b−a is minimal.
Input
The first line contains integer number t (1≤t≤100) — the number of test cases in the input. Then t test cases follow.
The first line of each test case contains one integer n (1≤n≤105) — the number of segments. The following n lines contain segment descriptions: the i-th of them contains two integers li,ri (1≤li≤ri≤109).
The sum of all values n over all the test cases in the input doesn’t exceed 105.
Output
For each test case, output one integer — the smallest possible length of the segment which has at least one common point with all given segments.

x轴上有n个段，[l1;r1]，[l2; r2]，…，[ln; rn]。段[l; r]包含边界，即它是这样的一组x，即l≤x≤r。段[1；r]的长度等于r-1。

Input
4
3
4 5
5 9
7 7
5
11 19
4 17
16 16
3 12
14 17
1
1 10
1
1 1
Output
2
4
0
0

Note
In the first test case of the example, we can choose the segment [5;7] as the answer. It is the shortest segment that has at least one common point with all given segments.

#include<bits/stdc++.h>
#include <time.h>
#include <iomanip>
#define _CRT_SECURE_NO_WARNINGS
//const double pi=acos(-1.0);
//const int inf=0x3f3f3f3f;
const int maxn=10000001;
using namespace std;
typedef struct{
int l;
int r;
}dn;
dn x[maxn];
bool complare(dn a,dn b){
return a.l>b.l;
}
bool complare1(dn a,dn b){
return a.r<b.r;
}
int main () {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//cout<<setw()<<setfill('0')<<endl;
//cout<<fixed<<setprecision()<<endl;
cin>>n;
int ds,l,r;
cin>>ds;
for(int i=1;i<=ds;i++){
cin>>x[i].l;
cin>>x[i].r;
}
int left,right;
sort(x+1,x+ds+1,complare);
right=x[1].l;
sort(x+1,x+ds+1,complare1);
left=x[1].r;
if(right>left)
cout<<right-left<<endl;
else
cout<<"0"<<endl;
}
}

# Box

Permutation p is a sequence of integers p=[p1,p2,…,pn], consisting of n distinct (unique) positive integers between 1 and n, inclusive. For example, the following sequences are permutations: [3,4,1,2], [1], [1,2]. The following sequences are not permutations: [0], [1,2,1], [2,3], [0,1,2].
The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation p of length n.
You don’t know this permutation, you only know the array q of prefix maximums of this permutation. Formally:
q1=p1,
q2=max(p1,p2),
q3=max(p1,p2,p3),

qn=max(p1,p2,…,pn).
You want to construct any possible suitable permutation (i.e. any such permutation, that calculated q for this permutation is equal to the given array).
Input
The first line contains integer number t (1≤t≤104) — the number of test cases in the input. Then t test cases follow.
The first line of a test case contains one integer n (1≤n≤105) — the number of elements in the secret code permutation p.
The second line of a test case contains n integers q1,q2,…,qn(1≤qi≤n) — elements of the array q for secret permutation. It is guaranteed that qi≤qi+1 for all i (1≤i<n).
The sum of all values n over all the test cases in the input doesn’t exceed 105.
Output
For each test case, print:
If it’s impossible to find such a permutation p, print “-1” (without quotes).
Otherwise, print n distinct integers p1,p2,…,pn (1≤pi≤n). If there are multiple possible answers, you can print any of them.

q1 = p1，
q2 = max（p1，p2），
q3 = max（p1，p2，p3），

qn = max（p1，p2，…，pn）。

Input
4
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1
Output
1 3 4 5 2
-1
2 1
1

Note
In the first test case of the
· q1=p1=1q1=p1=1;
· q2=max(p1,p2)=3q2=max(p1,p2)=3;
· q3=max(p1,p2,p3)=4q3=max(p1,p2,p3)=4;
· q4=max(p1,p2,p3,p4)=5q4=max(p1,p2,p3,p4)=5;
· q5=max(p1,p2,p3,p4,p5)=5q5=max(p1,p2,p3,p4,p5)=5.
It can be proved that there are no answers
for the second test case of the example.

#include<bits/stdc++.h>
#include <time.h>
#include <iomanip>
#define _CRT_SECURE_NO_WARNINGS
//const double pi=acos(-1.0);
//const int inf=0x3f3f3f3f;
using namespace std;
const int maxn=100001;
int q[maxn];
int p[maxn];
int ans[maxn];
int main () {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//cout<<setw()<<setfill('0')<<endl;
//cout<<fixed<<setprecision()<<endl;
int t;
cin>>t;
while(t--){
int n,y=0;
cin>>n;
queue<int> que;
memset(q,0,sizeof(q));
memset(p,0,sizeof(p));
for(int i=1;i<=n;i++){
cin>>q[i];
if(q[i]<q[i-1]||q[i]<i)y=1;
p[q[i]]=1;
}
if(y)cout<<"-1"<<endl;
else{
for(int i=1;i<=n;i++){
if(!p[i])que.push(i);
}
for(int i=1;i<=n;i++){
if(q[i]>q[i-1])ans[i]=q[i];
else if(q[i]==q[i-1]){
ans[i]=que.front();
que.pop();
}
}
for(int i=1;i<=n;i++)cout<<ans[i]<<" ";
cout<<endl;
}
}
exit(0);
}

## Optimal Subsequences (Easy Version)

This is the easier version of the problem. In this version 1≤n,m≤100. You can hack this problem only if you solve and lock both problems.
You are given a sequence of integers a=[a1,a2,…,an] of length n. Its subsequence is obtained by removing zero or more elements from the sequence a (they do not necessarily go consecutively). For example, for the sequence a=[11,20,11,33,11,20,11]:
[11,20,11,33,11,20,11], [11,20,11,33,11,20], [11,11,11,11], [20], [33,20] are subsequences (these are just some of the long list);
[40], [33,33], [33,20,20], [20,20,11,11] are not subsequences.
Suppose that an additional non-negative integer k (1≤k≤n) is given, then the subsequence is called optimal if:
it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k;
and among all subsequences of length k that satisfy the previous item, it is lexicographically minimal.
Recall that the sequence b=[b1,b2,…,bk] is lexicographically smaller than the sequence c=[c1,c2,…,ck] if the first element (from the left) in which they differ less in the sequence b than in c. Formally: there exists t (1≤t≤k) such that b1=c1, b2=c2, …, bt−1=ct−1 and at the same time bt<ct. For example:
[10,20,20] lexicographically less than [10,21,1],
[7,99,99] is lexicographically less than [10,21,1],
[10,21,0] is lexicographically less than [10,21,1].
You are given a sequence of a=[a1,a2,…,an] and m requests, each consisting of two numbers kj and posj (1≤k≤n, 1≤posj≤kj). For each query, print the value that is in the index posj of the optimal subsequence of the given sequence a for k=kj.
For example, if n=4, a=[10,20,30,20], kj=2, then the optimal subsequence is [20,30] — it is the minimum lexicographically among all subsequences of length 2 with the maximum total sum of items. Thus, the answer to the request kj=2, posj=1 is the number 20, and the answer to the request kj=2, posj=2 is the number 30.
Input
The first line contains an integer n (1≤n≤100) — the length of the sequence a.
The second line contains elements of the sequence a: integer numbers a1,a2,…,an (1≤ai≤109).
The third line contains an integer m (1≤m≤100) — the number of requests.
The following m lines contain pairs of integers kj and posj (1≤k≤n, 1≤posj≤kj) — the requests.
Output
Print m integers r1,r2,…,rm (1≤rj≤109) one per line: answers to the requests in the order they appear in the input. The value of rj should be equal to the value contained in the position posj of the optimal subsequence for k=kj.

[11,20,11,33,11,20,11]：[11,20,11,33,11,20,11]，[11,20,11,33,11,20]，[11,11,11,11]，[20]，[33,20]是子序列（这些只是一长串的一部分）；[40]，[33,33]，[33,20,20]，[20,20,11,11]不是子序列。

[10,20,20]从字典上小于[10,21,1]，

[7,99,99]在字典上小于[10,21,1]，

[10,21,0]在字典上小于[10,21,1]。

//字典序就是从左到右一次比较两个序列的大小，长度短的在前，数字大小小的在前，字母ASCLL码小的在前。

Input
3
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3
Output
20
10
20
10
20
10
Input
7
1 2 1 3 1 2 1
9
2 1
2 2
3 1
3 2
3 3
1 1
7 1
7 7
7 4
Output
2
3
2
3
2
3
1
1
3

Note
In the first example, for a=[10,20,10]a=[10,20,10] the optimal subsequences are:
for k=1k=1: [20][20]
for k=2k=2: [10,20][10,20]
for k=3k=3: [10,20,10][10,20,10].

#include<bits/stdc++.h>
#include <time.h>
#include <iomanip>
#define _CRT_SECURE_NO_WARNINGS
//const double pi=acos(-1.0);
//const int inf=0x3f3f3f3f;
using namespace std;
const int maxn=100005;
typedef struct{
int si;
int k;
}arr;
arr a[maxn],r[maxn];
bool complare1 (arr a,arr b){
return a.k>b.k||a.k==b.k&&a.si<b.si;
}
bool complare2 (arr a,arr b){
return a.si<b.si;
}
int q[maxn];
int main () {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//cout<<setw()<<setfill('0')<<endl;
//cout<<fixed<<setprecision()<<endl;
int n;
cin>>n;
for(int i=1;i<=n;i++){
int r;
cin>>r;
a[i].si=i;
a[i].k=r;
}
sort(a+1,a+n+1,complare1);
int t,l,pos;
cin>>t;
for(int i=1;i<=t;i++){
cin>>l>>pos;
for(int j=1;j<=l;j++)
r[j]=a[j];
sort(r+1,r+l+1,complare2);
cout<<r[pos].k<<endl;
}
exit(0);
}


***大学第一篇博客写的不好，还望见谅。***

11-30 428
11-26 77
12-19 103
01-21 650
10-16 489
10-16 268
09-22 179