Problem 2111 Min Number

Accept: 1025    Submit: 2022
Time Limit: 1000 mSec    Memory Limit :
32768 KB

Problem Description

Now you are given one non-negative integer n in 10-base notation, it will
only contain digits ('0'-'9'). You are allowed to choose 2 integers i and j,
such that: i!=j, 1≤i<j≤|n|, here |n| means the length of n’s 10-base
notation. Then we can swap n[i] and n[j].

For example, n=9012, we choose i=1, j=3, then we swap n[1] and n[3], then we
get 1092, which is smaller than the original n.

Now you are allowed to operate at most M times, so what is the smallest
number you can get after the operation(s)?

Please note that in this problem, leading zero is not allowed!

Input

The first line of the input contains an integer T (T≤100), indicating the
number of test cases.

Then T cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤M≤100)
in a single line.

Output

For each test case, output the minimum number we can get
after no more than M operations.

Sample Input

3
9012 0
9012 1
9012 2

Sample Output

9012
1092
1029 
 
思路:贪心模拟,头部尽量与尾部的最小值交换。
AC代码:
#include <iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
#define N_MAX 10000+10
#define V_MAX 1000+10
#define INF 0x3f3f3f3f
int m;
string n;
void change(string &s,int n){//当前调整s的第n位
char c=''+;int id;
for(int i=s.size()-;i>n;i--){
if(c>s[i]){
if(n==&&s[i]=='')continue;
c=s[i];id=i;
}
}
if(c<s[n])swap(s[n],s[id]);
}
int main(){
int t;scanf("%d",&t);
while(t--){
cin>>n>>m;
int cnt=;
string s;
while(m){
s=n;
while(s==n&&cnt<s.size()-){
change(n,cnt);
cnt++;
}
if(cnt>=s.size()-)break;//已经不需要交换了
m--;
}
cout<<n<<endl;
}
return ;
}

foj 2111 Problem 2111 Min Number的更多相关文章

  1. fzu 2111 Min Number

      http://acm.fzu.edu.cn/problem.php?pid=2111  Problem 2111 Min Number Accept: 572    Submit: 1106Tim ...

  2. Problem 2111 Min Number

                                                                                                        ...

  3. (Problem 28)Number spiral diagonals

    Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...

  4. (Problem 17)Number letter counts

    If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + ...

  5. HDU Problem D [ Humble number ]——基础DP丑数序列

    Problem D Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...

  6. FZOJ2111:Min Number

    Problem Description Now you are given one non-negative integer n in 10-base notation, it will only c ...

  7. Codeforces Round #427 (Div. 2) Problem B The number on the board (Codeforces 835B) - 贪心

    Some natural number was written on the board. Its sum of digits was not less than k. But you were di ...

  8. LeetCode Problem 9:Palindrome Number回文数

    描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...

  9. FOJ题目Problem 2082 过路费 (link cut tree边权更新)

    Problem 2082 过路费 Accept: 382    Submit: 1279 Time Limit: 1000 mSec    Memory Limit : 32768 KB Proble ...

随机推荐

  1. Python小项目之五子棋

    1.项目简介 在刚刚学习完python套接字的时候做的一个五子棋小游戏,可以在局域网内双人对战,也可以和电脑对战 2.实现思路 局域网对战 对于局域网功能来说,首先建立连接(tcp),然后每次下棋时将 ...

  2. mysql零散操作

    添加对外用户 CREATE USER 'admin'@'%' IDENTIFIED BY '!QAZ2wsx'; GRANT ALL PRIVILEGES ON *.* TO 'admin'@'%'; ...

  3. win10.net 安装出问题0x800F70422

    因为安装ooracle数据库的时候需要用到.net但安装的时候出了0x800F70422, 随后就去网上查了下这个错出现的原因,发现是我之前把Windows自带的更新给禁用了 只要把它再开启就行了.

  4. 数据追踪系统Zipkin 及其 Zipkin的php客户端驱动hoopak

    Zipkin是Twitter的一个开源项目,是一个致力于收集Twitter所有服务的监控数据的分布式跟踪系统,它提供了收集数据,和查询数据两大接口服务.Zipkin 是一款开源的分布式实时数据追踪系统 ...

  5. Base64及其Python实现

    1. 什么是Base64 Base64是一种基于64个可打印字符来表示二进制数据的表示方法 Base64是一种编码方式,提及编码方式,必然有其对应的字符集合.在Base64编码中,相互映射的两个集合是 ...

  6. day14之模块

    一.列表生成式与生成表达式 1.列表生成式(数据量小) 要求:在列表内造100个元素 示例: l=[] for i in range(100): l.append('egg%s'%i) print(l ...

  7. dialog BLE SDK 学习(1)

    dialog DA14580 SDK版本:5.0.4. 本文介绍了SDK的运行流程,剖析arch_main.c的工作过程. dialog的BLE协议栈,本来是想学习一下,看看是否能够移植到其他平台上, ...

  8. Linux系统自启动脚

    只需编辑/etc/init.d/rc.local文件,在最后加上你的脚本即可. 比如:我已经编写了一个脚本shell.sh,存放在/home/mars704/Desktop/ 下面在终端输入 gedi ...

  9. DiyCode开源项目 BaseActivity 分析

    1.首先将这个项目的BaseActivity源码拷贝过来. /* * Copyright 2017 GcsSloop * * Licensed under the Apache License, Ve ...

  10. Java面向对象---方法递归调用

    递归调用是一种特殊的调用形式,即方法自己调用自己 public int method(int num){ if(num==1){ return 1; } else { return num+metho ...