链接:

https://codeforces.com/contest/1251/problem/C

题意:

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.

思路:

双指针奇偶数,奇偶比较大小即可。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL; string s; int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
cin >> s;
int p1 = -1, p2 = -1;
int len = s.length();
for (int i = 0;i < len;i++)
{
if (p1 == -1 && ((int)s[i]-'0')%2 == 0)
p1 = i;
if (p2 == -1 && ((int)s[i]-'0')%2 == 1)
p2 = i;
}
while(p1 < len && p2 < len && p1 != -1 && p2 != -1)
{
if ((int)(s[p1]-'0') < (int)(s[p2]-'0'))
{
printf("%c", s[p1]);
p1++;
while(p1 < len && (int)(s[p1]-'0')%2 == 1)
p1++;
}
else
{
printf("%c", s[p2]);
p2++;
while(p2 < len && (int)(s[p2]-'0')%2 == 0)
p2++;
}
}
if (p1 < len && p1 != -1)
{
for (int i = p1;i < len;i++)
{
if ((int)(s[i]-'0')%2 == 0)
printf("%c", s[i]);
}
}
else if (p2 < len && p2 != -1)
{
for (int i = p2;i < len;i++)
{
if ((int)(s[i]-'0')%2 == 1)
printf("%c", s[i]);
}
}
puts("");
} return 0;
}

Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer的更多相关文章

  1. Educational Codeforces Round 75 (Rated for Div. 2)

    知识普及: Educational使用拓展ACM赛制,没有现场hack,比赛后有12h的全网hack时间. rank按通过题数排名,若通过题数相等则按罚时排名. (罚时计算方式:第一次通过每题的时间之 ...

  2. Educational Codeforces Round 75 (Rated for Div. 2) D. Salary Changing

    链接: https://codeforces.com/contest/1251/problem/D 题意: You are the head of a large enterprise. n peop ...

  3. Educational Codeforces Round 75 (Rated for Div. 2) B. Binary Palindromes

    链接: https://codeforces.com/contest/1251/problem/B 题意: A palindrome is a string t which reads the sam ...

  4. Educational Codeforces Round 75 (Rated for Div. 2) A. Broken Keyboard

    链接: https://codeforces.com/contest/1251/problem/A 题意: Recently Polycarp noticed that some of the but ...

  5. Educational Codeforces Round 75 (Rated for Div. 2)D(二分)

    #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;pair<int,int>a[20 ...

  6. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

  7. Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...

  8. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  9. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

随机推荐

  1. Python19之函数和过程

    一.函数和过程 函数和过程都是指一段实现特定功能的代码段,如果该代码段有返回值则称为函数,否则称为过程. 注:Python中只有函数而没有过程,就算是函数体内没有return语句返回一个值,Pytho ...

  2. python之numpy和pandas

    一.numpy矩阵的拼接合并 列拼接:np.column_stack() >>> import numpy as np >>> a = np.arange(9).r ...

  3. Spring Boot集成Junit测试

    添加依赖: 在测试类上添加注解:

  4. Linux 查询端口被占用命令

    1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 lsof -i:8080:查看8080端口占用 lsof abc.txt:显示开启文件a ...

  5. pat天梯赛练习集合 L3-007 天梯地图

    加了一些花的最短路,点的个数为500不需要堆优化,改一下dij的判断条件就可以了. 上代码: #include <iostream> #include <cstring> #i ...

  6. docker registry-v2 搭建私有仓库

    参考官方文档:https://docs.docker.com/registry/deploying/ 参考 :http://www.tuicool.com/articles/6jEJZj 本例子使用两 ...

  7. vue 百度云上传文件PostObject

    百度云上传文件 API(PostObject) PostObject接口  : 接口描述 此接口使用HTML表单上传文件到指定bucket,用于实现通过浏览器上传文件到bucket.在PutObjec ...

  8. SSH和SSM对比异同点、各自优势

    1SSH和SSM定义SSH 通常指的是 Struts2 做控制器(Action),Spring 管理各层的组件,Hibernate 负责持久化层. SSM 则指的是 SpringMVC 做控制器(co ...

  9. 这个一个对ES6多个异步处理的并发继发思想的总结和理解

    1.首先我们需要理解的是js中for循环.forEach循环.map循环的一些差异性,直接说了为后面说到的提供一些依据 1.1 for循环最基本,也是最容易理解的. 1.2 forEach和map用法 ...

  10. MySQL 数据库的安装与卸载

    一.安装 (1)打开下载的 mysql 安装文件双击解压缩,运行“mysql-5.5.40-win32.msi” (2)选择安装类型, 有“Typical(默认) ”. “Complete(完全) ” ...