题目传送门

 /*
题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大
贪心:考虑两种情况:1. 有偶数且比末尾数字大(flag标记);2. 有偶数但都比末尾数字小(x位置标记)
仿照别人写的,再看自己的代码发现有清晰的思维是多重要
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
using namespace std; const int MAXN = 1e5 + ;
const int INF = 0x3f3f3f3f;
char s[MAXN]; int main(void) //Codeforces Round #288 (Div. 2) B. Anton and currency you all know
{
#ifndef ONLINE_JUDGE
freopen ("B.in", "r", stdin);
#endif scanf ("%s", &s); int len = strlen (s);
bool flag = false;
int x = -;
for (int i=; i<len-; ++i)
{
if ((s[i] - '') % == )
{
x = i;
if (s[i] < s[len-])
{
swap (s[i], s[len-]);
flag = true; break;
}
}
} if (!flag)
{
swap (s[x], s[len-]);
}
(x != -) ? printf ("%s\n", s) : puts ("-1"); return ;
}

 if (len == )
{
puts ("-1");
}
else if (len == )
{
if ((s[] - '') % != ) puts ("-1");
else
{
printf ("%c%c\n", s[], s[]);
}
}
else if (len == )
{
if ((s[] - '') % == )
{
if ((s[] - '') % == )
{
if (s[] > s[]) swap (s[], s[]);
else swap (s[], s[]);
printf ("%s\n", s);
}
else
{
swap (s[], s[]);
printf ("%s\n", s);
}
}
else if ((s[] - '') % == )
{
swap (s[], s[]);
printf ("%s\n", s);
}
else puts ("-1");
}
else
{
char ch = ''; int x = -;
for (int i=len-; i>=; --i)
{
if ((s[i]-'') % == )
{
if (ch <= s[i])
{
if (ch == s[i])
{
if (s[i] < s[len-])
{
ch = s[i]; x = i;
}
}
else
{
ch = s[i]; x = i;
}
}
}
}
if (x == -) puts ("-1");
else
{
swap (s[x], s[len-]);
printf ("%s\n", s);
}
}

思维混乱的代码

贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know的更多相关文章

  1. Codeforces Round #288 (Div. 2) B. Anton and currency you all know 贪心

    B. Anton and currency you all know time limit per test 0.5 seconds memory limit per test 256 megabyt ...

  2. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  3. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  4. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  5. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

    题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...

  6. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  7. 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

    题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...

  8. 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ

    题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...

  9. 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs

    题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...

随机推荐

  1. September 24th 2016 Week 39th Saturday

    The worst solitude is to be destitute of sincere friendship. 最大的孤独莫过于没有真诚的友谊. I walk slowly, but I n ...

  2. oracle两时间相减得到相差的时间

    1.months_between(date1,date2);date1和date2相减得到相差的月份. select months_between(to_date('2015-05-11','yyyy ...

  3. IIS6与IIS7中如何设置文件过期

    在IIS6中:一. 打开IIS管理器 二. 选中要设置的网站单击属性,打开站点属性菜单 三. 单击HTTP头选项卡 四. 单击 启用内容过期 如:设置30分钟后过期,此时间段后过期项中填30,单位选择 ...

  4. ios框架

    iPhone OS(现在叫iOS)是iPhone, iPod touch 和 iPad 设备的操作系统.        1,Core OS: 是用FreeBSD和Mach所改写的Darwin, 是开源 ...

  5. 18.中介者模式(Mediator Pattern)

    using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...

  6. 解决Pyqt打包后运行报错:应用程序无法启动 因为程序的并行配置不正确

    做了一个生成二维码的小程序:http://www.cnblogs.com/dcb3688/p/4241048.html 直接运行脚本没问题,用pyinstaller打包后再运行就直接报错了: 应用程序 ...

  7. 【Java EE 学习 21 上】【其它类型的监听器】【使用HttpSessionActivationListener监听session的活化和钝化】

    一.ServletContextListener  Method Summary  void contextDestroyed(ServletContextEvent sce)           R ...

  8. oracle pctfree和pctused详解

    一.建立表时候,注意PCTFREE参数的作用 PCTFREE:为一个块保留的空间百分比,表示数据块在什么情况下可以被insert,默认是10,表示当数据块的可用空间低于10%后,就不可以被insert ...

  9. 3-1创建Sql Sever数据库登录名

    登录名:连接Sql Sever 服务器 数据库用户名: Sql Sever 的使用者 每个用来登录Sql Sever 的账户都是一个用户. 同一个数据库可以拥有多个用户,每一个用户也同时可以访问多个数 ...

  10. phpcms标签使用 —— 系统常量

    以下系统常量全局可用 1 2 3 4 5 6 7 8 9 10 11 12 13 CACHE_PATH 缓存文件夹地址 SITE_PROTOCOL 主机协议 SITE_URL 当前访问的主机名 HTT ...