题目链接: [传送门][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. 基于.NET Socket API 通信的综合应用

    闲谈一下,最近和客户进行对接Scoket 本地的程序作为请求方以及接受方,对接Scoket 的难度实在比较大,因为涉及到响应方返回的报文的不一致性,对于返回的报文的格式我需要做反序列化的难度增大了不少 ...

  2. STM32-外部中断,没有硬件干扰就是快乐

    一:触发方式 STM32 的外部中断是通过边沿来触发的,不支持电平触发: 二:外部中断分组 STM32 的每一个GPIO都能配置成一个外部中断触发源,STM32 通过根据引脚的序号不同将众多中断触发源 ...

  3. Xamarin.Android 反复报 Please Download android_m2repository_rxx.zip 的解决办法

    我原来一直用的是老版本的 Xamarin , android_m2repository_rxx.zip 早已在 C:\Users\XXX\AppData\Local\Xamarin\Android.S ...

  4. [已开源/文章教程]独立开发 一个社交 APP 的源码/架构分享 (已上架)

    0x00 背景 真不是和被推荐了2天的博客园一位大神较真,从他那篇文章的索引式文章内容也学习到了很多东西,看评论区那么多对社交APP源码有兴趣的,正巧我上周把我的一个社交APP开源了,包括androi ...

  5. Css-自适应高度修复(高度随内容而自动撑高)

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. C 语言学习的第 04 课:编译器常见错误和警告(1)

    同学们可能已经开始使用 C-Free 5 写自己的程序了.但是新手编程,总是会有一些磕磕绊绊.不要紧,在这篇文章中,就主要来了解一些编程开始时经常会遇到的语法方面的问题. warning: no ne ...

  7. JavaScript数独求解器

    <html> <head> <style type="text/css"> .txt { width: 50; height: 50; back ...

  8. No goals have been specified for this build

    在pom.xml文件中build后面加上<defaultGoal>compile</defaultGoal>

  9. Android应用崩溃后异常捕获并重启并写入日志

    在Android开发时,有时会因为一些异常导致应用报错,偶尔会因为错误 而崩溃,导致用户体验下降,为了解决这问题,我们就要对这样的异常处理: 代码如下: CrashHandler.java impor ...

  10. MySql_SQLyog快捷键

    1. SQL格式化 F12 格式化当前行所在的SQL Ctrl+F12    格式化选中的SQL Shift+F12   格式化所有SQL 2. 窗口操作 Ctrl+T 打开一个新的查询窗口 Alt+ ...