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. XCode5 使用AutoLayout情况下改变控件的 方法

    [self.viewButtonsetTranslatesAutoresizingMaskIntoConstraints:NO]; //[self.view addConstraint:[NSLayo ...

  2. ios swift 里面关于变量 常量 可选类型 控制流的一些心得

    //swift 里面没有头文件和实现文件.只有一个.swift文件 //swift 里面没有main的概念,程序从main.swift开始执行 //swift 每一条执行语句可以不用分号结束,多条语句 ...

  3. django1.11文档 模型重点笔记

    模型最重要的属性是Manager. 它是Django 模型进行数据库查询操作的接口,并用于从数据库提取实例. 如果没有自定义Manager,则默认的名称为objects. Managers 只能通过模 ...

  4. 洛谷 P2279 [HNOI2003]消防局的设立

    题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...

  5. Missian指南三:创建一个Missian服务器(使用spring)

    在使用Missian时,spring是可选的,但是作者本人强烈推荐和Spring配合使用.Spring是一个伟大的项目,并且它不会对程序在运行时的效率带来任何损耗. Missian在服务器端依赖与Mi ...

  6. Head First Python (一)

    建立一个数组: cast = ["Cleese","Palin","Jones","Idle"] 列出数组有多少数据项: ...

  7. 模拟:HDU1034-Candy Sharing Game

    解题心得: 1.直接模拟每一次分一半就行了,模拟过程,记录轮数,但是也看到有些大神使用的是链表,估计链表才是真的做法吧. 题目: Candy Sharing Game Time Limit: 2000 ...

  8. ACM二分搜索中的最大化最小值 总结

    这类题目都有个相似的地方就是需要你去找一个临界点. 分析题目要你求什么,例如时间 那么mid就是时间 看求得这个跟什么相关 例如 poj 3258 求得是距离 这个距离跟两者之间的差相关 那题目要求你 ...

  9. PowerShell批量配置VM端点

    我们可以通过PowerShell脚本批量添加VM端点.请您参考以下方案. 准备工作 – PowerShell连接China Azure 1. 从官网下载页面,下载并安装Windows Azure Po ...

  10. OpenCV学习笔记(三) 访问像素

    转自:OpenCV如何扫描图像.利用查找表和计时 测试代码:opencv\samples\cpp\tutorial_code\core\how_to_scan_images 测试函数耗时 cv::ge ...