蓝桥杯练习系统—基础练习 完美的代价

Posted kindleheart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了蓝桥杯练习系统—基础练习 完美的代价相关的知识,希望对你有一定的参考价值。

分析:

贪心思想,以最左边的第一个字母L为基础,从最右边R往左依次寻找与其相同的字母,找到后就从左往右一直交换过去

L++,R--,一直重复这样的思路,直到L>R

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 const int MAX_N = 8000+5;
 5 int n; 
 6 char s[MAX_N];
 7 void solve() {
 8     int a = 0, b = n-1;
 9     int ans = 0;
10     bool flag = true;
11     while(a < b) {    
12         int c = b;
13         while(s[c] != s[a] && c > a) { c--; }  
14         if(c == a) { flag = false; break; }
15         else for(int i = c; i < b; i++) { 
16              swap(s[i], s[i+1]);
17             ans++;
18         }
19         a++; b--;
20     }
21     if(flag) printf("%d", ans);
22     else printf("Impossible\n");
23 }
24 int main() {
25     while(scanf("%d", &n) == 1) {
26         scanf("%s", s);
27         solve(); 
28     }
29     return 0;
30 } 

以上是关于蓝桥杯练习系统—基础练习 完美的代价的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥杯 基础练习 BASIC-19 完美的代价

蓝桥杯基础练习 完美的代价

[蓝桥杯] 基础练习 完美的代价

[蓝桥杯] 基础练习 完美的代价

[蓝桥杯] 基础练习 完美的代价

蓝桥杯_基础训练_完美的代价(贪心)