CF Technocup 2020 - Elimination Round 3

A

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。

如果存在x且a≤x≤b和c≤x≤d,则两个段[a; b]和[c; d]具有一个公共点(相交)。例如,[2; 5]和[3;10]有一个共同点,但[5; 6]和[1; 4]没有。

您应该添加一个分段,该分段与每个给定分段至少要有一个公共点,并且要尽可能短(即长度最小)。所需的段可以退化为一个点 (即长度为零的段)加的段可能会也可能不在给定的n个段中。

换句话说,您需要找到一个段[a; b],使得[a; b]和每个[li; ri]的每个i都有一个公共点,而b-a最小。

输入项

第一行包含整数t(1≤t≤100)-输入中测试用例的数量。然后是t个测试用例。
每个测试用例的第一行包含一个整数n(1≤n≤105)-段数。以下n行包含段描述:第i行包含两个整数li,ri(1≤li≤ri≤109)。

输入中所有测试用例的所有值n的总和不超过105。

输出量

对于每个测试用例,输出一个整数-该段的最小可能长度,该段在所有给定段中至少具有一个公共点。

样例
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.

分析

这其实就是一道找规律的题,找到最小的 l 作为加入段的右端点,找到最小的 r 做加入段的左端点。

代码

#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;
 int n,mask=0;
 cin>>n;
 int ds,l,r;
 while(mask<n){
  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;
   mask++;
  }
 }

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.

译文

重要密钥在您需要打开的锁定框中。要打开此框,您需要输入密码。密码是长度为n的排列p。
您不知道此排列,只知道此排列的前缀最大值的数组q。正式地:
q1 = p1,
q2 = max(p1,p2),
q3 = max(p1,p2,p3),

qn = max(p1,p2,…,pn)。
您想要构造任何可能的合适排列(即任何此类排列,为此排列计算的q等于给定数组)。

输入项

第一行包含整数t(1≤t≤104)-输入中测试用例的数量。然后是t个测试用例。
测试用例的第一行包含一个整数n(1≤n≤105)-秘密代码排列p中的元素数。
测试用例的第二行包含n个整数q1,q2,…,qn(1≤qi≤n)-用于秘密置换的数组q的元素。对于所有i(1≤i<n),保证qi≤q(i+ 1)。
输入中所有测试用例的所有值n的总和不超过105。

输出量

对于每个测试用例,请打印:
如果找不到这样的排列p,请打印“ -1”(不带引号)。
否则,打印n个不同的整数p1,p2,…,pn(1≤pi≤n)。如果有多个可能的答案,则可以打印其中的任何一个。

样例
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][1,3,4,5,2] is the only possible answer:
· 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);
  }

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.

译文

这是问题的简单版本。在此版本中1≤n,m≤100。只有解决并锁定了两个问题,您才能解决此问题。
您将得到一个长度为n的整数序列a = [a1,a2,…,an]。它的子序列是通过从序列a中删除零个或多个元素获得的(它们不必连续进行)。 例如,对于序列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]是子序列(这些只是一长串的一部分);[40],[33,33],[33,20,20],[20,20,11,11]不是子序列。

假设给出了一个额外的非负整数k(1≤k≤n),则在以下情况下将该子序列称为最优
它的长度为k,其元素之和是所有长度为k的子序列中可能的最大值;
在满足上一项的所有长度为k的子序列中,按字典顺序最小。

回想一下,如果第一个元素(从左起)在序列b中的差异较小,则序列b = [b1,b2,…,bk]在字典上小于序列c = [c1,c2,…,ck]比c。形式上:存在t(1≤t≤k),使得b1 = c1,b2 = c2,…,bt-1 = ct-1,同时bt <ct。例如:

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

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

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

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

您将获得一个由a = [a1,a2,…,an]和m个请求组成的序列,每个请求均由两个数字kj和posj(1≤k≤n,1≤posj≤kj)组成。对于每个查询,在k = kj的情况下,打印给定序列a的最佳子序列的第posj个值。

例如,如果n = 4,a = [10,20,30,20],kj = 2,则最佳子序列为[20,30]-从字法上讲,这是长度2的所有子序列中最小的,总和最大项目总和。因此,请求kj = 2,posj = 1的答案是数字20,而请求kj = 2,posj = 2的答案是数字30。

输入项

第一行包含整数n(1≤n≤100)-序列a的长度。
第二行包含序列a的元素:整数a1,a2,…,an(1≤ai≤109).
第三行包含整数m(1≤m≤100)-请求数。
接下来的m行包含成对的整数kj和posj(1≤k≤n,1≤posj≤kj)—请求。

输出量

每行打印m个整数r1,r2,…,rm(1≤rj≤109):按请求在输入中出现的顺序回答。 rj的值应等于k = kj的最佳子序列位置posj中包含的值。

样例
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].

分析

先要找到k的最佳序列,然后输出其第posj个值。

代码

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

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

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页