简单贪心....

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. 基于Visual C++2013拆解世界五百强面试题--题11-查找数字出现次数

    在排序数组中,找出给定数字出现的次数比如{ 1, 2, 2, 2, 3}中2的出现次数是3次 我们可使用二分查找发,分别查找出2最先出现的位置和最后出现的位置相减即可. 下面是上代码: #includ ...

  2. Noip2008提高组总结

    Noip2008前三题是基础题,仔细一些都是可以AC的,第四题的证明很巧妙,但是看懂后代码其实很简单,感觉在这些大家都不屑去做的简单题中又学到了不少,四道题代码基本都是十几二十行就够了,渐渐感觉到,比 ...

  3. HDU 3756 Dome of Circus

    不会做,参见别人的程序: /* 底面为xy平面和轴为z轴的圆锥,给定一些点,使得圆锥覆盖所有点并且体积最小 点都可以投射到xz平面,问题转换为确定一条直线(交x,z与正半轴)使得与x的截距r 和与z轴 ...

  4. JS实现日历控件选择后自动填充

    最近在做人事档案的项目,在做项目的初期对B/S这块不是很熟悉,感觉信心不是很强,随着和师哥同组人员的交流后发现,调试程序越来越好了,现在信心是倍增,只要自己自己踏实的去研究.理解代码慢慢的效果就出来了 ...

  5. SPOJ LCS(Longest Common Substring-后缀自动机-结点的Parent包含关系)

    1811. Longest Common Substring Problem code: LCS A string is finite sequence of characters over a no ...

  6. linux下TUN/TAP虚拟网卡的使用

    转载:http://wushank.blog.51cto.com/3489095/1306849 tun/tap 驱动程序实现了虚拟网卡的功能,tun表示虚拟的是点对点设备,tap表示虚拟的是以太网设 ...

  7. Visual Studio Tools for Unity安装及使用

    Visual Studio Tools for Unity安装及使用 转载自:CSDN 晃了一下,10.1到现在又过去两个月了,这两个月什么也没有学,整天上班下班,从这周末开始拾起unity,为了年后 ...

  8. html 5 废弃的标签和属性

    第一类:表现性元素 basefont big center font s strike tt u 建议用语义正确的元素代替他们,并使用CSS来确保渲染后的效果 第二类:框架类元素 因框架有很多可用性及 ...

  9. ELK架构浅析

    转自:http://blog.csdn.net/lively1982/article/details/50678657 ELK是Elasticsearch.Logstash.Kibana的简称,这三者 ...

  10. jQuery 子元素选择

    对于如下代码片段  如何对河meishi这个div的子DIV呢? <div id="vertical-Menu-meishi" class="J-nav-item& ...