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

Автор Zzyzx, 12 лет назад, По-английски

Spoiler Alert: This is regarding problem D of the latest Codeforces round (242).

Link to problem statement: http://codeforces.me/contest/424/problem/D

I have two solutions, Let's call them sol-1 and sol-2

Link to sol-1: http://codeforces.me/contest/424/submission/6472455

Link to sol-2: http://codeforces.me/contest/424/submission/6472637

In sol-1 I use a struct node

struct node {
  int tot, col;
  bool operator <(const node &rhs) const {
    return tot < rhs.tot;
  }
};

In sol-2 I use std::pair <int, int> with the necessary changes to the remaining part of the code.

In sol-1 I get TLE on test case 17.

But In sol-2 I get WA on test case 3.

How is that possible?! Can someone please help me out?

UPD: Found my mistake. Thank you so much Igorjan94 and andreyv

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

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

For your struct operator < returns only

lhs.tot < rhs.tot

But for std::pair<int, int> the corresponding operator will return

lhs.first != rhs.first ? lhs.first < rhs.first : lhs.second < rhs.second
»
12 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится +8 Проголосовать: не нравится

because std::pair operator< is

template <class T1, class T2>
bool operator< (const pair<T1,T2>& lhs, const pair<T1,T2>& rhs)
{ 
    return lhs.first<rhs.first || (!(rhs.first<lhs.first) && lhs.second<rhs.second); 
}

your solution, but accepted 6474171

difference is in lower/upper_bound. You used this, and set doesn't have random access iterator so works linear time, but I changed it to this and it works in logarithmic time