【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;
}
【codeforces 508B】Anton and currency you all know的更多相关文章
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【13.77%】【codeforces 734C】Anton and Making Potions
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【81.37%】【codeforces 734B】Anton and Digits
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【77.39%】【codeforces 734A】Anton and Danik
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 785E】Anton and Permutation
[题目链接]:http://codeforces.com/problemset/problem/785/E [题意] 给你一个初始序列1..n顺序 然后每次让你交换任意两个位置上面的数字; 让你实时输 ...
- 【codeforces 785D】Anton and School - 2
[题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
随机推荐
- sql server中数据约束相关的查询
根据表名查找数据约束 SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'CMS_EventLog'; SEL ...
- Java 输入输出流 (七)
1.什么是IO Java中I/O操作主要是指使用Java进行输入,输出操作. Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列.Java的I/O流提供了读 ...
- bzoj3662
数学 其实我们发现不用每个数都去试一下,只要确定每个数字有几个就可以确定这个数.所以我们先搜索一下,然后检验. 但是这样太慢了,所以我们打表. 打出1-30的结果,然后取模. 打表的程序好像弄丢了.. ...
- oracle查询时间段
select count(*) tt from crm_cisco_call_detailwhere DateTime between to_date('2016-4-5 00:00:00','yyy ...
- [Swift通天遁地]三、手势与图表-(3)通过捏合手势放大和缩小图像视图
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- ACM_小Z的A+B
小Z的A+B Time Limit: 2000/1000ms (Java/Others) Problem Description: 小Z最喜欢A+B了,没事就研究研究,比如什么大整数A+B(就是100 ...
- Android 蓝牙开发基本流程
此例子基于 android demo 对于一般的软件开发人员来说,蓝牙是很少用到的,尤其是Android的蓝牙开发,国内的例子很少 Android对于蓝牙开发从2.0版本的sdk才开始支持,而 ...
- Hadoop Hive概念学习系列之hive里的视图(十二)
不多说,直接上干货! 可以先,从MySQL里的视图概念理解入手 视图是由从数据库的基本表中选取出来的数据组成的逻辑窗口,与基本表不同,它是一个虚表.在数据库中,存放的只是视图的定义,而不存放视图包含的 ...
- 这辈子写过的比较有意思的几个sql
递归 with myRecursion as( select * from recursion where id=1 union all select r.* from myRecursion m,r ...
- React Native组件间通信
React Native组件间通信 React Native组件的关系有:父子关系.无直接关系.组件间通信主要针对这两类来讨论. 一.父组件和子组件之间通信 父组件向子组件传递消息.数据通过对子组件的 ...