链接:

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. 剑指offer54:字符流中第一个不重复的字符

    1 题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中 ...

  2. Windows Subsystem for Linux(wsl)使用

    由于项目有一些环境需要在linux环境运行.可用微软win10的WSL来搭配使用 安装wsl 控制面板--程序和功能--启用或关闭windows功能,适用于linux的windows系统 应用商城下载 ...

  3. SQLSERVER查询存储过程内容

    --使用语句查看一个存储过程的定义 EXEC sp_helptext 'Auth_BankCardAuthorize' --查询所有存储过程的名称以及定义 SELECT name, definitio ...

  4. Asp.Net进阶/管家模式+发布订阅模式:练习

    现在需要实现一个需求:我需要在一个窗体中发送一个信息,其余几个窗体都能同时接收到发送的消息. 1.界面:一个管家窗体,1个主窗体,2个订阅者窗体.其中管家窗体为启动窗体. 2.订阅:2个订阅窗体订阅主 ...

  5. WPF不同方式快捷键判断

    private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { //单个按键e.Key方式判断 if (e.Key == Key ...

  6. 最全的ADB命令行大全(转)

    基本用法 命令语法 adb 命令的基本语法如下: adb [-d|-e|-s ] 如果只有一个设备/模拟器连接时,可以省略掉 [-d|-e|-s ] 这一部分,直接使用 adb . 为命令指定目标设备 ...

  7. robot framework 怎么验证搜索无记录,页面元素不存在

    假设你要验证搜索无记录,页面元素不存在,假设我搜索的文本为你好 页面展示为如下 搜索:你好 假设页面搜索有结果: 你好  class=vtext 你好1  class=vtext 你好2  class ...

  8. R_数据视觉化处理_初阶_02

    通过数据创建一幅简单的图像, #Crate a easy photopdf("mygraph.pdf") attach(mtcars) plot(wt,mpg) abline(lm ...

  9. Vue的学习笔记

    以下文章皆为观看慕课网https://www.imooc.com/learn/796中“河畔一角”老师的讲解做的笔记,仅供参考. 一.Vue特点 Vue是MVVM的框架,也就是模型视图->视图模 ...

  10. 如何在SAP云平台ABAP编程环境里创建自己的Z表

    选中ABAP包,右键创建一个新的Database Table: 维护表名为ZBOOKING: 表实现的源代码: @EndUserText.label : 'Jerry''s booking' @Aba ...