#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <queue>
using namespace std;
int n; vector<pair<int, int> > msts;
long long ts = 0, mpos[100001];
bool check(long long ms){
long long carry = 0;
for (int i = 100000; i > 0; i--){
if (mpos[i] > ms){
carry += mpos[i]-ms;
} else {
carry = max((carry-ms+mpos[i]), 0ll);
}
}
if (carry > 0){
return false;
}
return true;
}
long long ga(long long ms){
long long ans = 0, sum = 0, carry = 0;
for (int i = 100000; i > 0; i--){
mpos[i] += carry; long long su = min(ms-1, mpos[i]);
carry = mpos[i] - su; ans += ((su-1)*su)/(2ll); sum += su;
}
ans += (ts-sum) * (ms-1);
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n;
for (int i = 0; i < n; i++){
int a, b; cin >> a >> b;
msts.push_back(make_pair(a, b)); ts += b;
}
sort(msts.begin(), msts.end());
for (int i = 0; i < n/2; i++){
swap(msts[i], msts[n-i-1]);
}
int mstp = 0; priority_queue<int> pq;
for (int i = 100000; i > 0; i--){
while (mstp < n && i == msts[mstp].first){
pq.push(i-msts[mstp].second); mstp++;
}
while (pq.size() > 0 && pq.top() == i){
pq.pop();
}
mpos[i] = pq.size();
}
int low = 1, high = n+1;
while (true){
if (low == high){
cout << ga(low) << endl; return 0;
} else if (low + 1 == high){
if (check(low)){
cout << ga(low) << endl; return 0;
}
cout << ga(high) << endl; return 0;
}
int mid = (low+high)/2;
if (check(mid)){
high = mid;
} else {
low = mid;
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
2 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1132 KB |
Output is correct |
2 |
Correct |
3 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1132 KB |
Output is correct |
2 |
Correct |
3 ms |
1132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1260 KB |
Output is correct |
2 |
Correct |
15 ms |
1648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
1648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
1772 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
2536 KB |
Output is correct |
2 |
Correct |
29 ms |
2152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
2664 KB |
Output is correct |
2 |
Correct |
29 ms |
1940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
47 ms |
2456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |