Codeforces 435 B Pasha Maximizes【贪心】

题意:给出一串数字,给出k次交换,每次交换只能交换相邻的两个数,问最多经过k次交换,能够得到的最大的一串数字

从第一个数字往后找k个位置,找出最大的,往前面交换

有思路,可是没有写出代码来---sad

 1 #include<iostream>  

 2 #include<cstdio>  

 3 #include<cstring> 

 4 #include <cmath> 

 5 #include<stack>

 6 #include<vector>

 7 #include<map> 

 8 #include<set>

 9 #include<queue> 

10 #include<algorithm>  

11 using namespace std;

12 

13 typedef long long LL;

14 const int INF = (1<<30)-1;

15 const int mod=1000000007;

16 const int maxn=100005;

17 char s[maxn];

18 

19 int main(){

20     int k;

21     cin>>s;

22     cin>>k;

23     int best;

24     int len=strlen(s);

25     for(int i=0;i<len;i++){

26         best=i;

27         for(int j=i+1;j<=i+k&&j<len;j++){

28             if(s[j]>s[best]){

29                 best=j;

30             }

31         }

32         

33         if(best!=i){

34             for(int j=best;j>i;j--){

35                 swap(s[j],s[j-1]);

36                 k--;

37             }

38             

39         }

40     }

41     printf("%s\n",s);

42     return 0;

43 }
View Code

 

你可能感兴趣的:(codeforces)