【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排,就从第二列第一排开始点 ...
随机推荐
- (0!=0)==true? 记一个匪夷所思的问题
最近换了份工作,公司的开发框架是基于SSH自己搭建的.这个问题是我在解决一个需求的时候遇到的,其实解决这个疑惑的过程也就是读框架源码的过程,特此记录一下. 问题:ba.getState()!=CbBa ...
- lua继承
lua中其实是没有类的,有的只是表(table) lua查找一个表元素时的规则,其实就是如下3个步骤: 1.在表中查找,如果找到,返回该元素,找不到则往下看: 2.判断该表是否有元表,如果没有元表,返 ...
- 支持触屏的jQuery轮播图插件
移动轮播图我看到两类, 一款是无线天猫的m.tmall.com和携程,实现了无缝轮播. 一款是蘑菇街的,没有实现无缝轮播. 我自己重写一个,类似天猫. 1.页面代码 <!DOCTYPE html ...
- nodejs进阶(7)—async异步流程控制
Async介绍 Async是一个流程控制工具包,提供了直接而强大的异步功能.基于Javascript为Node.js设计,同时也可以直接在浏览器中使用. Async提供了大约20个函数,包括常用的 m ...
- C++基础--结构体声名
struct是一种数据结构,当需要存储的相关数据为一个集合时,struct是很好的选择;例如,当存储student,学生的学号, 名字,年龄,身高,就构成了一个集合,用stuct声名为: typede ...
- Java环境路径配置--转载
windows环境中JDK环境变量配置 一.环境准备 Windows10 jdk-9.0.1 二.下载并安装JDK 到Java的官网下载JDK安装包,地址:http://www.oracle.co ...
- wxpython 按钮等事件的触发
1.按钮事件的触发 方法中第二个参数为event
- 网络基础-IP、端口等
首先来理解一下几个概念. 白帽子:有能力破坏电脑安全但不具恶意目的的黑客.白帽子一般有清楚的定义.道德规范并常常试图同企业合作去改善发现的安全弱点. 正义技术员. 灰帽子:对 ...
- Django聚合函数
Django在Django .db中提供了以下聚合函数. 1.Avg:返回平均值 class Avg(expression, output_field=FloatField(), filter=Non ...
- Android(java)学习笔记45:深入分析Java ClassLoader原理
1. 前言: Android中的动态加载机制能更好的优化我们的应用,同时实现动态的更新,这就便于我们管理我们的应用,通过插件化来减轻我们的内存以及CPU消耗,在不发布新版本的情况下能更新某些模块. 当 ...