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< ...
随机推荐
- Java中PreparedStatement与Statement的总结
概要: PreparedStatement 接口继承自 Statement 接口,PreparedStatement 比普通Statement 对象使用起来更加灵活,更有效率. 一.PreparedS ...
- mongodb入门学习小记
Mongodb 简单入门(个人学习小记) 1.安装并注册成服务:(示例) E:\DevTools\mongodb3.2.6\bin>mongod.exe --bind_ip 127.0.0.1 ...
- LeetCode Find All Anagrams in a String
原题链接在这里:https://leetcode.com/problems/find-all-anagrams-in-a-string/ 题目: Given a string s and a non- ...
- C语言一维数组、二维数组、结构体的初始化
C语言数组的初始化表示方法 一.C语言一维数组初始化: (1)在定义数组时对数组元素赋以初值.如: static int a[10]={0,1,2,3,4,5,6,7,8,9}; 经过上面的定义和初始 ...
- 使用SpringMVC集成SpringSession的问题
最近在使用SpringSession时遇到一个问题,错误日志如下: Exception sending context initialized event to listener instance o ...
- 用Netty开发中间件:高并发性能优化
用Netty开发中间件:高并发性能优化 最近在写一个后台中间件的原型,主要是做消息的分发和透传.因为要用Java实现,所以网络通信框架的第一选择当然就是Netty了,使用的是Netty 4版本.Net ...
- php编写tcp服务器和客户端程序
这是我从别的地方看到的. 1.修改php.ini,打开extension=php_sockets.dll 2.客户端程序 SocketClient.php <?php set_time_limi ...
- C语言第三次作业
#include<stdio.h>//1.三角形 int main() { printf("*\n"); printf("**\n"); print ...
- XPath 简介
XPath 是一门在 XML 文档中查找信息的语言.XPath 用于在 XML 文档中通过元素和属性进行导航. 在学习之前应该具备的知识: 在您继续学习之前,应该对下面的知识有基本的了解: HTML ...
- oracle 笔试题
ORACLE笔试题一.单选题1.在Oracle中,以下不属于集合操作符的是( ). A. UNION B. SUM C. MINUS D. INTERSECT2.在Oracle中,执行下面的语句:SE ...