Codeforces Round #812 (Div. 2)A,B,C
A. Traveling Salesman Problem
原题链接:A. Traveling Salesman Problem
题目大意:题目保证给出的点都在X,Y轴上,即可以找出相距原点最远的X,Y值,既然过去了还要算返回的路程,相加乘二即是答案
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+ 100;
int n,m,t,T;
signed main(){
while(cin >> T){
while(T--){
cin >> n;
bool f = 0;
int ans = 0;
int x1 = 0,x2 = 0,y2 = 0,y1 = 0;
for(int i = 0 ; i < n ; i ++){
int a,b;cin >> a >> b;
x1 = max(a,x1);
x2 = min(a,x2);
y1 = max(b,y1);
y2 = min(b,y2);
}
ans = abs(x1) + abs(x2) +abs(y1) + abs(y2);
cout << ans * 2 << endl;
}
}
return 0;
}
B. Optimal Reduction
原题链接:B. Optimal Reduction
题目大意:选择一个L和R,将其中的元素全部都-1,因为元素在操作中只会变小,不会变大,所以当出现[3,0,3]如果选择L = 1,R = 3,则得到[0,-3,0],不可能让全部都为零,这种情况的时候需要左边操作三次右边操作三次才能使全部都为零。
题目思路:只需要判断整个数组是否完全递增或者整个数组是否完全递减
题目坑点:5 2 2 2 5 重复的数值不好与前后判断,所以需要去重
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+ 100;
int n,m,t,T;
int s[N];
void solve(int index){
for(int i = 2 ; i < index ; i ++){
if(s[i-1]>s[i]&&s[i]<s[i+1]){
cout << "NO" << endl;
return ;
}
}
cout << "YES" << endl;
}
signed main(){
IOS
while(cin >> T){
while(T--){
cin >> n;
int index = 1;
for(int i = 1 ; i <= n ; i ++){
int x;cin >> x;
if(index==1){
s[index++] = x;
}else{
if(s[index-1]!=x){
s[index++] = x;
}
}
}
index--;
// cout << index << endl;
solve(index);
}
}
return 0;
}
C. Build Permutation
原题链接:C. Build Permutation
题目大意:给你一个n,你需要把0~n-1按照 a[i] + i = (任意一个数的平方数)
这题大致思路都在注释里
#include <bits/stdc++.h>
using namespace std;
int n,m,t,T;
const int N = 2E5+100;
int s[N];
int main(){
cin >> T;
while(T--){
cin >> n;
int index = 1;
bool f = 0;
map<int,int> mp; //map 保证答案唯一
int eag = index*index;
while(eag){ //先找到n+n能到的最大平方数是多少
if(eag>=n+n){
index--;
eag = index * index;
break;
}else{
index++;
eag = index * index;
}
}
//从最大平方数eag往下跑
for(int i = n - 1 ; i >= 0 ; i --){
// cout << "eag = " << eag << endl;
//i递减则eag - i 递增,找另一个完全平方数
while(eag - i >= n){
index--;
eag = index * index;
}
//利用mp去重往小的找
while(mp[eag-i]==1){
index--;
eag = index * index;
}
s[i] = eag - i;
mp[s[i]] =1; //mp去重一下
}
for(int i = 0 ; i < n; i ++){
cout << s[i] << " ";
}
cout << endl;
}
return 0;
}