Cod sursa(job #641985)

Utilizator idomiralinIdomir Alin idomiralin Data 30 noiembrie 2011 10:49:55
Problema Transport Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
# include <cstdio>
# include <algorithm>

using namespace std;

int n, k, i, st, dr, sum, aux, aux1, ct, mij, aux2;
int a[16005]; 
int main()
{
    freopen("transport.in","r",stdin);
    freopen("transport.out","w",stdout);
    
    scanf("%d%d",&n,&k);
    
    for (i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
        sum += a[i];
        }
     
       st = 1; dr = sum; aux = sum;  
       
    while (ct != k)
    {
          ct = 1; aux1 = (st + dr) / 2;
          aux2 = aux1;  
         for (i = 1; i <= n; i++)
             if (a[i] <= aux1) aux1 -= a[i];
                          else 
                          {
                               ct++;
                               aux1 = (st + dr) / 2;
                               aux1 -= a[i];
                               }
         if (ct == k) break;                       
         if (ct > k) st = aux2 + 1;   
                else dr = aux2 - 1;
         }
         
    printf("%d", aux2);
    
return 0;
}