您的位置:首页 > 其它

Codeforces 435 B Pasha Maximizes【贪心】

2015-04-20 22:46 274 查看
题意:给出一串数字,给出k次交换,每次交换只能交换相邻的两个数,问最多经过k次交换,能够得到的最大的一串数字

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

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;

typedef long long LL;
const int INF = (1<<30)-1;
const int mod=1000000007;
const int maxn=100005;
char s[maxn];

int main(){
int k;
cin>>s;
cin>>k;
int best;
int len=strlen(s);
for(int i=0;i<len;i++){
best=i;
for(int j=i+1;j<=i+k&&j<len;j++){
if(s[j]>s[best]){
best=j;
}
}

if(best!=i){
for(int j=best;j>i;j--){
swap(s[j],s[j-1]);
k--;
}

}
}
printf("%s\n",s);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: