题目链接: [传送门][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. swift导航栏导航按钮添加多个按钮事件

    //导航左边返回按钮 let button1 = UIButton(frame:CGRectMake(0, 0, 18, 18)) button1.setImage(Constant.Image.Na ...

  2. Ionic实战四:ionic 即时通讯_ionic仿雅虎邮箱

    此产品是一款Ionic版微博.微信.朋友圈效果(微博.微信.聊天列表.聊天窗口.个人界面.编辑个人信息等)购买后二次开发方便快捷.    

  3. “奥特曼攻打小怪兽”java学习打怪升级第一步

    ---恢复内容开始--- 练习:回合制对战游戏:奥特曼和小怪兽进行PK,直到一方的血量为0时结束战斗,输出谁胜利了! 不难看出场景中有两个对象:”奥特曼“这一对象抽象为”Ao"类:     ...

  4. BroadcastReceiver之(手动代码注册广播)屏幕锁屏、解锁监听、开机自启

    对于解锁和锁屏这种用的比较频繁action,谷歌做了限制,必须手动用代码注册 直接上代码:这是注册广播(手动代码注册广播接收者) public class MainActivity extends A ...

  5. Centos|RHEL7以前解决网卡eth0相关问题

    网络-网络-有网才有络络,哈哈!学习在于不断记录,问题记录多了就成大牛了.当大牛达到一定层次,都会回馈社会.研发推出新的东东! CentOS找不到ifcfg-eth0解决方法 问题描述: ifconf ...

  6. 46-df 显示磁盘空间的使用情况

    显示磁盘空间的使用情况 df [options] [filesystem-list] 参数 当不带任何参数调用df时,用户将获得本地系统上每个挂载设备的空闲空间 filesystem-list是一个或 ...

  7. less sass学习总结(——待续哦——)

    一:less.sass是为了解决什么?  为什么要让css以编程语言来书写呢?

  8. if..elif语句

    根据用户输入内容打印其权限 # alex --> 超级管理员 # eric --> 普通管理员 # tony,rain --> 业务主管 # 其他 --> 普通用户 name ...

  9. 【CodeVS 1163】访问艺术馆

    http://codevs.cn/submission/2367697/ loli蜜汁(面向高一)树形dp是这道题的改编. 改编后的题目中每个展览厅的有多个不同的画,偷画的时间和画的价值也不同,求最大 ...

  10. 【BZOJ 1877】【SDOI 2009】晨跑

    拆点跑$MCMF最小费用最大流$ 复习一下$MCMF$模板啦啦啦--- 一些坑:更新$dist$后要接着更新$pre$,不要判断是否在队列中再更新,,,听不懂吧,听不懂就对了,因为只有我才会在这种错误 ...