package 蓝桥杯VIP; import java.util.Scanner; public class 完美的代价 { public static int sum = 0; public static void exchange(char[] arr, int x, int y) { //把arr字符数组中x下标和y下标对应的值交换位置 char item; item = arr[x]; arr[x] = arr[y]; arr[y] = item; sum ++; //移动次数加一 }…
思路(贪心): 1.两边往中间逼近,步数少; 2.单个字符出现时只考虑移动到中间的步数,不做移动,因为这是最后进行,不影响结果; 示例代码: #include <stdio.h>#define N 8000 int main(void){ int n = 0 ; int i = 0 , j = 0 , k = 0 , flag = 0 , sign = 0 , sum = 0; char arr[N] ; scanf("%d",&n); scanf("%s…