ENG  RUSTimus Online Judge
Online Judge
Задачи
Авторы
Соревнования
О системе
Часто задаваемые вопросы
Новости сайта
Форум
Ссылки
Архив задач
Отправить на проверку
Состояние проверки
Руководство
Регистрация
Исправить данные
Рейтинг авторов
Текущее соревнование
Расписание
Прошедшие соревнования
Правила
вернуться в форум

Обсуждение задачи 1021. Таинство суммы

O(n0 + n1) solution (ie: no hashing or binary search)
Послано Alexandru Mosoi 12 мар 2003 01:40
Just check the my source code to figure it out how I did it. If you
consider yourself a lamer just copy & paste it into the submit form.

#include <stdio.h>
#include <stdlib.h>

int n, m;
int t[50000];

int main(void)
{
    int found = 0, pos = 0;

    scanf("%d", &n);
    for(int i = 0; i < n; i ++) scanf("%d", &t[i]);

    scanf("%d", &m);
    for(int i = 0; i < m && !found; i ++)
    {
        int tmp;
        scanf("%d", &tmp);

        while(tmp + t[pos] < 10000) pos ++;
        found |= tmp + t[pos] == 10000;
    }

    if(found) printf("YES\n");
    else printf("NO\n");
    return 0;
}
Re: O(n0 + n1) solution (ie: no hashing or binary search)
Послано cearny (Adrian Cearnau) 19 мар 2003 17:36
Hey Alex!

Here's mine, slightly faster - O( input size ), but uses more memory
(the traditional trade-off):

#include <stdio.h>

/* Bigger than needed, but oh well :) */
char used_nums[66000];
/* This one will point at the middle of the
 * above vector so that we can index negative
 * numbers. */
char *used;

int main() {
  int i, j, n;

  /* Make it point at the middle of the vector. */
  used = &used_nums[ 33000 ];

  scanf( "%d", &n );

  /* Mark off each number in the first list. */
  for( i = 0; i < n; i++ ) {
    scanf( "%d", &j );
    used[ j ] = 1;
  }

  scanf( "%d", &n );

  /* For each number in the second list... */
  for( i = 0; i < n; i++ ) {
    scanf( "%d", &j );

    /* Check if the corresponding number has been marked off... */
    if( 10000 - j > -32769 && 10000 - j < 32768 && used[ 10000 -
j ] ) {
      printf( "YES" );
      return;
    }
  }

  printf( "NO" );
}

/* Coded while listening to Dio & Yngwie Malmsteen - Dream On
(tribute to Aerosmith) */
How much time does that program take?
Послано Igor Dex 28 май 2003 02:50
> Just check the my source code to figure it out how I did it. If you
> consider yourself a lamer just copy & paste it into the submit form.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int n, m;
> int t[50000];
>
> int main(void)
> {
>     int found = 0, pos = 0;
>
>     scanf("%d", &n);
>     for(int i = 0; i < n; i ++) scanf("%d", &t[i]);
>
>     scanf("%d", &m);
>     for(int i = 0; i < m && !found; i ++)
>     {
>         int tmp;
>         scanf("%d", &tmp);
>
>         while(tmp + t[pos] < 10000) pos ++;
>         found |= tmp + t[pos] == 10000;
>     }
>
>     if(found) printf("YES\n");
>     else printf("NO\n");
>     return 0;
> }
>
>
It would be faster , if...
Послано Igor Dex 28 май 2003 04:30
 if( 10000 - j > -32769 ...   is always true

Wrote while listening to "Tatoo" - "Do not trust, do not be afraid,
do not ask" ;-)
Re: O(n0 + n1) solution (ie: no hashing or binary search)
Послано ^[o_O]^ 7 фев 2006 18:50
you realize that this isn't O(input size), right?

Edited by author 07.02.2006 18:50