我发现The Hitchhiker's Guide to the Programming Contests中提到的算法(注意:此实现假定列表中没有重复项):
set<int> st;
set<int>::iterator it;
st.clear();
for(i=0; i<n; i++) {
st.insert(array[i]); it=st.find(array[i]);
it++; if(it!=st.end()) st.erase(it);
}
cout<<st.size()<<endl;
这是一种在 O(NlogN) 中找到最长递增子序列的算法。如果我尝试使用很少的测试用例来处理它,它似乎可以工作。但我仍然无法弄清楚它的正确逻辑。此外,它对我来说看起来并不那么直观。
谁能帮我深入了解为什么这个算法可以正常工作?