经典的题目,主要还是考思维,之前在想的时候只想到了在一个循环中,每次都用最小的来交换,结果忽略了一种情况,还可以选所有数中最小的来交换一个循环。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6511 | Accepted: 2532 |
Description
Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow has a unique "grumpiness" level in the range 1...100,000. Since grumpy cows are more likely to damage FJ's milking equipment, FJ would like to reorder the cows in line so they are lined up in increasing order of grumpiness. During this process, the places of any two cows (not necessarily adjacent) can be interchanged. Since grumpy cows are harder to move, it takes FJ a total of X+Y units of time to exchange two cows whose grumpiness levels are X and Y.
Please help FJ calculate the minimal time required to reorder the cows.
Input
Output
Sample Input
3231
Sample Output
7
Hint
#include#include #include #include #include using namespace std;#define N 100100struct node{ int id; int num;}g[N];int mylink[N];int f[N],tf[N];int flag[N];long long ans;int mi;int cmp(node t,node t1){ return t.num