Cod sursa(job #1007042)

Utilizator Viva12Ferentz Sergiu Viva12 Data 8 octombrie 2013 02:27:56
Problema Transport Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream fin ("transport.in");
ofstream fout ("transport.out");
int n,k;
int minV = -2,maxV = 0;
queue<int> s;
void scan(){

  fin >> n >> k;
  for(int i =0 ; i < n ; i++){
      int x;
      fin >> x;
      if(x > minV)
	minV = x;
      
      maxV += x;
      s.push(x);
  }
  
  
}

bool isValueOk(int value){

  int currentCap =0;
  int currentK = k;
  queue<int> auxS = s;
  int currentItem;
  while(!auxS.empty()){

    
    if (currentK == 0)
	break;
      currentItem = auxS.front();
     if(currentCap + currentItem <= value)
     {
       currentCap+= currentItem;
       auxS.pop();
       
    }
    else
      {
	
	currentK --;
	currentCap = 0;
      }
  
    
    
  }
  if(auxS.empty() == true){
    return true;
  }
  return false;
  
  
}
int rightValue;
int binarySearch(int min,int max){

  while ( max > min){
    //cout << max << " " << min<< endl;
    int mid = (max+min) >> 1;
    if(isValueOk(mid) == true)
    {
      max = mid;
    }
    else
    {
      min = mid+1;
    }
    
    
  }
  return max;
  
}
int main(){
  
    scan();
    fout<<binarySearch(minV,maxV) << endl;
    
    return 0;
}