本文主要是介绍F - Pasha Maximizes,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
如有错误,欢迎大神指出!!!
题目:
Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.
Help Pasha count the maximum number he can get if he has the time to make at most k swaps.
The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).
Print the maximum number that Pasha can get if he makes at most k swaps.
1990 1
9190
300 0
300
1034 2
3104
9090000078001234 6
9907000008001234
题意:最多交换K次,交换只能两两相邻之间交换,获得最大的数。
解题思路:由于数据不大,直接进行暴力搜索。输入是用字符串输入。
ac code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s) scanf("%s",s)
#define pi1(a) printf("%d\n",a)
#define pi2(a,b) printf("%d %d\n",a,b)
#define mset(a,b) memset(a,b,sizeof(a))
#define forb(i,a,b) for(int i=a;i<b;i++)
#define ford(i,a,b) for(int i=a;i<=b;i++)
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define mod 1000000007
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
char ss[1006];int main()
{int n;scanf("%s",ss);int k;si1(k);for(int i=0;i<strlen(ss);i++){if(ss[i]!='9'){char c=ss[i];int num=0;for(int j=1;j<=k;j++){if(ss[j+i]>c){c=ss[i+j];//搜索在这个字符之后更大的字符。num=j;//记录位置if(c=='9')break;}}k=k-num;//维护交换次数for(int j=num;j>=1;j--)//交换后的字符串{char a;a=ss[i+j];ss[i+j]=ss[i+j-1];ss[i+j-1]=a;}//printf("%s\n",ss);}if(k<=0)break;//交换次数使用完}printf("%s\n",ss);return 0;
}
这篇关于F - Pasha Maximizes的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!