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

Copy
10 20

Sample Output 1

Copy
10

Press button A ten times.


Sample Input 2

Copy
10 -10

Sample Output 2

Copy
1

Press button B once.


Sample Input 3

Copy
-10 -20

Sample Output 3

Copy
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的更多相关文章

  1. AtCoder Grand Contest 008

    AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...

  2. AtCoder Grand Contest 008 D - K-th K

    题目传送门:https://agc008.contest.atcoder.jp/tasks/agc008_d 题目大意: 给你一个长度为\(N\)的序列\(A\),请你构造一个长度为\(N^2\)的序 ...

  3. Atcoder Grand Contest 008 E - Next or Nextnext(乱搞+找性质)

    Atcoder 题面传送门 & 洛谷题面传送门 震惊,我竟然能独立切掉 AGC E 难度的思维题! hb:nb tea 一道 感觉此题就是找性质,找性质,再找性质( 首先看到排列有关的问题,我 ...

  4. AtCoder Grand Contest 008题解

    传送门 \(A\) 分类讨论就行了 然而我竟然有一种讨论不动的感觉 int x,y; inline int min(R int x,R int y){return x<y?x:y;} inlin ...

  5. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  6. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

  7. AtCoder Grand Contest 031 简要题解

    AtCoder Grand Contest 031 Atcoder A - Colorful Subsequence description 求\(s\)中本质不同子序列的个数模\(10^9+7\). ...

  8. AtCoder Grand Contest 010

    AtCoder Grand Contest 010 A - Addition 翻译 黑板上写了\(n\)个正整数,每次会擦去两个奇偶性相同的数,然后把他们的和写会到黑板上,问最终能否只剩下一个数. 题 ...

  9. AtCoder Grand Contest 009

    AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...

随机推荐

  1. maven---install报错

    若maven项目在install或者run的时候出现莫名奇妙的问题,应该考虑是否在pom.xml中引入的包是否为最新的包: 因为在本地maven仓库中已经存在的包就不会再下载,所以可以从这方面排查问题 ...

  2. 一篇文章让Oracle程序猿学会MySql【未完待续】

    一篇文章让Oracle DB学会MySql[未完待续] 随笔前言: 本篇文章是针对已经能够熟练使用Oracle数据库的DB所写的快速学会MySql,为什么敢这么说,是因为本人认为Oracle在功能性方 ...

  3. 使用HttpClient连接池进行https单双向验证

    https单双向验证环境的搭建参见:http://www.cnblogs.com/YDDMAX/p/5368404.html 一.单向握手 示例程序: package com.ydd.study.he ...

  4. logstash redis kafka传输 haproxy日志

    logstash 客户端收集 haproxy  tcp日志 input { file { path => "/data/haproxy/logs/haproxy_http.log&qu ...

  5. C++ 中的std::vector介绍(转)

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vec ...

  6. XMLtank测试记录

    1.首先就是这个分辨率对于高分屏的电脑适应太差了....立马让我有弃的想法..(如图) 2.游戏结束之后每次都要从第一关开始,很不方便.建议加入选关功能. 3.鼠标控制不方便,尤其是左键和右键分别控制 ...

  7. 深入了解DSP与ARM的区别与联系

    http://www.eeboard.com/bbs/thread-25219-1-1.html ARM微处理器的体系结构 了解DSP的体系结构 深入了解DSP与ARM的区别与联系 2011-09-3 ...

  8. CLI:使用Go开发命令行应用

      原文地址 CLI或者"command line interface"是用户在命令行下交互的程序.由于通过将程序编译到一个静态文件中来减少依赖,一次Go特别适合开发CLI程序.如 ...

  9. [pyqt4]mark

    sip SIP must be installed before building and using PyQt4 必须安装sip首先 https://riverbankcomputing.com/s ...

  10. C++之路进阶——poj3461(Oulipo)

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35694   Accepted: 14424 Descript ...