QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#125288 | #6744. Square | hos_lyric | AC ✓ | 33ms | 3688kb | C++14 | 3.6kb | 2023-07-16 14:48:52 | 2023-07-16 14:48:54 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
constexpr Int INF = 1001001001001001001LL;
void experiment() {
constexpr int lim = 100;
vector<Int> ys(lim);
for (Int x = 0; x < lim; ++x) {
ys[x] = x + round(sqrt(2.0L * x) + 1.0L);
}
map<Int, vector<Int>> xss;
for (Int x = 0; x < lim; ++x) {
xss[ys[x] - x].push_back(x);
}
for (const auto &kv : xss) {
cerr << kv.first << " " << kv.second;
for (const Int x : kv.second) {
cerr << " " << ys[x];
}
cerr << endl;
}
}
/*
1 [0] 1
2 [1] 3
3 [2, 3] 5 6
4 [4, 5, 6] 8 9 10
5 [7, 8, 9, 10] 12 13 14 15
6 [11, 12, 13, 14, 15] 17 18 19 20 21
7 [16, 17, 18, 19, 20, 21] 23 24 25 26 27 28
8 [22, 23, 24, 25, 26, 27, 28] 30 31 32 33 34 35 36
9 [29, 30, 31, 32, 33, 34, 35, 36] 38 39 40 41 42 43 44 45
10 [37, 38, 39, 40, 41, 42, 43, 44, 45] 47 48 49 50 51 52 53 54 55
11 [46, 47, 48, 49, 50, 51, 52, 53, 54, 55] 57 58 59 60 61 62 63 64 65 66
12 [56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66] 68 69 70 71 72 73 74 75 76 77 78
13 [67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78] 80 81 82 83 84 85 86 87 88 89 90 91
14 [79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91] 93 94 95 96 97 98 99 100 101 102 103 104 105
15 [92, 93, 94, 95, 96, 97, 98, 99] 107 108 109 110 111 112 113 114
*/
pair<Int, Int> coords(Int x) {
Int a = (sqrt(8.0L * x + 1.0L) - 1.0L) / 2.0L;
a -= 2;
for (; a <= 0 || a*(a+1)/2 < x; ++a) {}
assert((a-1)*a/2 < x); assert(x <= a*(a+1)/2);
const Int b = x - (a-1)*a/2;
return make_pair(a, b);
}
Int solve(Int X, Int Y) {
Int ret = INF;
if (X >= Y) {
chmin(ret, X - Y);
} else {
const auto P = coords(X);
const auto Q = coords(Y);
// cerr<<P<<" "<<Q<<endl;
auto check = [&](Int a, Int b, Int c) -> void {
const Int da = Q.first - a;
assert(da >= 0);
const Int db = (b + da) - Q.second;
if (db >= 0) {
chmin(ret, c + da + db);
}
};
check(P.first, P.second, 0);
if (P.first > 0) {
check(P.first - 1, P.first - 1, P.second);
}
}
return ret;
}
int main() {
// experiment();
// for(Int x=1;x<=15;++x)cerr<<x<<": "<<coords(x)<<endl;
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
Int X, Y;
scanf("%lld%lld", &X, &Y);
const Int ans = solve(X, Y);
printf("%lld\n", ans);
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3632kb
input:
2 5 1 1 5
output:
4 3
result:
ok 2 number(s): "4 3"
Test #2:
score: 0
Accepted
time: 33ms
memory: 3688kb
input:
100000 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 ...
output:
0 2 1 4 3 2 6 5 4 3 8 7 6 5 4 10 9 8 7 6 5 12 11 10 9 8 7 6 14 13 12 11 10 9 8 7 16 15 14 13 12 11 10 9 8 18 17 16 15 14 13 12 11 10 9 20 19 18 17 16 15 14 13 12 11 10 22 21 20 19 18 17 16 15 14 13 12 11 24 23 22 21 20 19 18 17 16 15 14 13 12 26 25 24 23 22 21 20 19 18 1 0 2 2 1 3 4 3 2 4 6 5 4 3 5 ...
result:
ok 100000 numbers