Jeon_Jungkook's blog

By Jeon_Jungkook, history, 18 months ago, In English

Hello every one :) Today I saw an interesting thing this code 36189750 got accepted for the problem A — Cut Ribbon while this code 36189530 did not. so im wondering what is the difference between memset and fill can anybody explain it to me. because they really look like the same thing to me so I would be grateful if some one tells the difference. Thank you for your help. UPD: also sorry for my bad English typing this is my first blog entries.

 
 
 
 
  • Vote: I like it
  • -24
  • Vote: I do not like it

»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it

1) In the second submission 'fill(dp, dp + n, -100);' fills only positions between 0 and n-1, while in the first one it fills whole array.

2) Function memset works only when you fill array with 0, 1 or -1.

  • »
    »
    18 months ago, # ^ |
      Vote: I like it +31 Vote: I do not like it

    The function memset only works with 1 if you are using an array of bool/char (or other 1-byte data type).

    We usually use it like memset(A, x, sizeof(A)), where A is an array of something. This converts the value x to an unsigned char and then copies it to the first sizeof(A) bytes of the memory address indicated by A (the start of the array).

    The representation of 0 is 00000000, and the representation of -1 is 11111111, and that's why it works with 0 and -1 with integers/long long too, but the representation of 1 is 00000001, so if you want to memset an array of integers to 1, you are going to end up with an array of integers equal to 00000001 00000001 00000001 00000001 = 16843009, you can actually test it out with this snippet:

    int a[100];
    memset(a, 1, sizeof(a));
    forn(i, 100){
        printf("%d\n", a[i]);
    }
    
»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it
  1. You fill elements between 0 and n-1 but you have loop condition "j <= n"

2.-100 is not enough. Imagine n = 4000 and a = 3. dp table will look like this: 0 -100 -100 1 -99 -99 2 -98 -98 and so on. It will eventually get over 0 which will result in wrong answer. Memset works because you can't assign -100 using it(on my pc I get -1667457892 when i use it the way you used it, and this number is pretty much enough)

»
18 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

hey , modify your fill function to this fill(dp,dp+n+1, INT_MIN); . here's your modified code AC solution 57905794

»
18 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I answered the same question a while ago ffs...