//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define db long double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
void dout() { cerr << '\n'; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << " " << H;
dout(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << #__VA_ARGS__, dout(__VA_ARGS__)
#else
#define dbg(...) ;
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
const int N = 100007;
int n, h[N], k[N], cnt[N], tmp[N], f[N];
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
cin >> n;
int mx = 0;
for (int i = 1; i <= n; i++) {
cin >> h[i] >> k[i];
f[h[i]]++;
cnt[h[i] - k[i] + 1]++;
cnt[h[i] + 1]--;
mx = max(mx, h[i]);
}
for (int i = 1; i <= mx; i++) {
cnt[i] += cnt[i - 1];
}
for (int i = mx; i >= 1; i--) {
f[i] += f[i + 1];
}
for (int i = 1; i < mx; i++) {
int l = max(0, (cnt[i + 1] - cnt[i] + 1) / 2), r = f[i] - cnt[i];
for (int j = l; j <= r; j++) {
for (int k = i + 1; k <= mx; k++) {
tmp[k] = cnt[k];
}
int x = j;
for (int k = i + 1; k <= mx; k++) {
if (x >= tmp[k]) {
x -= tmp[k];
tmp[k] = 0;
} else {
tmp[k] -= x;
break;
}
}
ll total = 0, running = 0;
for (int k = i + 1; k <= mx; k++) {
total += tmp[k];
running = max(running, (total + (k - i - 1)) / (k - i));
}
if (cnt[i] + j >= tmp[i + 1] && cnt[i] + j >= running) {
cnt[i] += j;
for (int k = i + 1; k <= mx; k++) {
cnt[k] = tmp[k];
}
break;
}
}
}
ll ans = 0;
for (int i = 1; i <= mx; i++) {
ans += (ll)cnt[i] * (cnt[i] - 1) / 2;
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
376 KB |
Output is correct |
2 |
Correct |
305 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1086 ms |
376 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1081 ms |
760 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1097 ms |
888 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1085 ms |
1400 KB |
Time limit exceeded |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
2040 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1098 ms |
2168 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1094 ms |
2296 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |