题目大意
给你一个长度为 n n n 的数列 b b b ,让你把这 n n n 个数重新排列,使得 ∑ s q r t ( a b s ( b i − i ) ) \sum sqrt(abs(b_i - i)) ∑sqrt(abs(bi−i)) 最小,允许你的答案与最优答案相差不超过 4 % 4\% 4%
解题思路
没啥思路
乱搞一下,看不太懂
Code
#include <bits/stdc++.h>
#define ll long long
#define qc ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define pb push_back
using namespace std;
const int MAXN = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int a[MAXN];
int vis[MAXN];
int ans[MAXN];
int n;
double cal(int x, int y){
return sqrt(abs(x-y) * 1.0);
}
void solve(){
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
// 不循环四次过不了
int t = 4;
while(t--)
for(int i = 0; i < n; i++){
for(int j = i + 1; j < n; j++){
if(cal(i, a[i]) + cal(j, a[j]) > cal(i, a[j]) + cal(j, a[i]))
swap(a[i], a[j]);
}
}
for(int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
qc;
int T;
cin >> T;
// T = 1;
while(T--){
solve();
}
return 0;
}