【CodeForces 915 C】Permute Digits(思维+模拟)
You are given two positive integer numbers a and b. Permute (change order) of the digits of a to construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.
It is allowed to leave a as it is.
Input
The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b(1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.
Output
Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.
The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.
Examples
123222
213
392110000
9321
49405000
4940
题意:
给定两个数字a和b,重新构建a,使a≤b的同时,a是该情况下的最大值。
思路:
(1) 先判断a与b的长度,若a的长度<b的长度,那么直接输出a的降序。
(2) 否则,令a升序排列。L指向a的最高位,R指向a的最低位。依次交换a[L]和a[R](将最大数与最小数进行交换),最高位后面的数按照升序排序。如果a<b,则将a[L]变成a[R],否则不能交换。
例:a=78135,b=55634 (a按升序排序为:13578)
<1> 81357->71358->51378 (确定第五位是5)
<2> 58137->57138->53178 (确定第四位是3)
<3> 53817 (确定第三位是1)
<4> 53871 (确定第二、第一位分别为7、1)
#include<algorithm> #include<iostream> #include<string> using namespace std; int main() { string a,b,t; cin>>a>>b; int lena=a.length(),lenb=b.length(); sort(a.begin(),a.end()); if(lena<lenb) reverse(a.begin(),a.end()); else { int L,R; ;L<lena;L++) { R=lena-; t=a; while(R>L) { swap(a[L],a[R--]); sort(a.begin()+L+,a.end()); if(a>b)a=t; else break; } } } cout<<a<<endl; ; }
【CodeForces 915 C】Permute Digits(思维+模拟)的更多相关文章
- Codeforces 915 C. Permute Digits (dfs)
题目链接:Permute Digits 题意: 给出了两个数字a,b(<=1e18),保证a,b都不带前缀0.用a的字符重组一个数字使这个值最大且小于b.(保证这个值存在) 题解: 这题遇到了不 ...
- CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)
ACM思维题训练集合 A new Berland businessman Vitaly is going to open a household appliances' store. All he's ...
- 【Codeforces】879D. Teams Formation 思维+模拟
题意 给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数 首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被 ...
- CodeForces-915C Permute Digits
C. Permute Digits time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- cf Permute Digits(dfs)
C. Permute Digits You are given two positive integer numbers a and b. Permute (change order) of the ...
- Codeforces Round #706 (Div. 2)B. Max and Mex __ 思维, 模拟
传送门 https://codeforces.com/contest/1496/problem/B 题目 Example input 5 4 1 0 1 3 4 3 1 0 1 4 3 0 0 1 4 ...
- C. Permute Digits dfs大模拟
http://codeforces.com/contest/915/problem/C 这题麻烦在前导0可以直接删除,比如 1001 100 应该输出11就好 我的做法是用dfs,每一位每一位的比较. ...
- Codeforces 758D:Ability To Convert(思维+模拟)
http://codeforces.com/problemset/problem/758/D 题意:给出一个进制数n,还有一个数k表示在n进制下的值,求将这个数转为十进制最小可以是多少. 思路:模拟着 ...
- Codeforces 758C:Unfair Poll(思维+模拟)
http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...
随机推荐
- C# 获取电脑硬盘剩余空间
获取本地硬盘的所有剩余空间: 主要应用到System.IO类库的:Driveinfo.Directory,将model转换成json需要用到Newtonsoft.Json.JsonConvert.Se ...
- C#中.Net的值传递和引用传递
/// <summary> /// 电脑类 /// </summary> public class Computer { public string Type { get; s ...
- vim右键粘贴 等杂
putty连上linux,vim编辑个文件,我去,右键不能用用上下面的命令,就好了. set mouse-=a 今天发现mysql倒入utf-8的文件网站显示出来都是乱码,不过用utf-8的控制台看是 ...
- 跨平台移动开发_PhoneGap 警告,通知,鸣叫,振动4 种通知类型
创建鸣叫 使用 confirmation.beep 创建鸣叫 function playBeep() { navigator.notification.beep(1); } 创建振动 使用 ...
- 基于alpine定制常用命令镜像
FROM alpine RUN apk update RUN apk add curl coreutils 像busybox.alpine镜像date命令都不是完整版的,不能执行加减的操作(date ...
- JS 排序:冒泡、 二分搜索 /折半搜索 half-interval search
冒泡排序: 重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来. var arr = [5,0,-56,900,12]; //大的排序次数 for(var i=0; i& ...
- 初识EMC
EMC,即电磁兼容,是指设备在预期的电磁环境中,能按设计要求正常抵抗电磁干扰的能力.其主要包含3个方面:电磁干扰(EMI),电磁抗扰(EMS)与静电放电抗扰(ESD). 电磁干扰的方式可以大概分为传导 ...
- 到底哪种类型的错误信息会阻止business transaction的保存
当试图在CRM WebUI保存一个business transaction比如Opportunity时,可能会遇到各种各样的错误消息.有的错误消息会阻止Business transaction被sav ...
- Android(java)学习笔记9:JDK5之后的Lock锁的概述和使用
1. Lock锁的概述: java.util.concurrent.locks,接口Lock 首先Lock是一个接口,Lock实现提供了比使用synchronized方法 和 同步代码块更为广泛的锁定 ...
- bzoj5000 OI树
Description 几天之后小跳蚤即将结束自己在lydsy星球上的旅行.这时,lydsy人却发现他们的超空间传送装置的能量早在小跳蚤通过石板来到lydsy星球时就已经消耗光了.这时,小跳蚤了解到自 ...