Codeforces Round #812 (Div. 2)
比赛链接:https://codeforces.com/contest/1713
A.Traveling Salesman Problem
You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).
More formally, if you are standing at the point (x,y), you can:
go left, and move to (x−1,y), or
go right, and move to (x+1,y), or
go up, and move to (x,y+1), or
go down, and move to (x,y−1).
There are n boxes on this plane. The i-th box has coordinates (xi,yi). It is guaranteed that the boxes are either on the x-axis or the y-axis. That is, either xi=0 or yi=0.
You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point (0,0).
Input
The first line contains a single integer t (1≤t≤100) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤100) — the number of boxes.
The i-th line of the following n lines contains two integers xi and yi (−100≤xi,yi≤100) — the coordinate of the i-th box. It is guaranteed that either xi=0 or yi=0.
Do note that the sum of n over all test cases is not bounded.
Output
For each test case output a single integer — the minimum number of moves required.
Example
input
3
4
0 -2
1 0
-1 0
0 2
3
0 2
-3 0
0 -1
1
0 0
output
12
12
0
Note
In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below.

(0,0)→(1,0)→(1,1)→(1,2)→(0,2)→(−1,2)→(−1,1)→(−1,0)→(−1,−1)→(−1,−2)→(0,−2)→(0,−1)→(0,0)
In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below.

(0,0)→(0,1)→(0,2)→(−1,2)→(−2,2)→(−3,2)→(−3,1)→(−3,0)→(−3,−1)→(−2,−1)→(−1,−1)→(0,−1)→(0,0)
In the third test case, we can collect all boxes without making any moves.
思路
根据样例解释,我们可以发现最终解决的是通过给定的几个坐标点,建立一个矩形,找出这个矩形的周长。


代码
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef unsigned long long ull;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};
const int N = 110;
int a[N][3];
void solve()
{
int n;
cin >> n;
//x最大,y最大,x最小,y最小
int a = 0 , b = 0, c = 0, d = 0;
int x , y;
for(int i = 0;i < n;i ++)
{
cin >> x >> y;
a = max(a,x);
b = max(b,y);
c = min(c,x);
d = min(d,y);
}
int ans = abs(a-c)+abs(b-d);
// cout << "ans=" ;
cout << ans*2 << endl;
}
int main()
{
/*读入优化*/
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T --)
solve();
return 0;
}
B. Optimal Reduction
Consider an array a of n positive integers.
You may perform the following operation:
select two indices l and r (1≤l≤r≤n), then
decrease all elements al,al+1,…,ar by 1.
Let's call f(a) the minimum number of operations needed to change array a into an array of n zeros.
Determine if for all permutations† b of a, f(a)≤f(b) is true.
† An array b is a permutation of an array a if b consists of the elements of a in arbitrary order. For example, [4,2,3,4] is a permutation of [3,2,4,4] while [1,2,2] is not a permutation of [1,2,3].
Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The first line of each test case contains a single integer n (1≤n≤105) — the length of the array a.
The second line contains n integers a1,a2,…,an (1≤ai≤109) — description of the array a.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case, print "YES" (without quotes) if for all permutations b of a, f(a)≤f(b) is true, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
Example
input
3
4
2 3 5 4
3
1 2 3
4
3 1 3 2
output
YES
YES
NO
Note
In the first test case, we can change all elements to 0 in 5 operations. It can be shown that no permutation of [2,3,5,4] requires less than 5 operations to change all elements to 0.
In the third test case, we need 5 operations to change all elements to 0, while [2,3,3,1] only needs 3 operations.
思路
给定一串数组,可以对该数组的l,r区域的每一个数-1,直到有数变为0,判断该串数组的顺序是否可以将所有数字变为0。
因此,我们可以判断出,如果该串数组只有一个峰,即

代码
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef unsigned long long ull;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};
const int N = 1e5+10;
int a[N];
void solve()
{
int n;
cin >> n;
for(int i = 1;i <= n;i ++)
cin >> a[i];
bool flag = false;
bool is = false;
for(int i = 1;i < n;i ++)
{
if(a[i] > a[i+1])
flag = true;
if(flag && a[i] < a[i+1])
is = true;
}
if(is)
puts("NO");
else
puts("YES");
}
int main()
{
/*读入优化*/
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T --)
solve();
return 0;
}
C. Build Permutation
Given an integer n. Find a permutation‡ p of [0,1,2,…,n−1] that is good or determine that no such permutation exists.
† An integer x is said to be a perfect square if there exists an integer y such that x=y2.
‡ An array b is a permutation of an array a if b consists of the elements of a in arbitrary order. For example, [4,2,3,4] is a permutation of [3,2,4,4] while [1,2,2] is not a permutation of [1,2,3].
Input
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The only line of each test case contains a single integer n (1≤n≤105) — the length of the permutation p.
It is guaranteed that the sum of n over all test cases does not exceed 105.
Output
For each test case, output n distinct integers p0,p1,…,pn−1 (0≤pi≤n−1) — the permutation p — if the answer exists, and −1 otherwise.
Example
input
3
3
4
7
output
1 0 2
0 3 2 1
1 0 2 6 5 4 3
Note
In the first test case, we have n=3. The array p=[1,0,2] is good since 1+0=12, 0+1=12, and 2+2=22
In the second test case, we have n=4. The array p=[0,3,2,1] is good since 0+0=02, 3+1=22, 2+2=22, and 1+3=22.
思路
题意:给定一个数n,构造一个初始下标为0的序列,该序列由[0 , n - 1]构成,一个序列完美当且仅当每个数的坐标于值之和(a[i] + i) 是一个完全平方数,请输出一个方案,若不存在输出-1。
先求出最大的完全平方数, 即比 2n-2 大的完全平方数, 然后从后往前, 暴力放就行了
代码
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef unsigned long long ull;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
int dx[] = {0,1,0,-1};
int dy[] = {-1,0,1,0};
const int N = 1e5+10;
int a[N];
void solve()
{
int n;
cin >> n;
memset(a,-1,sizeof(a));
//找出最大的完全平方数
int num = 0;
while(num*num < 2*n-2)
num ++;
num ++;
for(int i = n-1;i >= 1;i --)
{
for(int j = num;j >= 0;j --)
{
int x = j*j-i;
if(x >= 0 && x < n && a[x] == -1)
{
a[x] = i;
break;//跳出寻找位置的循环,对下一个数找位置
}
}
}
for(int i = 0;i < n;i ++)
{
if(a[i] == -1)
cout << 0 << " ";
else
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
/*读入优化*/
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while(T --)
solve();
return 0;
}

A.Traveling Salesman Problem
B. Optimal Reduction
C. Build Permutation
浙公网安备 33010602011771号