贪心 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> ...
随机推荐
- Java抽象类接口、内部类题库
一. 选择题 1. Person类和Test类的代码如下所示,则代码中的错误语句是( C ).(选择一项) public class Person { public String nam ...
- sqlserver 解析Json字符串
转自:https://www.simple-talk.com/sql/t-sql-programming/consuming-json-strings-in-sql-server/ http://ww ...
- Centos7 ZooKeeper 安装过程
www.apache.org/dist/上可以下载Hadoop整个生态环境的组件,我下的Zookeeper3.4.6版本 我一般都是在一个虚拟机上将一.二步都做完,然后克隆出来,再到克隆出来的虚拟机上 ...
- kmp
#include <bits/stdc++.h> #define MAXN 100000 using namespace std; string a, b; int next[MAXN]; ...
- mysqli的增强功能
批量执行sql语句 批量执行dml语句 基本语法 $sqls="sql1.sql2.sql3...." mysqli::multi_query($sqls) 案例: $mysqli ...
- Android Programming: Pushing the Limits -- Chapter 6: Services and Background Tasks
什么时候使用Service 服务类型 开启服务 后台运行 服务通信 附加资源 什么时候使用Service: @.任何与用户界面无关的操作,可移到后台线程,然后由一个Service来控制这个线程. 服务 ...
- EasyUi–7.tab和datagrid和iframe的问题
1. 多个tab切换,第2个不显示 动态添加tab Iframe页面的方法 展开 折叠 <script type="text/javascript"> $(functi ...
- Delphi之DLL知识学习3---为什么要使用DLL
使用DLL有若干理由,其中有一些前面提到过的.大体说来,使用动态链接库可以共享代码.系统资源,可以隐藏实现的代码或底层的系统例程.设计自定义控件 一.共享代码.资源和数据 前面已经提到,共享代码是创建 ...
- ODATA WEB API(二)----ODATA服务与客户端
一.概述 ODATA不经可以作为WebAPI建立相应的WEBAPI控制器,还可以建立ODataControl控制器,能够通过插件建立第三方ODataClinet类库:调用和使用数据变得简单可行. 二. ...
- Jquery自定义扩展方法(二)--HTML日历控件
一.概述 研究了上节的Jquery自定义扩展方法,自己一直想做用jquery写一个小的插件,工作中也用到了用JQuery的日历插件,自己琢磨着去造个轮子--HTML5手机网页日历控件,废话不多说,先看 ...