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 ...
随机推荐
- 玩转redis-延时消息队列
上一篇基于redis的list实现了一个简单的消息队列:玩转redis-简单消息队列 源码地址 使用demo 产品经理经常说的一句话,我们不光要有X功能,还要Y功能,这样客户才能更满意.同样的,只有简 ...
- go中的面向对象总结
我们总结一下前面看到的:Go 没有类,而是松耦合的类型.方法对接口的实现. OO 语言最重要的三个方面分别是:封装,继承和多态,在 Go 中它们是怎样表现的呢? 封装(数据隐藏):和别的 OO 语言有 ...
- wireshark抓包实战(三),界面菜单管理
1.默认列的增删查改 (1)增加列 选中某个关键词,然后右键应用为列 (2)修改列 选中某个列,右键编辑列 (3)删除列 选中某个列,然后选择移除该列 2.修改时间显示格式 依次选中"视图& ...
- 【Selenium02篇】python+selenium实现Web自动化:鼠标操作和键盘操作!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 这是python+selenium实现Web自动化第二篇博 ...
- Python Requests-学习笔记(11)-请求与响应对象
任何时候调用requests.*()你都在做两件主要的事情.其一,你在构建一个 Request 对象, 该对象将被发送到某个服务器请求或查询一些资源.其二,一旦 requests 得到一个从 服务器返 ...
- C#多线程(6):线程通知
目录 AutoRestEvent 类 常用方法 一个简单的示例 解释一下 复杂一点的示例 解释 回顾一下,前面 lock.Monitor 部分我们学习了线程锁,Mutex 部分学习了进程同步,Sema ...
- 这本最适合夯实基础的经典 Java 书籍,可能80% 的 Java 程序员没有认真看过!
公众号[程序员书单]出品,转载请注明出处 作者:黄小斜 今天要给大家带来的一本书,是大名鼎鼎的head first系列丛书的一本<head first Java>相信很多学习Java的朋友 ...
- 在数组添加元素时报错:IndexError: list index out of range
今天第一次发随笔还有许多不足之处,欢迎评论!!! 最近在写一个成语接龙的小游戏,结果在数组添加元素时报错:IndexError: list index out of range 源码: import ...
- php7.2.1+redis3.2.1 安装redis扩展(windows系统)
前提:已成功安装PHP环境和Redis服务 下面进入正题: 第一步,下载redis驱动扩展文件,注意:需要根据上面信息下载对应版本 https://windows.php.net/downloads/ ...
- jarvisoj MISC 取证2
打开之后一个文件和一个镜像 TrueCrypt....记住他了,再看一眼那个文件,好的,TrueCrypt加密..找密码 把Truecrypt.exe直接dump下来,用efdd解密就行了