提交时间:2022-07-20 12:02:02
运行 ID: 52826
#include <bits/stdc++.h> using namespace std; int n,a,b,p,cnt,u[25]; long long l = 1,q,minn; string w; int read() { char ch = getchar(); int ret = 0,f = 1; while(ch == ' ' || ch == '\n') ch = getchar(); while(ch == '-') { f *= -1; ch = getchar(); } while('0' <= ch && ch <= '9') { ret = ret * 10 + ch - '0'; ch = getchar(); } return f * ret; } int main() { srand(time(0)); n = read(),a = read(),b = read(); for(int i = 1; i <= n; ++i) { u[i] = read(); w += 'a' + u[i] - 1; l *= i; } minn = 0x7fffffff; while(p * b < minn) { if(p * b >= minn) break; cnt = 0; for(int i = 0; i < n; ++i) for(int j = i + 1; j < n; ++j) if(w[j] < w[i]) { swap(w[i],w[j]); cnt++; } minn = min(minn,1LL * p * b + 1LL * cnt * a); for(int i = 1; i <= n; ++i) u[i] = i; q = rand() % l; for(int i = 1; i <= q; ++i) next_permutation(u + 1,u + n + 1); w = ""; for(int i = 1; i <= n; ++i) w += u[i] + 'a' - 1; p++; } printf("%d\n",minn); return 0; }