一、题目
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;
}