Cod sursa(job #641993)

Utilizator idomiralinIdomir Alin idomiralin Data 30 noiembrie 2011 11:38:46
Problema Transport Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
# include <cstdio>

using namespace std;

int n, k, i, j,  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 = aux2;
                               aux1 -= a[i];
                               }
         if (ct == k) break;                       
         if (ct > k) st = aux2 + 1;   
                else dr = aux2 - 1;
         }
         
         for (i = aux2; ct == k; --i)
         {
             ct = 1; aux1 = i;
             for (j = 1; j <= n; j++)
             if (a[j] <= aux1) aux1 -= a[j];
                          else 
                          {
                               ct++;
                               aux1 = i;
                               aux1 -= a[j];
                               }    
             if (ct != k) aux2 = i + 1;                   
         }
          
             
    printf("%d", aux2);
    
return 0;
}