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& ...
随机推荐
- Windows 下配置 ApacheBench (AB) 压力测试
下载 http://httpd.apache.org/download.cgi 我用的是ApacheHaus. 安装服务 1. 打开apache目录下的 conf/httpd.conf,搜索并修改 L ...
- Python循环的基本使用(for in、while)
Python的循环有两种: 一种是for-in 循环:主要用于遍历tuple.list; 一种是while循环:只要条件满足,就不断循环,条件不满足时退出循环. #!/usr/bin/python # ...
- Mysql的多机配置(主从、主主等)
前言: 最近这几天给弄了2台机器,部署centlos7.5,除了公司的一些模块外,给2台机器做了下主主备份. 其实网上资料一大堆,但是感觉按照别人的思路不如自己的舒服,虽然这玩意思路差不多,但是还是在 ...
- web框架解析
一.白手起家 要想模拟出web请求响应的流程,先想想平时我们是怎么上网浏览网页的?首先打开浏览器,然后在地址栏中输入我们想要访问的页面,紧接着按下回车键Enter,最后跳转至目标页面(当然我们也会出现 ...
- vmware vSphere Data Protection 6.1 --------1-部署
一.简介 1.vdp的介绍 介绍可以参考:vmware vSphere Data Protection简述(未完成) 官方中文文档:https://docs.vmware.com/cn/VMware- ...
- Flask无法访问(127.0.0.1:5000)的问题解决方法
Flask默认开启的ip地址是:http://127.0.0.1:5000/ 但在运行时可能存在无法访问的问题,特别是当我们在linux服务器上搭建flask时,此时需要将代码修改如下: app.ru ...
- 基于【 SpringBoot】一 || QQ授权流程
一.准备工作 1.qq开放平台应用申请,获取APP ID和APP Key 2.qq开放平台配置回调地址 二.服务器端生成授权链接 1.请求地址 https://graph.qq.com/oauth2. ...
- Linux 常见压缩格式详解
linux 文件压缩格式详解 压缩文件原理 在计算机科学和信息论中,数据压缩或者源编码是按照特定的编码机制用比未经编码少的数据比特(或者其它信息相关的单位)表示信息的过程.例如,如果我们将" ...
- 【转载】Asp.Net MVC网站提交富文本HTML标签内容抛出异常
今天开发一个ASP.NET MVC网站时,有个页面使用到了FCKEditor富文本编辑器,通过Post方式提交内容时候抛出异常,仔细分析后得出应该是服务器阻止了带有HTML标签内容的提交操作,ASP. ...
- python的excel处理之openpyxl
一.颜色处理 cell = sheet.cell(row, col)font = Font(size=12, bold=False, name='Arial', color=colors.BLACK) ...