Codeforce 1251C. Minimize The Integer
C. Minimize The Integer
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a huge integer a consisting of n digits (n is between 1 and 3⋅105, inclusive). It may contain leading zeros.
You can swap two digits on adjacent (neighboring) positions if the swapping digits are of different parity (that is, they have different remainders when divided by 2).
For example, if a=032867235 you can get the following integers in a single operation:
302867235 if you swap the first and the second digits;
023867235 if you swap the second and the third digits;
032876235 if you swap the fifth and the sixth digits;
032862735 if you swap the sixth and the seventh digits;
032867325 if you swap the seventh and the eighth digits.
Note, that you can’t swap digits on positions 2 and 4 because the positions are not adjacent. Also, you can’t swap digits on positions 3 and 4 because the digits have the same parity.
You can perform any number (possibly, zero) of such operations.
Find the minimum integer you can obtain.
Note that the resulting integer also may contain leading zeros.
Input
The first line contains one integer t (1≤t≤104) — the number of test cases in the input.
The only line of each test case contains the integer a, its length n is between 1 and 3⋅105, inclusive.
It is guaranteed that the sum of all values n does not exceed 3⋅105.
Output
For each test case print line — the minimum integer you can obtain.
Example
inputCopy
3
0709
1337
246432
outputCopy
0079
1337
234642
Note
In the first test case, you can perform the following sequence of operations (the pair of swapped digits is highlighted): 070–––9→0079.
In the second test case, the initial integer is optimal.
In the third test case you can perform the following sequence of operations: 24643–––2→2463–––42→243–––642→234642.
因为只能交换奇数偶数,所以奇数偶数相对位置不变。然后将奇偶数分成两个队列,比较大小输出完事了!
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=300010;
typedef long long ll;
vector<int>v1,v2;
int t;
char s[maxn];
int main()
{
scanf("%d",&t);
while(t--){
v1.clear();
v2.clear();
scanf("%s",s);
int len=strlen(s);
int x;
for(int i=0;i<len;i++){
x=s[i]-'0';
if(x%2)v1.push_back(x);
else v2.push_back(x);
}
int i,j;
for( i=0,j=0;i<v1.size()&&j<v2.size();){
if(v1[i]<v2[j]) cout<<v1[i],i++;
else cout<<v2[j],j++;
}
if(i<v1.size()) for(int k=i;k<v1.size();k++)cout<<v1[k];
if(j<v2.size()) for(int k=j;k<v2.size();k++)cout<<v2[k];
cout<<endl;
}
}
Codeforce 1251C. Minimize The Integer的更多相关文章
- Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer
链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n ...
- Educational Codeforces Round 75
目录 Contest Info Solutions A. Broken Keyboard B. Binary Palindromes C. Minimize The Integer D. Salary ...
- ATC/TC/CF
10.25 去打 CF,然后被 CF 打了. CF EDU 75 A. Broken Keyboard 精神恍惚,WA 了一发. B. Binary Palindromes 比赛中的憨憨做法,考虑一个 ...
- Codeforce - Street Lamps
Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...
- SGU 248. Integer Linear Programming( 背包dp )
看了半天...发现就是个背包...然后就不打算敲了. 看了一眼forum..顿时吓傻..其他人用了gcd啊什么的各种奇怪的东西..然后还是敲了个背包结果就AC了= =既然写了代码就扔上来吧... -- ...
- Minimize the error CodeForces - 960B
You are given two arrays A and B, each of size n. The error, E, between these two arrays is defined ...
- 解题报告:codeforce 7C Line
codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...
- Two progressions CodeForce 125D 思维题
An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...
- Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心
B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...
随机推荐
- openlayers-统计图显示(中国区域高亮)
openlayers版本: v3.19.1-dist 统计图效果: 案例下载地址:https://gitee.com/kawhileonardfans/openlayers-examp ...
- C语言 加密解密
加密解密算法,对于一个未接触加密的人来说,这听起来是多么可望而不可及,但是只要我们理解了加密的本质,对于它就没那么陌生了,更难的是加密的算法,而不是加密这个术语上! 我们知道,文本文件是以ascii码 ...
- 利用xposed hook Auto.js程序、解密其js脚本
一.原理 原理很简单就是hook auto.js的com.stardust.autojs.script.StringScriptSource类,当然前题你要逆向的auto.js程序dex没有加固,当然 ...
- 响应式web设计(Responsive web design)
在全面进入互联网时代后,随着各种移动设备的普及,移动互联网更加受到大众的青睐.由于移动互联网的使用量远远超出了传统互联网的使用量,移动设备也正在逐渐超越桌面设备.因为用户在移动设备上的使用习惯不同,U ...
- Redis cluster集群配置教程
这里建议大家安装4.0.9版本的 1.打开Centos虚拟机,登陆. 2.通过WinSCP把Redis集群tar包上传到虚拟机里的目录里,我的目录是 /usr/local 这里我已经上传过了并解压了, ...
- AJ学IOS 之小知识之_xcode插件的删除方法_自动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自动提示,
AJ分享,必须精品 一:解决解决自动提示图片插件KSImageNamed有时不灵_分类或宏之类不能自动提示 其实,插件神马的我们自己也能写,并没有想象中的那么难,不过目前我们还是先解决当前问题 在做微 ...
- 【Java】 语言基础习题汇总 [2] 面向对象
30 面向对象的三条主线和面向对象的编程思想? 类与类的成员 : 属性.方法.构造器.代码块.内部类. 面向对象的三大特征:封装.继承.多态[如果还有一个,那就是抽象] 关键字:this.super. ...
- 阿里Canal框架数据库同步-实战教程
一.Canal简介: canal是阿里巴巴旗下的一款开源项目,纯Java开发.基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了MySQL(也支持mariaDB). 二.背景介绍: ...
- ATcoder D - Handstand 2
题目大意: 给一个数N,在小于N的所有数中,找到(A,B)的数量,其中A的第一个数字要等于B的最后的一个数字,A的最后一个数字要等于B的第一个数字. 题解:对从1到N的所有数x,用一个二维数组保存dp ...
- 在vue中使用ztree树插件
插件资源及api:树官网 本事例是在vue3.0+中演示,事例是实际项目中正在用的组件所以部分打了马赛克. 1.插件准备(提前准备好插件文件) 可以直接在官网下载,搭建好脚手架后将准备好的文件放在li ...