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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用python写一个函数读取二进制文件,二进制文件第一行是版本信息 等号左边是描述,右边是值 后面的头信息以{开始,最后一个}结束 注意头信息里面也有{} 头信息里面的参数都是等号左边是描述,右边是值,值的最后以;结束 头信息的参数可多可少 下面是文件头信息示例 vbf_version = 2.6; header { // Created by VbfSign build: 2017/12/12 on 2020/10/19 9:54 sw_part_number = "8891903679"; sw_version = "A"; sw_part_type = SBL; data_format_identifier = 0x10; ecu_address = 0x1A31; file_checksum = 0x0D81F89D; call = 0x1FFF8004; verification_block_start = 0x20000800; verification_block_length = 0x00000054; verification_block_root_hash = 0x3C440B332BE20F05B9C108F347658FBEBEAA4B3E0D655CAEE76AE210E93E828A; sw_signature_dev = 0x8A97CF400B82AB1B0D82C26976CB26F7AE6B34A87E56675D39E52B9C811F1AB9C040B52FA925BB60E981F388A748F0E09B20D2CBEF7A9A49119EC68C1EFE83BD0EA34B0FDE59F16B8533FEE72219D698C898F4D9EB40B9FCE42C73C028519D1767C11AEEBF0FE746B2F8AB601DA7E3686E4B46AC05851FCDD7A55CBF5F72DA826B0152640514F87CE905362CBFA7F19FE04CAA84A2882E20592E2000D576F629A1B05A207FB59DDC6E8DD10154F148AACA7CBBD5E978BF9E67EDC1ADD186E098A81FEF3C657D2AEFC083F25D307DE00715AB8B2C78CDB23CD13B3DAD9110333ADAB672F1EC6B6D5CFCAF04FC71490A54ECE1745E46D4AB3A9D1E0BA0CACE0B92; sw_signature = 0x12FBA8C1BCB608E5ADC31E1AC73292E5EB7DE42813F6F403DE17A1436766A43BBB78387019BBAF910B86F98214278093889EBB8E9CCC24C4A659F6C5FA3C9F1B794C88544C36235739686474454253AFD99020193B86EBD6FB6CD190B723ECF73CAA3C35F65C82A9E0BEFC94711FBBAD428DD77276B2933508A086720B934EA0691A49D0510C1F107448BCF3FA9378057628280A0E49FE3F196F925E31D1234ECF759911F05274DC6291FF5AFF2EF6CB7039062314559F799D1CB5E82902D10B20755E45F23F397EA335098A8E87002D681D8A400E4AC56CEBC9E2BBA9023B311361A5432A321CB588C1BF691898DA6486A3088E5AD93A3A1DB99461E5A75825; }
最新发布
06-04

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值