贪心 Codeforces Round #288 (Div. 2) B. Anton and currency you all know
/*
题意:从前面找一个数字和末尾数字调换使得变成偶数且为最大
贪心:考虑两种情况: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的更多相关文章
- 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 ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- 贪心 Codeforces Round #301 (Div. 2) B. School Marks
题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges
题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...
- 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ
题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...
- 贪心 Codeforces Round #173 (Div. 2) B. Painting Eggs
题目传送门 /* 题意:给出一种方案使得abs (A - G) <= 500,否则输出-1 贪心:每次选取使他们相差最小的,然而并没有-1:) */ #include <cstdio> ...
随机推荐
- SQL基本CRUD
--已知Oracle的Scott用户中提供了三个测试数据库表 --名称分别为dept,emp,salgrade.使用SQL语言完成一下操作 --1,查询20号部门的所有员工信息: SELECT * F ...
- log4j介绍以及使用教程
一.介绍 Log4j是Apache的一个开放源代码项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接 口服务 器.NT的事件记录器.UNIX Syslog ...
- Jams倒酒
Jams是一家酒吧的老板,他的酒吧提供2种体积的啤酒,a ml 和 b ml,分别使用容积为a ml 和 b ml的酒杯来装载. 酒吧的生意并不好.Jams发现酒鬼们都很穷,不像他那么土豪.有时,他们 ...
- web页面记住密码存在安全问题 - 处理方式
现在一般安全网站都不会做记住密码功能,因为记住密码存在安全缺陷. 不考虑网络拦截问题,如果是登录页面记住密码,第二次登录,直接进入开发者模式修改类型为text即可看到密码. 处理方式: 1.把auto ...
- linux中解决SSH连接慢问题 关键点GSSAPIAuthentication
[root@ok 6FE5-D831]# ssh -v xxx.xxx.xxx.64 OpenSSH_5.3p1, OpenSSL Feb debug1: Reading configuration ...
- 与你相遇好幸运,Tippecanoe在Centos下の安装
全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...
- 12.享元模式(Flyweight Pattern)
using System; using System.Collections; namespace ConsoleApplication5 { class Program { /// <summ ...
- probe函数何时调用的
转自:http://blog.csdn.net/xiafeng1113/article/details/8030248 Linux中 probe函数何时调用的 所以的驱动教程上都说:只有设备和驱动的名 ...
- PHP求时间间隔 n天、周、月、年后的时间
<?php date_default_timezone_set('PRC'); // 设置时区 $date1 = strtotime('2015-01-01'); //把日期转换成时间戳 $da ...
- 设计模式学习之迭代器模式(Iterator,行为型模式)(17)
参考地址:http://www.cnblogs.com/zhili/p/IteratorPattern.html 一.介绍迭代器是针对集合对象而生的,对于集合对象而言,必然涉及到集合元素的添加删除操作 ...