이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
long long n,d;
bool check(array<long long,3>fuels[], long long f){
long long F=f;
//cout << "attempting: " << f << "\n";
for(long long i = 1;i<n+2;i++){
//cout << "at station " << i << "\n";
//cout << fuels[i][0] << " " << fuels[i][1] << " " << fuels[i][2] << "\n";
f-=fuels[i][0]-fuels[i-1][0];
//cout << "current fuel: " << f << "\n";
if(f<0){
return 0;
}
if(F>fuels[i][2]){
continue;
}
f+=fuels[i][1];
//cout << "fueled from station " << i << "\n";
}
return 1;
}
bool check2(array<long long,3>fuels[], long long f){
long long F=f;
//cout << "attempting: " << f << "\n";
for(long long i = 1;i<=d;i++){
//cout << "at station " << i << "\n";
//cout << fuels[i][0] << " " << fuels[i][1] << " " << fuels[i][2] << "\n";
f-=fuels[i][0]-fuels[i-1][0];
//cout << "current fuel: " << f << "\n";
if(f<0){
return 0;
}
if(F>fuels[i][2]){
continue;
}
f+=fuels[i][1];
//cout << "fueled from station " << i << "\n";
}
return 1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n>>d;
array<long long,3> fuels[n+2];
fuels[0][0]=0;
fuels[0][1]=0;
fuels[0][2]=d+5;
fuels[n+1][0]=d;
fuels[n+1][1]=0;
fuels[n+1][2]=d+5;
vector<long long>bs;
for(long long i = 1;i<=n;i++){
long long a,b,x;
cin >> x >> a >> b;
fuels[i][0]=x;
fuels[i][1]=a;
fuels[i][2]=b;
bs.push_back(b);
}
bs.push_back(d);
sort(bs.begin(),bs.end());
sort(fuels,fuels+n+2);
if(d<=1e4){
array<long long,3>dummy[d+1];
for(long long i = 0;i<=d;i++){
dummy[i][0]=i;
dummy[i][1]=0;
dummy[i][2]=1e9;
}
for(long long i = 0;i<n+2;i++){
dummy[fuels[i][0]][1]+=fuels[i][1];
//cout << "added " << fuels[i][1] << " to " << fuels[i][0] << "\n";
swap(fuels[i][0],fuels[i][2]);
}
sort(fuels,fuels+n+2);
long long curind = 0;
for(long long i = 0;i<=d;i++){
while(curind<n+2&&fuels[curind][0]<i){
dummy[fuels[curind][2]][1]-=fuels[curind][1];
//cout << "removed " << fuels[curind][1] << " from " << fuels[curind][2] << "\n";
curind++;
}
if(check2(dummy,i)){
cout << i;
return 0;
}
}
cout << -1;
return 0;
}
long long hi = d;
for(long long i : bs){
if(check(fuels,i)){
hi=i;
break;
}
}
long long lo = 0;
while(lo<hi){
long long mid = (lo+hi)/2;
if(check(fuels,mid)){
hi=mid;
}
else{
lo=mid+1;
}
}
cout << lo;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |