이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std;
const long long INFINI = 1000ll * 1000ll * 1000ll * 1000ll * 1000ll;
struct Inter {
int deb, fin, num;
Inter(int _deb = 0, int _fin = 0, int _num = 0) {
deb = _deb;
fin = _fin;
num = _num;
}
};
bool operator < (const Inter &a, const Inter &b) {
return a.fin < b.fin;
}
int nbStations, nbReqs;
long long hauteurs[300*1000];
vector<Inter> inters[300*1000];
bool check(long long maxi, int pos, long long nbInvs) {
vector<long long> invs(nbStations, 0);
priority_queue<Inter> ouverts;
long long nbOuverts = 0;
for(int iStation = 0;iStation <= pos;iStation++) {
for(Inter inter : inters[iStation]) {
if(inter.fin > pos) {
ouverts.push(inter);
}
}
long long besoin = (hauteurs[iStation] + nbInvs - maxi + 1) / 2;
if(iStation == pos) {
besoin = nbInvs;
}
long long doitOuvrir = max(0ll, besoin - nbOuverts);
nbOuverts += doitOuvrir;
while(doitOuvrir > 0) {
if(ouverts.empty())
return false;
Inter mel = ouverts.top();
ouverts.pop();
long long nbPose = min<long long>(mel.num, doitOuvrir);
invs[0] += nbPose;
invs[mel.deb] -= 2 * nbPose;
invs[mel.fin] += 2 * nbPose;
mel.num -= nbPose;
doitOuvrir -= nbPose;
if(mel.num != 0) {
ouverts.push(mel);
}
}
}
if(hauteurs[0] + invs[0] > maxi)
return false;
for(int iStation = 1;iStation < nbStations;iStation++) {
invs[iStation] += invs[iStation - 1];
if(hauteurs[iStation] + invs[iStation] > maxi)
return false;
}
return true;
}
bool estPossible(long long maxTickets) {
int maxGauche = 0;
int maxDroite = 0;
for(int iStation = 0;iStation < nbStations;iStation++) {
if(hauteurs[iStation] > hauteurs[maxGauche]) {
maxGauche = iStation;
}
if(hauteurs[iStation] >= hauteurs[maxDroite]) {
maxDroite = iStation;
}
}
if(hauteurs[maxGauche] <= maxTickets)
return true;
return check(maxTickets, maxGauche, hauteurs[maxGauche] - maxTickets) ||
check(maxTickets, maxGauche, hauteurs[maxGauche] - maxTickets + 1) ||
check(maxTickets, maxDroite, hauteurs[maxDroite] - maxTickets) ||
check(maxTickets, maxDroite, hauteurs[maxDroite] - maxTickets + 1);
}
int main() {
scanf("%d%d", &nbStations, &nbReqs);
for(int iReq = 0;iReq < nbReqs;iReq++) {
int deb, fin, num;
scanf("%d%d%d", &deb, &fin, &num);
if(fin < deb)
swap(deb, fin);
deb--; fin--;
hauteurs[deb] += num;
hauteurs[fin] -= num;
inters[deb].push_back(Inter(deb, fin, num));
inters[fin].push_back(Inter(deb, fin, num));
}
for(int iStation = 1;iStation < nbStations;iStation++) {
hauteurs[iStation] += hauteurs[iStation - 1];
}
long long deb = 0, fin = INFINI;
while(fin - deb > 1) {
long long mil = (deb + fin) / 2;
if(estPossible(mil)) {
fin = mil;
}
else {
deb = mil;
}
}
printf("%lld\n", deb + 1);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
arranging_tickets.cpp: In function 'int main()':
arranging_tickets.cpp:103:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &nbStations, &nbReqs);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
arranging_tickets.cpp:107:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &deb, &fin, &num);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |