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.
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.
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.
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
2
4
0
0
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
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
struct num
{
int l,r;
}a[100010];
int cmp1(num x,num y)
{
return x.r<y.r;
}
int cmp2(num x,num y)
{
return x.l>y.l;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--)
{
int n,i;
cin>>n;
for(i=1;i<=n;i++)
cin>>a[i].l>>a[i].r;
int k1,k2;
sort(a+1,a+n+1,cmp1);
k1=a[1].r;
sort(a+1,a+n+1,cmp2);
k2=a[1].l;
if(k2>k1)
cout<<k2-k1<<endl;
else
cout<<"0"<<endl;
}
return 0;
}
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).
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 The sum of all values n over all the test cases in the input doesn’t exceed 105. For each test case, print: If it’s impossible to find such a permutation p, print “-1” (without quotes). 4 1 3 4 5 2 In the first test case of the example answer [1,3,4,5,2] is the only possible answer: q1=p1=1; 题意:有一个数列p由1~n组成,qi=max(p1,p2,…,pi),给你数列q,问存不存在数列p。 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); it has a length of k and the sum of its elements is the maximum possible among all subsequences of length k; [10,20,20] lexicographically less than [10,21,1], 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. 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. 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. 3 20 7 2 In the first example, for a=[10,20,10] the optimal subsequences are: for k=1: [20], 题意:给一个数列和k,求元素之和是所有长度为k的子数列中的最大的,在满足上一项的所有长度为k的子数列中,取字典顺序最小的一个。求这个数列的第pos个数字是什么。 记录分数:1299+33=1332Output
Otherwise, print n distinct integers p1,p2,…,pn (1≤pi≤n). If there are multiple possible answers, you can print any of them.Example
input
5
1 3 4 5 5
4
1 1 3 4
2
2 2
1
1output
-1
2 1
1Note
q2=max(p1,p2)=3;
q3=max(p1,p2,p3)=4;
q4=max(p1,p2,p3,p4)=5;
q5=max(p1,p2,p3,p4,p5)=5.
It can be proved that there are no answers for the second test case of the example.分析:
在q[i]>=q[i-1],q[i]>=i的情况下,存在p。
在存在的情况下,当q[i]>q[i-1]的时候,p[i]=q[i];当q[i]=q[i-1]的时候p[i]等于一个小于q[i]且之前没有出现的数。如果我们用个数组存这个数是否出现过,每次再遍历寻找的话耗时太大。所以我们可以用队列存没有在q中出现过的数,然后遇到q[i]=q[i-1]的时候出队。#include
D1. Optimal Subsequences (Easy Version)
[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:
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
[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.Input
Output
Examples
input
10 20 10
6
1 1
2 1
2 2
3 1
3 2
3 3output
10
20
10
20
10input
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 4output
3
2
3
2
3
1
1
3Note
for k=2: [10,20],
for k=3: [10,20,10].分析:
暴力。定义一个结构体,先按数值从大到小排序,对于数值相等的,按位置从小到大排序。每次输入k的时候,取出前k个,按位置从小到大排序。代码:
#include