Codeforces 508B. Anton and currency you all know

来源:互联网 发布:javaweb并发项目源码 编辑:程序博客网 时间:2024/06/03 17:52

B. Anton and currency you all know
time limit per test
0.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that to simplify the calculations, its fractional part was neglected and the exchange rate is now assumed to be an integer.

Reliable sources have informed the financier Anton of some information about the exchange rate ofcurrency you all know against the burle for tomorrow. Now Anton knows that tomorrow the exchange rate will be an even number, which can be obtained from the present rate by swapping exactly two distinct digits in it. Of all the possible values that meet these conditions, the exchange rate for tomorrow will be the maximum possible. It is guaranteed that today the exchange rate is anodd positive integer n. Help Anton to determine the exchange rate of currency you all know for tomorrow!

Input

The first line contains an odd positive integer n — the exchange rate ofcurrency you all know for today. The length of numbern's representation is within range from 2 to 105, inclusive. The representation ofn doesn't contain any leading zeroes.

Output

If the information about tomorrow's exchange rate is inconsistent, that is, there is no integer that meets the condition, print - 1.

Otherwise, print the exchange rate of currency you all know against the burle for tomorrow. This should be the maximum possible number of those that are even and that are obtained from today's exchange rate by swapping exactly two digits. Exchange rate representation should not contain leading zeroes.

Sample test(s)
Input
527
Output
572
Input
4573
Output
3574
Input
1357997531
Output
-1


大概题意是:

给出一个奇数(位数10^5,保证为奇数),找到一个位上的数为偶数,与最后一位交换,使这个数变成偶数,并且为最大可能的偶数。找不到则输出 -1 。直接贪心暴力求解。

例如: 

527  转换为  572
4573  转换为  3574
1357997531  找不到偶数  输出 -1

设最后一位为a, 大概思路是从头扫到尾,找出比a大的偶数b, 并且b 的位置越靠后越好, 同时找出比a小的偶数c, c的位置越靠前越好。

如果找到比a小的偶数c, 直接交换ac,  swap(a, c),不用理有没有找到b;

如果找不到c, 而找得到比a大的偶数b, 交换ab,  swap(a, b)。

如果b, c 都找不到,也就是找不到偶数,则输出 -1


例如:

(1)只找到比a小的偶数c, 交换最靠前的


1245  可以转换为

1542

1254

很明显转换为1542 得到的值更大


1425  可以转换为

1524

1452

很明显转换为1524 得到的值更大


(2)只找到比a大的偶数b,交换最靠后的

1685  可以转换为

1586

1658

很明显转换为1658 得到的值更大


1865  可以转换为

1856

1568

很明显转换为1856 得到的值更大


(3)如果既找到比a小的偶数, 也找到 比a大的偶数,则交换比a小的偶数,并且是最靠前的偶数

182825  可以转化为

152828

185822

182528

182852

很明显转换为185822 得到的值更大


#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <string>#include <algorithm>#include <queue>#include <stack>using namespace std;const int MAXN = 100010;char s[MAXN];int main(){    //freopen("in.txt", "r", stdin);    //freopen("out.txt", "w", stdout);    while(scanf("%s", s) != EOF)    {        int pos1 = -1;        int pos2 = -1;        int len = strlen(s);        for(int i = 0; i < len-1; i++)        {            if((s[i]-'0')%2==0 && s[i]<s[len-1] && pos1==-1)            {                pos1 = i;            }            if((s[i]-'0')%2==0 && s[i]>s[len-1])            {                pos2 = i;            }        }        if(pos1 != -1)        {            swap(s[len-1], s[pos1]);        }        else if(pos2 != -1)        {            swap(s[len-1], s[pos2]);        }        if(pos1==-1 && pos2==-1)            printf("-1\n");        else        {            printf("%s\n", s);        }    }    return 0;}


0 0