Cod sursa(job #2032123)

Utilizator LucaSeriSeritan Luca LucaSeri Data 4 octombrie 2017 16:37:43
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
#include <stack>
using namespace std;
stack<int> v;
int n, k;

ifstream f("transport.in");
ofstream g("transport.out");

bool verif(int x){
  stack <int> aux = v;
  int trans = 0;
  int sum = 0;
  while(aux.size()){
      int tmp = aux.top();
      aux.pop();
      if(tmp > x) return false;
      if(tmp + sum > x){
        trans ++;
        sum = tmp;
      }else sum += tmp;
  }

  return trans < k;
}
int main(){
    f >> n >> k;
    int best = 16000*16000 + 500;
    int step = 30;
    for(int i = 1; i <= n; ++i){
        int temp;
        f >> temp;
        v.push(temp);
    }

    for(; step >= 0; -- step){
      if(best - (1<<step) >= 0 && verif(best-(1<<step))){
          best -= 1<<step;
      }
    }

    g << best;
    return 0;
}