0

問題の説明はこちらhttps://www.interviewstreet.com/challenges/dashboard/#problem/4fe12e4cbb829

You are distributing candy among children. All the children sit in a line and each of
them  has a rating score according to his or her usual performance. Each child gets 
at least 1 piece. Children get jealous of their immediate neighbors, so if two 
children sit next to each other then the one with the higher rating must get 
more candies. You wants to save money, so minimize the total number of candies.

Input: A file with the children's ratings, 1 per line.

そして、私のコードは小さな入力サイズで機能していますが、非常に大きな入力では、(interview.comコンソールで)出力が得られないか、システムで回答から少し外れています。私のコードのエラーはどのくらい寒いですか?

#include <stdio.h>

int main() 
{
    int i, j, n, rating[100000], candy[1000000], total = 0;

    scanf("%d", &n);

    for (i=0 ; i < n; i++) 
    {
        scanf("%d",&rating[i]);
        candy[i] = 1;
    }

    for (i=0 ; i < n+1 ; i++)
    {
      for (j=1 ; j < n-1; j++)
      {
          if( rating[j-1] < rating[j] )
          {
              if ( candy[j-1] >= candy[j] )
                  candy[j]++;
          }

          if( rating[j+1] < rating[j] )
          {
              if ( candy[j+1] >= candy[j] )
                  candy[j]++;
          }       
      }  
    }

    for (i=0 ; i < n ; i++)
        total = total + candy[i];

    printf("%d",total);

    return 0;
}
4

1 に答える 1

0

rating自動配列とを使用して、実装の変換制限の1つに遭遇している可能性がありますcandy。それらをファイルスコープ、つまり。の外に配置してみてくださいmain。次に、静的ストレージ期間があります。ほとんどのシステムでは、自動オブジェクトよりもはるかに大きな静的オブジェクトを使用できます。

于 2012-09-02T10:21:56.340 に答える