简单贪心....

B. Pasha Maximizes
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 test(s)
input
1990 1
output
9190
input
300 0
output
300
input
1034 2
output
3104
input
9090000078001234 6
output
9907000008001234

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; char str[200];
int nt; int main()
{
long long int a;
int k;
cin>>a>>k;
while(a)
{
str[nt++]=a%(10LL)+'0';
a/=10LL;
}
for(int i=nt-1;i>=0&&k;i--)
{
int mark=-1,dist=-1;
for(int j=i;j>=max(0,i-k);j--)
{
if(str[j]>str[mark])
{
mark=j; dist=i-j;
}
}
k-=dist;
for(int j=mark;j<i;j++)
{
swap(str[j],str[j+1]);
}
}
for(int i=nt-1;i>=0;i--)
{
cout<<str[i];
}
cout<<endl;
return 0;
}

Codeforces 435B. Pasha Maximizes的更多相关文章

  1. CF 435B Pasha Maximizes(贪心)

    题目链接: [传送门][1] Pasha Maximizes time limit per test:1 second     memory limit per test:256 megabytes ...

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

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

  3. Codeforces Round #249 (Div. 2) B. Pasha Maximizes

    看到题目的时候,以为类似插入排序,比较第i个元素和第i-1个元素, 如果第i个元素比第i-1个元素小,则不交换 如果第i个元素比第i-1个元素大,则交换第i个元素和第i-1个元素 继续比较第i-1个元 ...

  4. Codeforces 435 B Pasha Maximizes【贪心】

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

  5. codeforces 557B. Pasha and Tea 解题报告

    题目链接:http://codeforces.com/problemset/problem/557/B 题目意思:有 2n 个茶杯,规定第 i 个茶杯最多只能装 ai 毫升的水.现在给出 w 毫升的水 ...

  6. Codeforces 595B - Pasha and Phone

    595B - Pasha and Phone 代码: #include<bits/stdc++.h> using namespace std; #define ll long long # ...

  7. Codeforces 595B. Pasha and Phone 容斥

    B. Pasha and Phone time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. codeforces B. Pasha and String

    Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters ...

  9. codeforces B. Pasha and String(贪心)

    题意:给定一个长度为len的字符序列,然后是n个整数,对于每一个整数ai, 将字符序列区间为[ai,len-ai+1]进行反转.求出经过n次反转之后的序列! /* 思路1:将区间为偶数次的直接去掉!对 ...

随机推荐

  1. C++模板:字典树

    //插入 void insert(char *s,char *s1){ for(int l=strlen(s),x=0,i=0;i<l;i++){ if(!trie[x].son[s[i]-'a ...

  2. C# windows ce编程-----我的第一次

    最近公司要求开发抄表软件,软件分为PC端和手持终端(简称HHU),HHU是基于英文版的windows ce6.0操作系统,开发环境要求VS2005+SQLite数据库,开发语言为C#,因为是第一次基本 ...

  3. java线程学习——汉堡销售问题

    汉堡店中有一个负责做汉堡的厨师,一个负责销售的营业员,用java线程表示他们的营业过程: 问题原型就是生产者与消费者的问题. 首先定义一个汉堡包箱子类与几个相关的变量类: public class H ...

  4. HTML+CSS基础总结

    1. 首先声明正确的文档类型doctype 通常有四种文档类型可供选择: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" ...

  5. [译]MDX 介绍

    关于MDX MDX (Multi Dimensional eXpression language) 是非常强大的工具,可以将你的多维数据库/cube 发挥到极致. 本文会覆盖MDX基础,并且希望能使你 ...

  6. Linux下载文件时报错 To connect to ohse.de insecurely, use ‘--no-check-certificate’.

    远程调用下载文件直接用wget就可以,一般文件路径类型是http.如果有遇到是https就会下载出错. 错误如下: [root@vmu010226-node1 tmp]# wget https://w ...

  7. Java安全机制之泛型(JDK1.5)

    泛型,类型安全机制. 好处: 1.将运行时期出现问题ClassCastException转移到了编译时期,方便解决问题,减少运行时期的问题,有利于程序的健壮性. 2.避免了强制转换的麻烦 泛型格式: ...

  8. LNMP安装包sh脚本

    Xshell 5 (Build 0719) Copyright (c) 2002-2015 NetSarang Computer, Inc. All rights reserved. Type `he ...

  9. JS拖动浮动DIV

    <!DOCTYPE html> <html> <head> <meta charset="utf8"> <title>j ...

  10. html基本框架