CF1810D图解
更好的阅读体验
原题:
思路:
经典的小学数学奥数题。
设
请注意,第
对于操作为
因为要保证第
用表达式表示为
需要特判
如果这个区间与之前之前计算的结果有交集,那么就是可以保留的,并更新区间,否则就丢弃之。
对于操作类型为
假设高度为
h 。
- 首先要预留一个
a 。
- 然后计算
\left\lfloor\frac{h - a}{a - b}\right\rfloor 表示到达小于等于h - a 的位置所需要的时间。
- 如果刚好到达
h - a 的位置+1 就可以了,否则+2 。注意最好不要直接上取整,因为容易引起精度问题。
这样就计算出了
注意我们不能直接判断已知范围
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
bool check(int& l1, int& r1, int l2, int r2, bool flag) {
int ll = max(l1, l2);
int rr = min(r1, r2);
if (ll > rr) return false;
if (flag) {
l1 = ll;
r1 = rr;
}
return true;
}
void solve() {
int q;
cin >> q;
int opt, a, b, c;
int l = -1, r = 1e18;
while (q--) {
cin >> opt;
if (opt == 1) {
cin >> a >> b >> c;
int lnew = -1, rnew = -1;
if (c == 1) lnew = 1, rnew = a;
else lnew = (a - b) * (c - 2) + a + 1, rnew = (a - b) * (c - 1) + a;
if (check(l, r, lnew, rnew, true)) cout << "1 ";
else cout << "0 ";
}
else {
cin >> a >> b;
int x = l, res = 0;
if (a >= l) {
res = 1;
}
else {
x -= a;
res = x / (a - b);
x = res * (a - b);
if (x == l - a) res++;
else res += 2;
}
c = res + 1;
int lnew = -1, rnew = -1;
if (c == 1) lnew = 1, rnew = a;
else lnew = (a - b) * (c - 2) + a + 1, rnew = (a - b) * (c - 1) + a;
if (check(l, r, lnew, rnew, false)) res = -1;
cout << res << ' ';
}
}
cout << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) solve();
return 0;
}
点个赞再走吧