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>
#include <vector>
#include <utility>
using namespace std;
using ll = long long;
const int oo = 2*100*1000*1000 + 42; // BUG: diminué
const int NB_MAX_CELLULES_PLEINES = 300;
const int NB_MAX_EVENEMENTS = 4*NB_MAX_CELLULES_PLEINES;
int nbLignes, nbColonnes;
int nbCellulesPleines;
pair<int, int> cellulesPleines[NB_MAX_CELLULES_PLEINES];
struct Evenement
{
bool estDebut;
bool estPourOuvertureFenetre;
uint iColonne;
uint iLigne;
bool operator< (const Evenement &autre) const
{
if (iColonne != autre.iColonne) {
return iColonne < autre.iColonne;
}
if (estDebut != autre.estDebut) {
return estDebut;
}
if (iLigne != autre.iLigne) {
return iLigne < autre.iLigne;
}
return estPourOuvertureFenetre < autre.estPourOuvertureFenetre;
}
};
const int NB_ETIREMENTS = 3;
const int ETIREMENT_HAUT = 0;
const int ETIREMENT_BAS = 1;
const int ETIREMENT_MILIEU = 2;
struct Colonne
{
int nbRecouvrements[100]; // pour chaque ligne
Colonne ()
{
fill(nbRecouvrements, nbRecouvrements+100, 0);
}
void appliquerEvenement (const Evenement &evenement)
{
nbRecouvrements[evenement.iLigne] += evenement.estDebut ? 1 : -1;
}
bool estVide () const
{
for (int iLigne=0; iLigne<nbLignes; iLigne++) {
if (nbRecouvrements[iLigne]) {
return false;
}
}
return true;
}
int nbEtirementsHaut () const
{
for (int iLigne=0; iLigne<nbLignes; iLigne++) {
if (nbRecouvrements[iLigne]) {
return iLigne;
}
}
return +oo;
}
int nbEtirementsBas () const
{
for (int iLigne=nbLignes-1; iLigne>=0; iLigne--) {
if (nbRecouvrements[iLigne]) {
return nbLignes - iLigne - 1;
}
}
return +oo;
}
int nbEtirementsMilieu () const
{
if (estVide()) {
return +oo;
}
int nbEtirementsMax = 0;
int dernierVu = +oo;
for (int iLigne=0; iLigne<nbLignes; iLigne++) {
if (nbRecouvrements[iLigne]) {
nbEtirementsMax = max(iLigne - dernierVu, nbEtirementsMax);
dernierVu = iLigne;
}
}
return nbEtirementsMax;
}
int nbEtirements (const int iEtirement) const
{
if (iEtirement == ETIREMENT_HAUT) {
return nbEtirementsHaut();
}
if (iEtirement == ETIREMENT_BAS) {
return nbEtirementsBas();
}
return nbEtirementsMilieu();
}
};
Evenement evenements[NB_MAX_EVENEMENTS];
int getNbEtirementsVerticauxMin (const uint nbDroite)
{
int nbEvenements = 0;
for (int iCellulePleine=0; iCellulePleine<nbCellulesPleines; iCellulePleine++) {
const uint iLigne = cellulesPleines[iCellulePleine].first;
const uint iColonne = cellulesPleines[iCellulePleine].second;
evenements[nbEvenements++] = Evenement{true, true, iColonne, iLigne};
evenements[nbEvenements++] = Evenement{false, true, iColonne+nbDroite+1, iLigne};
evenements[nbEvenements++] = Evenement{true, false, iColonne + nbColonnes, iLigne};
evenements[nbEvenements++] = Evenement{false, false, iColonne+nbDroite+1 + nbColonnes, iLigne};
}
sort(evenements, evenements+nbEvenements);
int nbEtirementsMin = +oo;
Colonne debutFenetre;
Colonne finFenetre;
int nbColonnesVides = nbColonnes;
map<int, int> nbOccurencesEtirements[NB_ETIREMENTS];
for (int iEtirement=0; iEtirement<NB_ETIREMENTS; iEtirement++) {
nbOccurencesEtirements[iEtirement][+oo] = nbColonnes;
}
for (int iEvenement=0; iEvenement<nbEvenements; iEvenement++) {
// Mettre à jour le meilleur
if (nbColonnesVides == 0) {
map<int, int>::iterator it;
it = nbOccurencesEtirements[ETIREMENT_HAUT].end();
it--;
const int nbHaut = it->first;
it = nbOccurencesEtirements[ETIREMENT_BAS].end();
it--;
const int nbBas = it->first;
it = nbOccurencesEtirements[ETIREMENT_MILIEU].end();
it--;
const int nbMilieu = it->first;
const int nbEtirements = nbHaut + nbBas + max(nbMilieu - nbHaut - nbBas, 0);
nbEtirementsMin = min(nbEtirements, nbEtirementsMin);
}
// Appliquer l'événement
const Evenement &evenement = evenements[iEvenement];
if (evenement.estPourOuvertureFenetre) {
debutFenetre.appliquerEvenement(evenement);
}
else {
finFenetre.appliquerEvenement(evenement);
}
// Avancer
if (iEvenement < nbEvenements-1 && evenements[iEvenement+1].iColonne != evenement.iColonne) {
const int distAAvancer = evenements[iEvenement+1].iColonne - evenement.iColonne;
if (debutFenetre.estVide()) {
nbColonnesVides += distAAvancer;
}
if (finFenetre.estVide()) {
nbColonnesVides -= distAAvancer;
}
for (int iEtirement=0; iEtirement<NB_ETIREMENTS; iEtirement++) { // HAUT, BAS, MILIEU
nbOccurencesEtirements[iEtirement][debutFenetre.nbEtirements(iEtirement)] += distAAvancer;
nbOccurencesEtirements[iEtirement][finFenetre.nbEtirements(iEtirement)] -= distAAvancer;
if (nbOccurencesEtirements[iEtirement][finFenetre.nbEtirements(iEtirement)] == 0) {
nbOccurencesEtirements[iEtirement].erase(finFenetre.nbEtirements(iEtirement));
}
}
}
}
return nbEtirementsMin;
}
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;
for (int nbDroite=0; nbDroite<=2*nbColonnes; nbDroite++) {
const ll nbEtapes = getNbEtirementsVerticauxMin(nbDroite) + nbDroite;
nbEtapesMin = min(nbEtapes, nbEtapesMin);
}
printf("%lld\n", nbEtapesMin);
return 0;
}
Compilation message (stderr)
cultivation.cpp: In function 'int main()':
cultivation.cpp:227: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:228:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &nbCellulesPleines);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:232: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... |