贪心 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> ...
随机推荐
- 用jquery怎么实现点击显示,再一次点击隐藏
html代码: <button>点击</button> <div class="div"></div> css代码: <sty ...
- 解决spring+shiro cacheManager 登录报错
一.项目启动,登录报错 org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...
- cf378D(stl模拟)
题目链接:http://codeforces.com/contest/733/problem/D 用map<pair<int, int>int>标记(第一次用~)... 代码: ...
- Gif图片制作
gif图片是博客中展示项目效果的一种很好的方式,为我们的app制作一张gif图片并不复杂,录制屏幕采用系统自带的QuickTime Player,制作gif采用PicGIF软件.licecap软件更是 ...
- iOS - 线程管理
iOS开发多线程篇—GCD的常见用法 一.延迟执行 1.介绍 iOS常见的延时执行有2种方式 (1)调用NSObject的方法 [self performSelector:@selector(run) ...
- Tomcat的Session管理机制
>>Session和Cookie请求的过程 Http连接本身是无状态的,即前一次发起的连接跟后一次没有任何关系,是属于两次独立的连接请求,但是互联网访问基本上都是需要有状态的,即服务器需要 ...
- Linux 下 Console / 控制台 复制粘贴快捷键
Linux下复制粘贴快捷键 1. 在终端下: 复制命令:Ctrl + Shift + C 组合键. 粘贴命令:Ctrl + Shift + V 组合键. 2. 在控制台下: 复制命令:Ctrl + ...
- Install PIL on mac osX10.9
follow this instruction: http://blog.csdn.net/liushuaikobe/article/details/8729652 and if you encoun ...
- Linux重置root密码步骤
1.开机时任意按一个方向键,进入界面,选择linux系统,按e键进入2.然后用上下键选择kerner(内核)那一行,按e键进入编辑界面,编辑界面最后一行显示如下:(grub edit> kern ...
- wp8 入门到精通 Utilities类 本地存储+异步
public class CCSetting { public async static void AddOrUpdateValue<T>(string key, T value) { t ...