Cod sursă (job #672330)

Utilizator avatar alex_benescu Benescu Alexandru alex_benescu IP ascuns
Problemă SCV (clasele 9-10) Compilator cpp-32 | 2,79 kb
Rundă Arhiva de probleme Status evaluat
Dată 28 oct. 2022 02:22:26 Scor 20
// #include <bits/stdc++.h>
#include <fstream>
// #include <iostream>
#define L 5e6 + 5
#define FAIL L + 1

// #define fin cin
// #define fout cout

using namespace std;

ifstream fin("scv.in");
ofstream fout("scv.out");
 
long long costNewSCV, timeNewSCV, productionSCV, goalProduction;
long long currentTime, currentSCV, minerals, totalSCV;
bool success;
 
void startProcess(long long numberOfSCV);
bool status_1();
bool status_2();
bool status_3();
bool status_4();
bool status_5();
bool status_6();
 
void startProcess(long long numberOfSCV){
  totalSCV = numberOfSCV;
  currentTime = minerals = 0;
  currentSCV = 1;

  if (!status_1()) {
    currentTime = FAIL;
  }
}
 
bool status_1(){
  if (currentSCV < totalSCV) {
    return status_2();
  } else {
    return status_3();
  }
}
 
bool status_2(){
  if (costNewSCV <= minerals) {
    minerals -= costNewSCV;
    return status_5();
  } 
  // else if (currentTime > L) {
  //   return status_6();
  // } else {
  //   currentTime++;
  //   minerals += currentSCV * productionSCV;
  // }

  long long deltaTime = (costNewSCV - minerals + currentSCV * productionSCV - 1) / (currentSCV * productionSCV);
  currentTime += deltaTime;
  minerals += deltaTime * currentSCV * productionSCV;
  if (currentTime > L) {
    return status_6();
  } else {
    minerals -= costNewSCV;
    return status_5();
  }
}
 
bool status_3(){
  if (goalProduction <= minerals) {
    return status_4();
  }

  long long deltaTime = (goalProduction - minerals + currentSCV * productionSCV - 1) / (currentSCV * productionSCV);
  currentTime += deltaTime;
  minerals += deltaTime * currentSCV * productionSCV;
  if (currentTime > L) {
    return status_6();
  } else {
    return status_4();
  }
}
 
bool status_4(){
  return true;
}
 
bool status_5(){
  minerals += currentSCV * productionSCV * timeNewSCV;
  currentTime += timeNewSCV;
  if (currentTime > L)
    return status_6();
  else{
    currentSCV++;
    return status_1();
  }
}
 
bool status_6(){
  return false;
}
 
int main(){
  long long le, ri, mid1, mid2, best, time1, time2;
  fin >> costNewSCV >> timeNewSCV >> productionSCV >> goalProduction;
  /**
  startProcess(3);
  cout << currentTime << "\n";
  **/
  /**/
  le = best = 1;
  ///ri = L;
  ri = goalProduction + 1;
  while (le <= ri){
    mid1 = le + (ri - le) / 3;
    mid2 = le + 2 * (ri - le) / 3;
 
    ///cout << "Process was started for " << mid1 << ".\n";
    startProcess(mid1);
    time1 = currentTime;
    ///cout << "CurrentTime is " << time1 << ".\n\n";
 
    ///cout << "Process was started for " << mid2 << ".\n";
    startProcess(mid2);
    time2 = currentTime;
    ///cout << "CurrentTime is " << time2 << ".\n\n";
 
    if (time1 < time2){
      best = time1;
      ri = mid2 - 1;
    }
    else{
      best = time2;
      le = mid1 + 1;
    }
  }
  fout << best << "\n";
  /**/
  return 0;
}