链接:

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.

思路:

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

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. string s;
  5. int main()
  6. {
  7. ios::sync_with_stdio(false);
  8. int t;
  9. cin >> t;
  10. while(t--)
  11. {
  12. cin >> s;
  13. int p1 = -1, p2 = -1;
  14. int len = s.length();
  15. for (int i = 0;i < len;i++)
  16. {
  17. if (p1 == -1 && ((int)s[i]-'0')%2 == 0)
  18. p1 = i;
  19. if (p2 == -1 && ((int)s[i]-'0')%2 == 1)
  20. p2 = i;
  21. }
  22. while(p1 < len && p2 < len && p1 != -1 && p2 != -1)
  23. {
  24. if ((int)(s[p1]-'0') < (int)(s[p2]-'0'))
  25. {
  26. printf("%c", s[p1]);
  27. p1++;
  28. while(p1 < len && (int)(s[p1]-'0')%2 == 1)
  29. p1++;
  30. }
  31. else
  32. {
  33. printf("%c", s[p2]);
  34. p2++;
  35. while(p2 < len && (int)(s[p2]-'0')%2 == 0)
  36. p2++;
  37. }
  38. }
  39. if (p1 < len && p1 != -1)
  40. {
  41. for (int i = p1;i < len;i++)
  42. {
  43. if ((int)(s[i]-'0')%2 == 0)
  44. printf("%c", s[i]);
  45. }
  46. }
  47. else if (p2 < len && p2 != -1)
  48. {
  49. for (int i = p2;i < len;i++)
  50. {
  51. if ((int)(s[i]-'0')%2 == 1)
  52. printf("%c", s[i]);
  53. }
  54. }
  55. puts("");
  56. }
  57. return 0;
  58. }

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. Android--自定义Dialog style

    <style name="dialog" parent="@android:style/Theme.Dialog"> <item name=& ...

  2. O(1) gcd 板子

    const int N = 2e5+10; const int M = 500; int cnt, p[N], _gcd[M][M]; int v[N][3],vis[N]; int gcd(int ...

  3. RabbitMQ消息队列入门(一)——RabbitMQ消息队列的安装(Windows环境下)

    一.RabbitMQ介绍1.RabbitMQ简介RabbitMQ是一个消息代理:它接受和转发消息.你可以把它想象成一个邮局:当你把你想要发布的邮件放在邮箱中时,你可以确定邮差先生最终将邮件发送给你的收 ...

  4. ubuntu下安装软件时报错解决:Unmet dependencies. Try 'apt-get -f install' with no packages

    在错误后面运行以下代码,补全依赖项: sudo apt-get -f install

  5. CentOS 6.x 配置iptables

    CentOS 6.x 配置iptables 来源 https://www.cnblogs.com/chillax1314/p/7976067.html iptables -P INPUT DROP-- ...

  6. centos7 GNOME 安装微信客户端

    写在前边 最近新装了一个 centos7 GNOME 系统,用了很久了 win,突然转换 linux 桌面版,觉得焕然一新,给搬砖生活增添了一份新意 ~ 先看一下效果图: 怎么弄呢? 下载最新版本 t ...

  7. 【转载】Asp.Net中Cookie对象的作用以及常见属性

    Cookie对象是服务器为用户访问存储的特定信息,这些信息一般存储在浏览器中,服务器可以从提交的数据中获取到相应的Cookie信息,Cookie的最大用途在于服务器对用户身份的确认,即票据认证,用户会 ...

  8. h5 点击ios键盘完成 出现键盘大小的白块

    document.addEventListener('focusout', function (e) { window.scrollTo() }) 源文件链接 https://blog.csdn.ne ...

  9. react-native 沉浸式状态栏

    使用StatusBar即可实现沉浸式,但是必须把背景色设置为透明.否则如果有不同页面的头部颜色不一样的话,导航栏的颜色动画会很怪异,不会是跟着页面一起动画. <StatusBar barStyl ...

  10. EntityFramework进阶(三)- 根据IQueryable获取DbContext

    本系列原创博客代码已在EntityFramework6.0.0测试通过,转载请标明出处 有时候我们要通过IQueryable获取所在的DbContext信息,这是完全可以的. 以下代码从个人开源框架中 ...