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& ...
随机推荐
- GraphHopper-初识
GraphHopper GraphHopper is a fast and Open Source road routing engine. Is fast and memory efficie ...
- 综述论文翻译:A Review on Deep Learning Techniques Applied to Semantic Segmentation
近期主要在学习语义分割相关方法,计划将arXiv上的这篇综述好好翻译下,目前已完成了一部分,但仅仅是尊重原文的直译,后续将继续完成剩余的部分,并对文中提及的多个方法给出自己的理解. 论文地址:http ...
- [.Net] 一句话Linq(递归查询)
功能查询起止日期范围内连续的月份列表. /* Period */ cbxPeriod.DataSource = Enumerable.Range(, ).Select(t => DateTime ...
- javascript常用小案例
常用javascript小案例 样式调节 //注: 这个可以控制td中的字段成行显示 #modelInfos td,th { white-space: nowrap; } //文本输入框随着内容尺寸往 ...
- Python循环的基本使用(for in、while)
Python的循环有两种: 一种是for-in 循环:主要用于遍历tuple.list; 一种是while循环:只要条件满足,就不断循环,条件不满足时退出循环. #!/usr/bin/python # ...
- C++中的构造函数与析构函数及组合类的调用
// 构造函数与析构函数及类的组合 #include "stdafx.h"#include <iostream>using namespace std; //枚举enu ...
- java 简单操作HDFS
创建java 项目 package com.yw.hadoop273; import org.apache.hadoop.conf.Configuration; import org.apache.h ...
- SRID (空间引用识别号, 坐标系)【转】
SRID (空间引用识别号, 坐标系)-云栖社区-阿里云 Spatial Reference List -- Spatial Reference Chapter 8. PostGIS Referenc ...
- 为什么无人机测量主流现在都不用RTK技术,而是PPK技术【转】
为什么无人机测量主流现在都不用RTK技术,而是PPK技术_宇辰网_让世界读懂无人机_全球专业无人机资讯|电商|大数据服务平台 大疆Phantom 4 RTK正式发布_宇辰网_让世界读懂无人机_全球专业 ...
- Springmvc的@ResponseBody方法返回Model时404:跳转jsp视图
我有一个控制器方法,添加了@ResponseBody注解 @GetMapping(value = "/users") @ResponseBody public Map<Str ...