zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    NOI2005瑰丽华尔兹
    BZOJ4919[Lydsy1706月赛]大根堆
    BZOJ1097[POI2007]ATR-Tourist Attractions
    Luogu2973 [USACO10HOL]Driving Out the Piggies G
    Luogu5505 JSOI2011分特产
    Luogu1450 HAOI2008硬币购物
    NOI2001 炮兵阵地
    汕头市队赛 SRM 09 A 撕书
    洛谷P1196 银河英雄传说
    洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11779189.html
Copyright © 2011-2022 走看看