【codeforces 508B】Anton and currency you all know

【题目链接】:http://codeforces.com/contest/508/problem/B

【题意】

给你一个奇数;
让你交换一次数字;
使得这个数字变成偶数;
要求偶数要最大;

【题解】

肯定是1..len-1里面的某个偶数和最后那个奇数交换;
先考虑交换之后数字变大的情况;
即a[i]< a[len] 这里a[i]%2==0
这时,顺序1..len-1枚举找这样的数字;->在高位变大这样最优
找到之后交换直接输出;
然后数字变小的情况;
则逆序len-1..1找这样的数字即a[i]>a[len]这里a[i]%2==0
然后第一个找到的直接交换,然后输出;->在低位变小;
(数字不可能不变的,因为要交换的是一个奇数和一个偶数);
排除上面两张情况后输出-1

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100;

char s[N];
int a[N];
int len;

void o()
{
    rep1(i,1,len)
        cout << a[i];
    exit(0);
}

int main()
{
    //freopen("D:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin >> (s+1);
    len = strlen(s+1);
    rep1(i,1,len)
        a[i] = s[i]-'0';
    rep1(i,1,len-1)
        if (a[i]<a[len] && a[i]%2==0)
        {
            swap(a[i],a[len]);
            o();
        }
    rep2(i,len-1,1)
        if (a[i]>a[len] && a[i]%2==0)
        {
            swap(a[i],a[len]);
            o();
        }
    cout <<-1<<endl;
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(128)  评论(0编辑  收藏  举报