CF1262(A,B,D1)

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.

Example

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>
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;
}

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

Example

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 example answer [1,3,4,5,2] is the only possible answer:

q1=p1=1;
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.

分析:

题意:有一个数列p由1~n组成,qi=max(p1,p2,…,pi),给你数列q,问存不存在数列p。
在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<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
int a[100010],res[100010],b[100010];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		queue<int> que;
		memset(b,0,sizeof(b));
		memset(a,0,sizeof(a));
		int n,i,j,p=0;
		cin>>n;
		for(i=1;i<=n;i++)
		{
			cin>>a[i];
			if(a[i]<a[i-1] || a[i]<i)
				p=1;
			b[a[i]]=1;
		}
		if(p)
			cout<<"-1"<<endl;
		else
		{
			for(i=1;i<=n;i++)
				if(!b[i])
					que.push(i);
			for(i=1;i<=n;i++)
			{
				if(a[i]>a[i-1])
					res[i]=a[i];
				else if(a[i]==a[i-1])
					{
						res[i]=que.front();
						que.pop();
					}
			}
			for(i=1;i<=n;i++)
				cout<<res[i]<<" ";
			cout<<endl;
		}
	}
	return 0;
}

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

Examples

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] the optimal subsequences are:

for k=1: [20],
for k=2: [10,20],
for k=3: [10,20,10].

分析:

题意:给一个数列和k,求元素之和是所有长度为k的子数列中的最大的,在满足上一项的所有长度为k的子数列中,取字典顺序最小的一个。求这个数列的第pos个数字是什么。
暴力。定义一个结构体,先按数值从大到小排序,对于数值相等的,按位置从小到大排序。每次输入k的时候,取出前k个,按位置从小到大排序。

代码:

#include<bits/stdc++.h>
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
struct num
{
	int id,w;
}a[1000],b[1000];
int cmp1(num a1,num a2)
{
	if(a1.w==a2.w)
		return a1.id<a2.id;
	else
		return a1.w>a2.w;
}
int cmp2(num a1,num a2)
{
	return a1.id<a2.id;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n,i;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		int x;
		cin>>x;
		a[i].id=i;
		a[i].w=x;
	}
	sort(a+1,a+n+1,cmp1);
	int t,k,pos;
	cin>>t;
	for(i=1;i<=t;i++)
	{
		int j;
		cin>>k>>pos;
		for(j=1;j<=k;j++)
			b[j]=a[j];
		sort(b+1,b+k+1,cmp2);
		cout<<b[pos].w<<endl;
	}
	return 0;
}

记录分数:1299+33=1332

<think>好的,我需要解决用户在Docker中运行Jenkins时遇到的端口8080被占用的问题。首先,我得理解用户的问题。他们在运行Jenkins容器时,可能因为主机上的8080端口已经被其他进程占用,导致容器启动失败。用户需要解决方案来释放该端口或让Jenkins使用其他端口。 首先,我应该考虑如何检查端口占用情况。通常,在Linux系统中可以使用`netstat`或`lsof`命令来查看哪个进程占用了8080端口。例如,运行`sudo lsof -i :8080`或`sudo netstat -tuln | grep 8080`可以找到占用端口的进程ID,然后通过`kill`命令终止该进程。但用户可能不熟悉这些命令,需要详细说明步骤。 接下来,如果用户不想终止占用端口的进程,可以考虑修改Jenkins容器的端口映射。Docker允许在运行容器时通过`-p`参数指定端口映射,比如将主机的8081端口映射到容器的8080端口。这样,Jenkins仍然在容器内使用8080,但外部通过8081访问。需要提醒用户更新书签或相关配置中的端口号。 另外,用户可能不知道如何正确使用Docker命令,所以需要给出具体的示例,比如`docker run -d -p 8081:8080 jenkins/jenkins`。同时,可能需要检查现有的容器是否已经在运行并占用了端口,使用`docker ps`查看,并停止或删除旧容器。 还需要考虑用户是否修改过Jenkins的配置文件,比如在Dockerfile中指定了端口,或者通过环境变量调整端口设置。如果用户自定义了Dockerfile,可能需要修改`EXPOSE`指令或运行时的参数。 此外,用户可能遇到其他服务如Nginx或Apache占用了8080端口的情况,这时可以建议调整这些服务的端口,或者使用Docker的网络配置来避免冲突。例如,使用反向代理时,可能需要修改Nginx的监听端口,并在容器配置中保持一致。 最后,应该提醒用户备份重要数据,避免因操作失误导致数据丢失。同时,提供一些参考文档或社区资源的链接,帮助用户进一步解决问题。 总结起来,解决方案可能包括停止占用端口的进程、修改容器端口映射、检查现有容器、调整服务配置等步骤。需要以清晰的步骤和具体的命令示例来指导用户操作,确保他们能够逐步解决问题。</think>### 解决Docker运行Jenkins时端口8080被占用的问题 #### 1. 确认端口占用情况 首先通过以下命令检查占用8080端口的进程: ```bash sudo lsof -i :8080 # 或 sudo netstat -tuln | grep 8080 ``` 若发现占用进程(如其他Jenkins实例或Nginx),可通过`kill <PID>`终止进程[^3]。 #### 2. 修改Jenkins容器端口映射 通过调整Docker命令的`-p`参数,将主机端口改为未占用的值(例如8081): ```bash docker run -d -p 8081:8080 -v jenkins_home:/var/jenkins_home jenkins/jenkins ``` 此时可通过`http://localhost:8081`访问Jenkins[^2]。 #### 3. 检查并清理现有容器 若已有Jenkins容器在运行,可能导致端口冲突: ```bash docker ps # 查看运行中的容器 docker stop <容器ID> # 停止旧容器 docker rm <容器ID> # 删除旧容器 ``` #### 4. 自定义Dockerfile调整端口(可选) 在自定义Dockerfile中显式声明端口: ```dockerfile FROM jenkins/jenkins EXPOSE 8081 # 声明容器端口 ``` 构建镜像后运行: ```bash docker run -d -p 8081:8081 custom-jenkins-image ``` #### 5. 调整其他服务配置 若Nginx/Apache占用8080端口,可修改其配置文件中的监听端口: ```nginx # Nginx配置示例 server { listen 8081; # 原为listen 8080; ... } ``` 修改后重启服务[^3]。 #### 6. 使用Docker网络隔离 创建独立网络避免端口冲突: ```bash docker network create jenkins-net docker run -d --network jenkins-net -p 8081:8080 jenkins/jenkins ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值