IOI 2004 - Empodia

Author: Benjamin Qi

Table of Contents


Edit on Github

Official Editorial

Let denote the input sequence. An interval is framed if all three of the following conditions hold:

Our approach will be to iterate over all and check whether contributes a new empodio with right endpoint .

  • Maintain a stack consisting of all indices satisfying the second condition.
  • When we increment , repeatedly pop the top element of while . Then add to .
  • To check whether is part of an empodio, find the maximum such that . If is to the right of the rightmost left endpoint of any empodio found so far and , then we have found a new empodio.

To check whether , we can maintain a separate stack that stores all indices such that .

Note: The test data on Yandex has sequences of length greater than

1#include <bits/stdc++.h>
2using namespace std;
3
4using ll = long long;
5using ld = long double;
6using db = double;
7using str = string; // yay python!
8
9using pi = pair<int,int>;
10using pl = pair<ll,ll>;

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

Give Us Feedback on IOI 2004 - Empodia!