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 ...
随机推荐
- 当const放在function声明后
#include <iostream> class MyClass { private: int counter; public: void Foo() { std::cout <& ...
- Win10 cmd的ssh命令连接linux虚拟机
其实就是一个小发现了~ 闲的没事的时候在cmd里面敲了ssh命令,居然提示是一个命令,貌似以前是没有这功能的.然后就打开虚拟机试试能不能远程连接.没想到还成功了~ 有了这功能就省得安装专门的远程连接工 ...
- 1年左右的Java开发经验面试者的心得
面试,相信只要踏入这行业的人都会经历,不同的公司有不同的面试流程,但是综合起来,其实还是大体一致的!只有不断的总结自己的面试经历,得出自己的技术不足点,才能更好的去查缺补漏,从而更加自信的进行面试找到 ...
- 013-结构体-C语言笔记
013-结构体-C语言笔记 学习目录 1.[掌握]返回指针的函数 2.[掌握]指向函数的指针 3.[掌握]结构体的声明 4.[掌握]结构体与数组 5.[掌握]结构体与指针 6.[掌握]结构体的嵌套 7 ...
- SpringCloud系列之网关(Gateway)应用篇
@ 目录 前言 项目版本 网关访问 鉴权配置 限流配置 前言 由于项目采用了微服务架构,业务功能都在相应各自的模块中,每个业务模块都是以独立的项目运行着,对外提供各自的服务接口,如没有类似网关之类组件 ...
- 怎么自定义DataGridViewColumn(日期列,C#)
参考:https://msdn.microsoft.com/en-us/library/7tas5c80.aspx 未解决的问题:如果日期要设置为null,怎么办? DataGridView控件提供了 ...
- 转:Cookies 和 Session的区别
转自:http://blog.csdn.net/axin66ok/article/details/6175522 1.cookie 是一种发送到客户浏览器的文本串句柄,并保存在客户机硬盘上,可以用来在 ...
- 【题解】POJ3041 Asteroids - 图论 - 二分图匹配
声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 POJ3041 Asteroids 题目描述 假如你现在正处在一个 \(N*N\ ...
- 设计模式-原型模式(Prototype)【重点:浅复制与深复制】
讲故事 最近重温了一下星爷的<唐伯虎点秋香>,依然让我捧腹不已,幻想着要是我也能有一名秋香如此的侍女,夫复何求呀,带着这个美好的幻想沉沉睡去... 突然想到,我是一名程序猿呀,想要什么对象 ...
- PyTorch中在反向传播前为什么要手动将梯度清零?
对于torch中训练时,反向传播前将梯度手动清零的理解 简单的理由是因为PyTorch默认会对梯度进行累加.至于为什么PyTorch有这样的特点,在网上找到的解释是说由于PyTorch的动态图和aut ...