Блог пользователя tejustiwari

Автор tejustiwari, история, 6 лет назад, По-английски

Can Someone Please Tell Me Why is this Happening ?
Problem : 1238B - Kill `Em All
83162183 This Submission gives TLE
83162136 This Submission gets Accepted

The only difference in both the submissions is shown in the picture
i added one single line of code and got TLE
if i remove this green highlighted line of code the solution gets Accepted ,
Even if I am removing the if condition , the solution gets accepted 83161695

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by tejustiwari (previous revision, new revision, compare).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by tejustiwari (previous revision, new revision, compare).

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +11 Проголосовать: не нравится

One thing I realized is that the size of template is inversely proportional to the rating of person. At the end you only used basic stl and manipulation, so what was the need of such long template??

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

I'm not sure, but declaring int val[100005] = {} for every test case might cause TLE. I guess maybe modifying val means that it has to be reinitialized every time. There's no use for val anyways, since set automatically removes duplicates.

In general, try to avoid declaring large constant-length arrays for each test case.

  • »
    »
    6 лет назад, скрыть # ^ |
    ← Rev. 3  
    Проголосовать: нравится +4 Проголосовать: не нравится

    thanks qpwoeirut but have a look at the accepted solution in which i am doing the same . I am declaring int val[100005] = {} for every test case still the solution gets accepted .

    Using val[] { the highlighted part } inside the if condition is the reason behind TLE which I suppose is an O(1) operation and must not be the reason behind TLE but sadly it is the only reason

    • »
      »
      »
      6 лет назад, скрыть # ^ |
      ← Rev. 2  
      Проголосовать: нравится 0 Проголосовать: не нравится

      Here are some interesting observations:

      1. Changing val from int[] to bool[] gets AC even with val[a[i]] = 1 operation enabled.
      2. Declaring val global doesn't help.
      3. Most interestingly, commenting the if statement gets AC even with val[a[i]] = 1 operation enabled. 83166543

      It's some peculiar behaviour. It will be interesting to see if somebody can explain this.

    • »
      »
      »
      6 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      It's definitely not the operation costs.

      Below code gets AC and it does everything.

      fo(i,n)
                {
                  cin>>a[i];
                  if (val[a[i]] != 1) {   // Always check the condition
                      int x = 1, y = 2;
                      assert(x + y == 3);
                  }
                  s.insert(a[i]);     // Always insert a[i]
                  val[a[i]]=1;        // Always set val[a[i]]
                  maxi=max(maxi,a[i]);
                }
      

      Submission 83167312

      TLE occurs only if the statement val[a[i]] = 1 is inside the if (val[a[i]] != 1) condition and val is an int[] array;

»
6 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +2 Проголосовать: не нравится

If u change int val [ ] to bool val [ ] .. your solution will work.

your edited solution

my solution

found something : )

bool vs int comparison (time)

Which value is better to use? Boolean true or Integer 1?

»
6 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

If you do not modify elements of val[] array, the compiler knows its values (all zeroes btw) and optimizes code accordingly.

You can analyze it here: https://godbolt.org/z/NQBf32