Cod sursa(job #641991)

Utilizator idomiralinIdomir Alin idomiralin Data 30 noiembrie 2011 11:30:21
Problema Transport Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.92 kb
# include <cstdio>
# include <algorithm>

using namespace std;

int n, k, i, j, ok, st, dr, sum, aux, aux1, mij, aux2;
int a[16005], ct[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;  
       ct[aux2] = 0;
    while (ct[aux2] != k)
    {
          aux1 = (st + dr) / 2;
          aux2 = aux1;  
          ct[aux2] = 1;
         
         for (i = 1; i <= n; i++)
         {
             if (a[i] <= aux1) aux1 -= a[i];
                          else 
                          {
                               aux1 = aux2;
                               if (a[i] <= aux1)
                               aux1 -= a[i];
                               else
                               {
                                   ct[aux2] = 100000;
                                   break;
                                   }
                               ct[aux2]++;
                               }
              }                 
         if (ct[aux2] == k) break;                       
         if (ct[aux2] > k) st = aux2 + 1;   
                else dr = aux2 - 1;
         }
         
         for (i = aux2; ct[i] == k; --i);
         
         /*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", i + 1);
    
return 0;
}