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 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的更多相关文章
- Educational Codeforces Round 75 (Rated for Div. 2)
知识普及: Educational使用拓展ACM赛制,没有现场hack,比赛后有12h的全网hack时间. rank按通过题数排名,若通过题数相等则按罚时排名. (罚时计算方式:第一次通过每题的时间之 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
随机推荐
- C++ 计算定积分、不定积分、蒙特卡洛积分法
封装成了一个类,头文件和源文件如下: integral.h #pragma once //Microsoft Visual Studio 2015 Enterprise #include <io ...
- c++实现的顺序栈
栈是一种运算受限的线性表,是一种先进后出的数据结构,限定只能在一端进行插入和删除操作,允许操作的一端称为栈顶,不允许操作的称为栈底 因此需要的成员变量如下 int *_stack; //指向申请的空间 ...
- iTunes向ipad传影片
iTunes向ipad传影片(方法一) 在电脑上用itunes传视频到ipad-百度经验 iTunes向ipad传影片(方法二)
- linux 磁盘占用的排查流程
Linux 服务器在使用过程中可能会遇到各种问题,其中之一就是"没有可用空间". 遇到这种情况,就需要进行排查,定位到消耗了磁盘的那个文件夹. 流程如下: 1. df -h df ...
- vsCode 代码不高亮显示的问题——安装Vetur插件
vsCode 代码不高亮显示: 解决办法:安装Vetur插件 点击左侧菜单的扩展-->搜索Vetur-->点击安装-->安装完成重启vsCode
- maven一些简单常用却容易记混的命令参数-U -e -B
install 命令完成了项目编译.单元测试.打包功能,同时把打好的可执行jar包(war包或其它形式的包)布署到本地maven仓库,但没有布署到远程Maven私服仓库: deploy 命令完成了项目 ...
- C# DataGridView 动态添加列和行
https://blog.csdn.net/alisa525/article/details/7350471 dataGridView1.ReadOnly = true ; //禁用编辑功能 ...
- 90% 的 Python 开发者不知道的描述符应用
经过上面的讲解,我们已经知道如何定义描述符,且明白了描述符是如何工作的. 正常人所见过的描述符的用法就是上篇文章提到的那些,我想说的是那只是描述符协议最常见的应用之一,或许你还不知道,其实有很多 Py ...
- VBA用户自定义函数(十五)
函数是一组可重复使用的代码,可以在程序中的任何地方调用.这消除了一遍又一遍地编写相同的代码的需要.这使程序员能够将一个大程序划分成许多小的可管理的功能模块. 除了内置函数外,VBA还允许编写用户定义的 ...
- 【转载】常见面试题:C#中String和string的区别分析
在很多人面试C#开发工程师的时候,会遇到一个面试题,就是C#中String和string有啥区别.其实针对这个问题C#中String和string没有本质上的区别,两者在程序中都可使用,稍微的一个区别 ...