题目链接: [传送门][1]

Pasha Maximizes

time limit per test:1 second     memory limit per test:256 megabytes

Description

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.

Input

The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).

Output

Print the maximum number that Pasha can get if he makes at most k swaps.

Sample Input

1990 1

300 0

1034 2

Sample Output

9190

300

3104

9907000008001234

解题思路:

题目大意:给你一个整数,为交换相邻的两个数字k次能达到的最大数字的值
简单贪心,暴力从头开始枚举,选取从枚举位置开始前k个字符中最大的,使之前移。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
    char str[100];
    int ans[100],K,maxx,pos;
    memset(str,0,sizeof(str));
    memset(ans,0,sizeof(ans));
    while(~scanf("%s %d",str,&K))
    {
        int len = strlen(str);
        for (int i = 0; i < len; i++)
        {
            ans[i] = str[i] - '0';
        }
        bool flag = false;
        for (int i = 0; i < len; i++)
        {
            if (!K)
                break;
            maxx = 0;
            for (int j = i; j <= i + K && j < len; j++)
            {
                if (maxx < ans[j])
                {
                    maxx = ans[j];
                    pos = j;
                    flag = true;
                }
            }
            for (int j = pos; j > i; j--)
            {
                int tmp = ans[j];
                ans[j] = ans[j-1];
                ans[j-1] = tmp;
            }
            if (flag)
            {
                K = K - (pos - i);
                flag = false;
            }
        }
        for (int i = 0; i < len; i++)
        {
            printf("%d",ans[i]);
        }
        printf("\n");
        memset(str,0,sizeof(str));
        memset(ans,0,sizeof(ans));
    }
    return 0;
}

CF 435B Pasha Maximizes(贪心)的更多相关文章

  1. Codeforces 435B. Pasha Maximizes

    简单贪心.... B. Pasha Maximizes time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. Codeforces 435 B Pasha Maximizes【贪心】

    题意:给出一串数字,给出k次交换,每次交换只能交换相邻的两个数,问最多经过k次交换,能够得到的最大的一串数字 从第一个数字往后找k个位置,找出最大的,往前面交换 有思路,可是没有写出代码来---sad ...

  3. CF 557B(Pasha and Tea-贪心)

    B. Pasha and Tea time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. CF 115B Lawnmower(贪心)

    题目链接: 传送门 Lawnmower time limit per test:2 second     memory limit per test:256 megabytes Description ...

  5. CF Soldier and Badges (贪心)

    Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  6. CF Anya and Ghosts (贪心)

    Anya and Ghosts time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  7. cf 853 A planning [贪心]

    题面: 传送门 思路: 一眼看得,这是贪心[雾] 实际上,我们要求的答案就是sigma(ci*(ti-i))(i=1~n),这其中sigma(ci*i)是确定的 那么我们就要最小化sigma(ci*t ...

  8. codeforces 435 B. Pasha Maximizes 解题报告

    题目链接:http://codeforces.com/problemset/problem/435/B 题目意思:给出一个最多为18位的数,可以通过对相邻两个数字进行交换,最多交换 k 次,问交换 k ...

  9. CF 1082E Increasing Frequency(贪心)

    传送门 解题思路 贪心.对于一段区间中,可以将这段区间中相同的元素同时变成\(c\),但要付出的代价是区间中等于\(c\)的数的个数,设\(sum[i]\)表示等于\(c\)数字的前缀和,Max[i] ...

随机推荐

  1. STM32 控制红外线收发

    买了一块STM32的板子,这次需要将IR的code移植到STM32上面,因为STM32成本比树莓派低得多,所以 一些简单的外设挂在STM32上就行了. 我买的板子的型号是STM32F103C8T6,价 ...

  2. Theano2.1.6-基础知识之在thenao中的求导

    来自:http://deeplearning.net/software/theano/tutorial/gradients.html Derivatives in Theano 一.计算梯度 现在,让 ...

  3. checkboxlist 下拉框多选功能 ,模拟dropdownlist带复选框效果

    前台代码 01.<html xmlns="http://www.w3.org/1999/xhtml"> 02.<head runat="server&q ...

  4. 项目分享一:在项目中使用 IScroll 所碰到的那些坑

    最近做了个 WEB APP 项目,用到了大名鼎鼎的 IScroll,滚动的效果的确很赞,但是坑也是特别多,下面总结一下,希望自后来者有帮助. 该项目现已开源在 github 上,https://git ...

  5. nios II--实验2——led软件部分

    软件开发 首先,在硬件工程文件夹里面新建一个software的文件夹用于放置软件部分:打开toolsàNios II 11.0 Software Build Tools for Eclipse,需要进 ...

  6. MVC认知路【点点滴滴支离破碎】【五】----form表单上传单个文件

    //个人理解:前台一个form加input[type='file'],在加一个submit的按钮 主要设置form的action,method,enctype='multipart/form-data ...

  7. 让所有的浏览器都支持html5

    <!--[if lt IE 9]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js" ...

  8. Android四大组件之Activity详解——传值和获取结果

    废话不多说,先来看效果图 项目源码: http://download.csdn.net/detail/ginodung/8331535 程序说明: 在MainActivity中输入用户名和密码,然后提 ...

  9. linux基础-第十四单元 Linux网络原理及基础设置

    第十四单元 Linux网络原理及基础设置 三种网卡模式图 使用ifconfig命令来维护网络 ifconfig命令的功能 ifconfig命令的用法举例 使用ifup和ifdown命令启动和停止网卡 ...

  10. Andriod ADB开启Activity、Service以及BroadCast(包括参数的传递)

    /*****************开启Activity  并传递参数*******************/ 使用am命令启动Activity并传递参数的方法,也能用作C层与Java进行数据传递的一 ...