1 条题解
-
0
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<string> #include<queue> #include<stack> #include<vector> #include<cstdlib> #include<map> #include<cmath> using namespace std; const int maxn = 1e4 + 10; int now[maxn],ach[maxn],n,i,j,num[10*maxn]; bool vis[maxn]; long long ans = 0,Min = 1E9; int main() { /* #ifndef ONLINE_JUDGE #ifndef YZY freopen(".in","r",stdin); freopen(".out","w",stdout); #else freopen("yzy.txt","r",stdin); #endif #endif */ cin >> n; for (i = 1; i <= n; i++) { scanf("%d",&now[i]); ach[i] = now[i]; Min = min(1LL*now[i],Min); num[now[i]] = i; } sort (ach + 1,ach + n + 1); memset(vis,false,sizeof(vis)); for (i = 1; i <= n; i++) if (!vis[i]) { vis[i] = 1; int pos = num[ach[i]]; long long MM = now[i],tot = now[i],k = 1; while (pos != i) { vis[pos] = 1; tot += 1LL*now[pos]; MM = min(1LL*now[pos],MM); pos = num[ach[pos]]; ++k; } if (MM != Min) ans += min(tot + MM * (k-2),tot + Min * (k-1) + 2*(MM + Min) - MM); else ans += (tot + MM * (k-2)); } cout << ans; return 0; }
- 1
信息
- ID
- 1697
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- (无)
- 递交数
- 3
- 已通过
- 3
- 上传者