Educational Codeforces Round 36 (Rated for Div. 2) C 不大于某个数的序列变换
2018-01-19 12:42
387 查看
C. Permute Digitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou 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.InputThe 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.OutputPrint 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.Examplesinput
123 222output
213input
3921 10000output
9321input
4940 5000output
4940
给出A,B。对A进行变换,变为不大于B的最大的那个数。
这道题就是逐个暴力,从最大的开始找,如果找到,立即break。之后在查找下一位。
例如123 222
首先123对第一位进行处理,和最大的3呼唤,变为321,后两位排序 312. 比222大,不行
在和2互换,213,后两位排序213,可以。
之后对第二位进行处理,12呼唤,231,比222大,不行。
所以最终就是213.
就是对每一个位置进行暴。
#include <iostream> #include<algorithm> #include<stdio.h> #include<math.h> using namespace std; int main() { string a,b; cin>>a>>b; int len1=a.length(); int len2=b.length(); sort(a.begin(),a.end()); if(len1<len2) { for(int i=len1-1;i>=0;i--) { cout<<a[i]; } cout<<endl; return 0; } for(int i=0;i<len1;i++) { for(int j=len1-1;j>i;j--) { string c=a; swap(a[i],a[j]); sort(a.begin()+i+1,a.end()); if(a>b) { a=c; } else { break; } } } cout<<a<<endl; }#include <iostream> #include<algorithm> #include<stdio.h> #include<math.h> using namespace std; int main() { string a,b; cin>>a>>b; int len1=a.length(); int len2=b.length(); sort(a.begin(),a.end()); if(len1<len2) { for(int i=len1-1;i>=0;i--) { cout<<a[i]; } cout<<endl; return 0; } for(int i=0;i<len1;i++) { for(int j=len1-1;j>i;j--) { string c=a; swap(a[i],a[j]); sort(a.begin()+i+1,a.end()); if(a>b) { a=c; } else { break; } } } cout<<a<<endl; }
相关文章推荐
- Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree(并查集)
- Educational Codeforces Round 36 (Rated for Div. 2)D. Almost Acyclic Graph(正解!!百度的那些拓扑排序现在都已经TLE!)
- Educational Codeforces Round 36 (Rated for Div. 2) 题解
- Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
- Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
- Educational Codeforces Round 36 (Rated for Div. 2) A-C
- Educational Codeforces Round 36 (Rated for Div. 2)C. Permute Digits(贪心)
- 【codeforces 】Educational Codeforces Round 36 (Rated for Div. 2)(C.模拟 D.拓扑排序判断环)
- Codeforces Educational Codeforces Round 42 (Rated for Div. 2) D,E,F
- Educational Codeforces Round 33 (Rated for Div. 2) B - Beautiful Divisors
- Educational Codeforces Round 35 (Rated for Div. 2) D. Inversion Counting
- Educational Codeforces Round 40 (Rated for Div. 2)
- Educational Codeforces Round 37 (Rated for Div. 2) 【F】【线段树】
- codeforces Educational Codeforces Round 41 (Rated for Div. 2) for B problem
- Educational Codeforces Round 33 (Rated for Div. 2) A题. Chess For Three
- Educational Codeforces Round 33 (Rated for Div. 2) C - Rumor
- Educational Codeforces Round 33 (Rated for Div. 2)
- Educational Codeforces Round 34 (Rated for Div. 2)
- Educational Codeforces Round 40 (Rated for Div. 2)
- Educational Codeforces Round 37 (Rated for Div. 2)F. SUM and REPLACE