B - -- - B

题目来源

一、题目

Snuke has come to Seisu-ya (integer shop) with an integer
B in his hand. In Seiyu-ya, you can exchange your integer for another integer by paying money.
More specifically, you can use the following two services any number of times in any order:
Pay 1 yen (the currency of Japan) to multiply your integer by −1
Pay 2 yen to subtract 1 from your integer.
How many integers are there that Snuke can get for at most Cyen?
初始值B和钱数C,1块钱让B * -1, 2块钱让B - 1,问能得到最多的结果数。

二、输入

B C

三、输出

输出答案

四、样例

input:

11 2

output:

3

input:

0 4

output:

4

input:

112 20210213

output:

20210436

input:

-211 1000000000000000000

output:

1000000000000000422

五、思路 + 代码

想了半天,没想到啥好办法,
思路是以绝对值|B|为分界点,将数轴分为内部和外部两个区间再分别考虑
如果有更好的办法欢迎交流在这里插入图片描述

代码如下:

#include<iostream>
using namespace std;
typedef long long ll;
ll Max(ll a, ll b) {
	return a > b ? a : b;
}
ll b, c, ans;
//解决区间 : [-b, b]
ll solve_r(ll b, ll c) {
	ll depth = c / 2;
	ll flip = c & 1;
	if (depth < b) {
		return (1 + depth) * 2 - (!flip);
	}
	else {
		return b * 2 + 1;
	}
}
//解决区间 : (-inf, b) & (-b, +inf)
ll solve_l(ll b, ll c) {
	ll depth = c / 2;
	ll flip = c & 1;
	return Max(0, depth * 2 - (!flip));
}
int main() {
	cin >> b >> c;
	if (b > 0) ans = solve_r(b, c) + solve_l(-b, c - 1);
	else if (b == 0) ans = 1 + solve_l(b, c);
	else ans = solve_l(b, c) + solve_r(-b, c - 1);
	cout << ans << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值