This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize "O3,inline,unroll-loops,omit-frame-pointer"
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <set>
using namespace std;
using ll = long long;
const int oo = 2*1000*1000*1000 + 42;
const int NB_MAX_CELLULES_PLEINES = 300;
const int NB_MAX_INDEXS = 3*NB_MAX_CELLULES_PLEINES;
const int NB_MAX_EVENEMENTS = 2*NB_MAX_CELLULES_PLEINES;
int nbLignes, nbColonnes;
int nbCellulesPleines;
pair<int, int> cellulesPleines[NB_MAX_CELLULES_PLEINES];
struct Evenement
{
bool estDebut;
uint iColonne;
uint iLigneDebut, iLigneFin;
bool operator< (const Evenement &autre) const
{
if (iColonne != autre.iColonne) {
return iColonne < autre.iColonne;
}
if (estDebut != autre.estDebut) {
return estDebut;
}
if (iLigneDebut != autre.iLigneDebut) {
return iLigneDebut < autre.iLigneDebut;
}
return iLigneFin < autre.iLigneFin;
}
};
Evenement evenements[NB_MAX_EVENEMENTS];
bool getEstPossible (const uint nbBas, const uint nbDroite)
{
int nbEvenements = 0;
set<uint> lignesUtiles;
for (int iCellulePleine=0; iCellulePleine<nbCellulesPleines; iCellulePleine++) {
const uint iLigne = cellulesPleines[iCellulePleine].first;
const uint iColonne = cellulesPleines[iCellulePleine].second;
evenements[nbEvenements++] = Evenement{true, iColonne, iLigne, iLigne+nbBas+1};
evenements[nbEvenements++] = Evenement{false, iColonne+nbDroite+1, iLigne, iLigne+nbBas+1};
lignesUtiles.insert(iLigne);
lignesUtiles.insert(iLigne+nbBas+1);
if (iLigne > 0) {
lignesUtiles.insert(iLigne-1);
}
}
// Réindexation
map<uint, int> reindexation;
int nouvelIndex = 0;
for (uint index : lignesUtiles) {
reindexation[index] = nouvelIndex;
nouvelIndex++;
}
sort(evenements, evenements+nbEvenements);
for (int iEvenement=0; iEvenement<nbEvenements; iEvenement++) {
evenements[iEvenement].iLigneDebut = reindexation[evenements[iEvenement].iLigneDebut];
evenements[iEvenement].iLigneFin = reindexation[evenements[iEvenement].iLigneFin];
}
int nbRecouvrements[NB_MAX_INDEXS];
fill(nbRecouvrements, nbRecouvrements+NB_MAX_INDEXS, 0);
int dernierPlein[NB_MAX_INDEXS];
fill(dernierPlein, dernierPlein+NB_MAX_INDEXS, +oo);
int nbDebutsVus = 0;
for (const Evenement &evenement : evenements) {
const int iColonne = evenement.iColonne;
// Vérification de la validité
if (!evenement.estDebut && nbDebutsVus == nbCellulesPleines) {
uint iLigneInvalide = -1;
bool dernierValide = false;
for (uint iLigne : lignesUtiles) {
const int index = reindexation[iLigne];
if (dernierValide && iLigne-iLigneInvalide-1 >= (uint)nbLignes) {
return true;
}
if (iColonne - dernierPlein[index] < nbColonnes) { // Invalide
iLigneInvalide = iLigne;
dernierValide = false;
}
else {
dernierValide = true;
}
if (iLigne - iLigneInvalide >= (uint)nbLignes) {
return true;
}
}
}
nbDebutsVus += evenement.estDebut;
// Mise à jour
for (uint iLigne=evenement.iLigneDebut; iLigne<evenement.iLigneFin; iLigne++) {
nbRecouvrements[iLigne] += evenement.estDebut ? 1 : -1;
if (nbRecouvrements[iLigne] == 0) {
dernierPlein[iLigne] = +oo;
}
else if (dernierPlein[iLigne] == +oo) {
dernierPlein[iLigne] = iColonne;
}
}
}
return false;
}
int main ()
{
scanf("%d %d", &nbLignes, &nbColonnes);
scanf("%d", &nbCellulesPleines);
for (int iCellule=0; iCellule<nbCellulesPleines; iCellule++) {
int iLigne, iColonne;
scanf("%d %d", &iLigne, &iColonne);
iLigne--, iColonne--;
cellulesPleines[iCellule] = make_pair(iLigne, iColonne);
}
ll nbEtapesMin = +oo;
ll nbBas = 0;
ll nbDroite = 2*nbColonnes;
//int nbIterations = 0;
while (true) {
ll nbBasAvant = nbBas;
ll nbDroiteAvant = nbDroite;
// Augmenter nbBas
ll debut = nbBas - 1;
ll fin = 2*nbLignes;
while (fin-debut > 1) {
const ll milieu = (debut + fin) / 2;
if (getEstPossible(milieu, nbDroite-1)) {
fin = milieu;
}
else {
debut = milieu;
}
}
nbBas = fin;
// Diminuer nbDroite
debut = -1;
fin = nbDroite;
while (fin-debut > 1) {
const ll milieu = (debut + fin) / 2;
if (getEstPossible(nbBas, milieu)) {
fin = milieu;
}
else {
debut = milieu;
}
}
nbDroite = fin;
nbEtapesMin = min(nbBas+nbDroite, nbEtapesMin);
if ((nbBas == nbBasAvant && nbDroite == nbDroiteAvant) || nbDroite == 0) {
break;
}
/*if (nbIterations > 303) {
cerr << "ERREUR" << endl;
return 42;
}
nbIterations++;*/
}
printf("%lld\n", nbEtapesMin);
return 0;
}
Compilation message (stderr)
cultivation.cpp: In function 'int main()':
cultivation.cpp:145:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &nbLignes, &nbColonnes);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:146:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &nbCellulesPleines);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:150:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &iLigne, &iColonne);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |