Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is the O(n*lg(n)) solution from <a href="http://comscigate.com/Books/contests/icpc.pdf" rel="noreferrer">The Hitchhiker’s Guide to the Programming Contests</a> (note: this implementation assumes there are no duplicates in the list):</p> <pre class="lang-cpp prettyprint-override"><code>set&lt;int&gt; st; set&lt;int&gt;::iterator it; st.clear(); for(i=0; i&lt;n; i++) { st.insert(array[i]); it=st.find(array[i]); it++; if(it!=st.end()) st.erase(it); } cout&lt;&lt;st.size()&lt;&lt;endl; </code></pre> <p>To account for duplicates one could check, for example, if the number is already in the set. If it is, ignore the number, otherwise carry on using the same method as before. Alternatively, one could reverse the order of the operations: first remove, then insert. The code below implements this behaviour:</p> <pre class="lang-cpp prettyprint-override"><code>set&lt;int&gt; st; set&lt;int&gt;::iterator it; st.clear(); for(int i=0; i&lt;n; i++) { it = st.lower_bound(a[i]); if (it != st.end()) st.erase(it); st.insert(a[i]); } cout&lt;&lt;st.size()&lt;&lt;endl; </code></pre> <p>The second algorithm could be extended to find the longest increasing subsequence(LIS) itself by maintaining a parent array which contains the position of the previous element of the LIS in the original array.</p> <pre class="lang-cpp prettyprint-override"><code>typedef pair&lt;int, int&gt; IndexValue; struct IndexValueCompare{ inline bool operator() (const IndexValue &amp;one, const IndexValue &amp;another){ return one.second &lt; another.second; } }; vector&lt;int&gt; LIS(const vector&lt;int&gt; &amp;sequence){ vector&lt;int&gt; parent(sequence.size()); set&lt;IndexValue, IndexValueCompare&gt; s; for(int i = 0; i &lt; sequence.size(); ++i){ IndexValue iv(i, sequence[i]); if(i == 0){ s.insert(iv); continue; } auto index = s.lower_bound(iv); if(index != s.end()){ if(sequence[i] &lt; sequence[index-&gt;first]){ if(index != s.begin()) { parent[i] = (--index)-&gt;first; index++; } s.erase(index); } } else{ parent[i] = s.rbegin()-&gt;first; } s.insert(iv); } vector&lt;int&gt; result(s.size()); int index = s.rbegin()-&gt;first; for(auto iter = s.rbegin(); iter != s.rend(); index = parent[index], ++iter){ result[distance(iter, s.rend()) - 1] = sequence[index]; } return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload