Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么两个代码感觉只是时间上会稍微有点差别,却一个是WA,一个是AC,而不报TLE?????

Posted by nuciedh at 2007-02-13 13:32:19 on Problem 3104
In Reply To:WA了,谁帮忙看看 ! Posted by:nuciedh at 2007-02-13 12:22:05
这个WA:
 #include <iostream>
 #include <fstream>
 const int N = 100010;
 
 using namespace std;
 
 int n, k, a[N];
 
 int attempt (int l, int r)
 {
     int i, b, t, s;
     int tmp;
     if (l < r)
     {
           s = 0;
           t = (l + r) / 2;
           for (i = 0; i < n; i ++)
           {
               tmp = a[i];
               tmp -= t;
               if (tmp > 0)
               {
                   s += (tmp + k - 2) / (k - 1);
               }
           }
           if (s <= t)
           {
                 b = attempt (l, t);
           }
           else
           {
               b = attempt (t + 1, r);
           }
           return b;
     }
     else
     {
         return r;
     }
 }
 
 void solve ()
 {
      int i, tmp;
      for (tmp = 0, i = 0; i < n; i ++)
      {
          scanf ("%d", &a[i]);
          if (a[i] > tmp)
          {
                   tmp = a[i];
          }
      }
      scanf ("%d", &k);
      if (k > 1)
      {
           printf ("%d\n", attempt (1, tmp));
      }
      else
      {
          printf ("%d\n", tmp);
      }
 }
 
 int main ()
 {
     while (EOF != scanf ("%d", &n))
     {
           solve ();
     }
     return 0;
 }

这个AC:
#include <iostream>
#include <fstream>
const int N = 100010;

using namespace std;

int n, k, a[N];

int attempt (int l, int r)
{
    int i, b, t, s;
    int tmp;
    if (l < r)
    {
          s = 0;
          t = (l + r) / 2;
          for (i = 0; i < n; i ++)
          {
              tmp = a[i];
              tmp -= t;
              if (tmp > 0)
              {
                  s += tmp / (k - 1);
                  if ((tmp % (k - 1)) != 0)
                  {
                          s ++;
                  }
                  if (s > t)
                  {
                        break;
                  }
              }
          }
          if (i >= n)
          {
                b = attempt (l, t);
          }
          else
          {
              b = attempt (t + 1, r);
          }
          return b;
    }
    else
    {
        return r;
    }
}

void solve ()
{
     int i, tmp;
     for (tmp = 0, i = 0; i < n; i ++)
     {
         scanf ("%d", &a[i]);
         if (a[i] > tmp)
         {
                  tmp = a[i];
         }
     }
     scanf ("%d", &k);
     if (k > 1)
     {
          printf ("%d\n", attempt (1, tmp));
     }
     else
     {
         printf ("%d\n", tmp);
     }
}

int main ()
{
    while (EOF != scanf ("%d", &n))
    {
          solve ();
    }
    return 0;
}















Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator