CF round #762(div2)C题 Challenging Cliffs
题目
补题。
昨晚真是没想到这么做啊。。。好蠢
官方tutorial:
标程:
#include "bits/stdc++.h"
using namespace std;
int main()
{
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<int> h(n);
for (int i = 0;i < n; i++){
cin >> h[i];
}
sort(h.begin(), h.end());
if(n == 2){
cout << h[0] << " " << h[1] << "\n";
continue;
}
int pos = -1, mn = INT_MAX;
for (int i = 1;i < n; i++){
if(mn > abs(h[i] - h[i - 1])){
pos = i;
mn = abs(h[i] - h[i - 1]);
}
}
for (int i = pos;i < n; i++){
cout << h[i] << " ";
}
for(int i = 0;i < pos; i++){
cout << h[i] << " ";
}
cout << "\n";
}
}
我的:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,n,a[200005];
int main()
{
cin>>t;
while(t--){
scanf("%d",&n);
memset(a,0,sizeof(a));
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
if(n==2){
cout<<a[0]<<' '<<a[1]<<endl;
continue;
}
int mi=1000000001,index=0,j;
for(j=1;j<n;j++){
if(a[j]-a[j-1]<mi){
mi=a[j]-a[j-1];
index=j-1;
}
}
j=index+1;
printf("%d ",a[index]);
index=(index+2)%n;
for(int i=1;i<n-1;i++){
printf("%d ",a[index]);
index=(index+1)%n;
}
printf("%d\n",a[j]);
}
return 0;
}