AtCoder Grand Contest 008 A
Problem Statement
Snuke has a calculator. It has a display and two buttons.
Initially, the display shows an integer x. Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:
- Button A: When pressed, the value on the display is incremented by 1.
- Button B: When pressed, the sign of the value on the display is reversed.
Find the minimum number of times Snuke needs to press the buttons to achieve his objective. It can be shown that the objective is always achievable regardless of the values of the integers x and y.
Constraints
- x and y are integers.
- |x|,|y|≤109
- x and y are different.
Input
The input is given from Standard Input in the following format:
- x y
Output
Print the minimum number of times Snuke needs to press the buttons to achieve his objective.
Sample Input 1
- 10 20
Sample Output 1
- 10
Press button A ten times.
Sample Input 2
- 10 -10
Sample Output 2
- 1
Press button B once.
Sample Input 3
- -10 -20
Sample Output 3
- 12
Press the buttons as follows:
- Press button B once.
- Press button A ten times.
- Press button B once.
题意:告诉我们两个数字,两种操作,一种是+1,一种是改变正负,问最小进行几次操作可以将x变成y
解法:讨论讨论讨论。。。。(注意等于0的情况)
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- int main()
- {
- ll n,m;
- cin>>n>>m;
- // cout<<min(abs(m-n),min(abs(-1*n-m)+1,min(abs(-1*n+m)+1,min(abs(-1*n+m)+2,abs(-1*n-m)+2))))<<endl;
- // if(n==m) continue;
- // cout<<n<<" "<<m<<" ";
- if(n<m)
- {
- ll pos=n*(-);
- if(n>=&&m>=)
- {
- //3 5
- cout<<m-n<<endl;
- }
- else if(n<=&&m>=)
- {
- //-3 5
- //3 5
- //-3 2
- //-3+1=-2 2
- if(pos<=m)
- {
- if(n!=)
- {
- cout<<m-pos+<<endl;
- }
- else
- {
- cout<<m-pos<<endl;
- }
- }
- else
- {
- // cout<<"A"<<endl;
- //ll ans=pos-m;
- if(m==)
- {
- cout<<pos<<endl;
- }
- else
- cout<<pos-m+<<endl;
- }
- }
- else if(n<=&&m<=)
- {
- //-3 -2
- cout<<pos+m<<endl;
- }
- }
- else
- {
- if(n>=&&m>=)
- {
- //4 2
- if(m!=)
- {
- cout<<n-m+<<endl;
- }
- else
- {
- cout<<n+m+<<endl;
- }
- }
- else if(n>=&&m<=)
- {
- ll pos=-*m;
- if(n==)
- {
- cout<<-*m+<<endl;
- }
- else
- {
- if(n<=pos)
- {
- cout<<pos-n+<<endl;
- }
- else
- {
- cout<<n+m+<<endl;
- }
- }
- }
- else
- {
- if(n==)
- {
- cout<<(-)*m+<<endl;
- }
- else
- {
- cout<<(-)*m-(-)*n+<<endl;
- }
- }
- }
- return ;
- }
AtCoder Grand Contest 008 A的更多相关文章
- AtCoder Grand Contest 008
AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...
- AtCoder Grand Contest 008 D - K-th K
题目传送门:https://agc008.contest.atcoder.jp/tasks/agc008_d 题目大意: 给你一个长度为\(N\)的序列\(A\),请你构造一个长度为\(N^2\)的序 ...
- Atcoder Grand Contest 008 E - Next or Nextnext(乱搞+找性质)
Atcoder 题面传送门 & 洛谷题面传送门 震惊,我竟然能独立切掉 AGC E 难度的思维题! hb:nb tea 一道 感觉此题就是找性质,找性质,再找性质( 首先看到排列有关的问题,我 ...
- AtCoder Grand Contest 008题解
传送门 \(A\) 分类讨论就行了 然而我竟然有一种讨论不动的感觉 int x,y; inline int min(R int x,R int y){return x<y?x:y;} inlin ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
- AtCoder Grand Contest 011
AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...
- AtCoder Grand Contest 031 简要题解
AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...
- AtCoder Grand Contest 010
AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...
- AtCoder Grand Contest 009
AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...
随机推荐
- HBASE数据模型&扩展和负载均衡理论
示例数据模型 HBase中扩展和负载均衡的基本单元成为region,region本质上是以行健排序的连续存储区间.如果region太大,系统会把它们 自动拆分,相反的,就是把多个region合并,以减 ...
- Linux基本操作命令
Linux基本操作命令 首先介绍一个名词“控制台(console)”,它就是我们通常见到的使用字符操作界面的人机接口,例如dos.我们说控制台命令,就是指通过字符界面输入的可以操作系统的命令,例如do ...
- 怎么搭建DC+SCCM 域环境(一)
需要的软件: 1. SCCM 2012 SP1 2. SQL Server 2012 3. System ISO 4. ADK 环境搭建顺序: 1. 安装DC和SCCM 机器,并配置需要的IP.DNS ...
- 自定义NSLog
我们在调试程序的时候,往往需要输出一些日志信息,用到NSLog函数,当我们准备发布程序,需要注释掉NSLog代码,这个时候往往会定义一个宏,在调试的时候,会输出日志,在Release正式版本的时候,会 ...
- 简单理解js的prototype属性
在进入正文之前,我得先说说我认识js的prototype这个东西的曲折过程. 百度js的prototype的文章,先看看,W3School关于prototype的介绍: 你觉得这概念适合定义js的pr ...
- [Android Tips] 21. Regex Named Groups in Android
Android SDK 并没有包含 Java 7 新增加的命名捕获组功能,需要使用第三方库 https://github.com/tony19/named-regexp import com.goog ...
- expdp ORA-31693 ORA-31617 ORA-19505 ORA-27037
使用expdp并行导出数据的时候报如下错误: Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64b ...
- update kernel 3.10-3.12
安装包下载以及依赖包安装 1.到www.kernel.org下载3.12.48压缩包 2.tar xvf linux-3.12.48.tar.xz 3.sudo yum install ncurses ...
- XPath 运算符
XPath 表达式可返回节点集.字符串.逻辑值以及数字. XPath 运算符 下面列出了可用在 XPath 表达式中的运算符: 运算符 描述 实例 返回值 | 计算两个节点集 //book | //c ...
- 关于使用FusionCharts生成图表时出现invalid xml data错误提示的解决方法
FusionCharts的确功能是够强大的.收集的功能估计更强大.在初次使用时,对着手册,一步一步操作,就是生成图表工具不成功.一直报"Invalid xml data"错误.后面 ...