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;
}

原文地址:https://www.cnblogs.com/YDDDD/p/11779189.html

时间: 2024-08-28 16:37:22

Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer的相关文章

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 buttons of his keyboard are malfunctioning. For simplicity, we assume that Polycarp's keyboard contains 26 buttons (one for each letter of the Latin alph

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 same backward as forward (formally, t[i]=t[|t|+1?i] for all i∈[1,|t|]). Here |t| denotes the length of a string t. For example, the strings 010, 1001 and

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 people work at you, and n is odd (i.?e. n is not divisible by 2). You have to distribute salaries to your employees. Initially, you have s dollars for it,

Educational Codeforces Round 75 (Rated for Div. 2)

(体验到了胡出一道题但是写锅的绝望呢) A: 送分题. #include<bits/stdc++.h> #define maxn 100005 #define maxm 500005 #define inf 0x7fffffff #define ll long long using namespace std; bool may[30]; char str[maxn]; inline int read(){ int x=0,f=1; char c=getchar(); for(;!isdigi

Educational Codeforces Round 75 (Rated for Div. 2)D(二分)

#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;pair<int,int>a[200007];int n;long long s;bool check(int x){ int num=0; long long sum=s; for(int i=n;i;--i){ if(a[i].first>=x) ++num; else if(a[i].second>=x&&s

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.