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. PAT 乙级 1017

    题目 题目地址:PAT 乙级 1017 题解 粗看是一道大数除法题,实际上只不过是通过字符数组模拟除法过程,理解之后还是比较简单的: 具体分析一下本题: 因为题设中的除数(n)是一位整数,因此大幅简化 ...

  2. 将数组转化为json字符串(不使用json_encode函数)

    将数组转化为json字符串(不使用json_encode函数) public function arrayToJson($arr,$jsonStr=''){ $jsonStr.='{'; foreac ...

  3. 后缀数组的使用心得——POJ2774 最长连续公共子串

    对于这道题,将两个字符串直接合并成为一个字符串,分别记录连个字符串结束的位置. 首先,应用黑暗圣典的模板,我们可以顺利得到height,rank,sa三个数组. 之后直接扫描1-n所有的位置,选出来一 ...

  4. CCPC_1005

    可怕.....的提.....显而易见的规律活活没照出来...不过说起来却是不能严格证明....于是...脑筋急转弯活活猜不出来..... 1*1->1*2->2*2->2*3-> ...

  5. python实现分布式进程

    今天用python实现分布式,基于python2.7,注意:在linux下执行测试通过,在windows测试失败.# -*- coding: utf-8 -*-__author__ = 'dell'i ...

  6. linux下编译openjdk8

    一.准备工作                                                           1.0 工作环境 Ubuntu 12.04,32位机 1.1.安装JD ...

  7. VBA连接到SQL2008需要加上端口号

    VBA连接到SQL2008需要加上端口号1433,比如 conn = "server=XXXX.XXXX.XXXX.XXXX,1433;provider=SQLOLEDB.1;databas ...

  8. laravel5.2总结--数据填充

      1 生成一个seeder文件 你可以通过 make:seeder artisan命令来生成一个 Seeder.所有通过框架生成的 Seeder 都将被放置在 database/seeds 路径: ...

  9. 基于web自动化测试框架的设计与开发(本科论文word)

  10. ROM+VGA 图片显示

    内容 1.将一幅图片制成mif文件,初始化rom,图片像素为 120 * 60 2.驱动VGA,将图片显示在屏幕上 1.VGA 时序 下面是我的笔记截图,感觉更好理解. 2.640*480 60hz ...