#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> using namespace std; const int maxn = 100000; //---------------------------- class element { public: int value; int index;}; //---------------------------- element a[maxn],lis[maxn]; int trace[maxn],ans[maxn]; int n; //---------------------------- int BSearch(int i, int j, int x) { int l,r,m; l = i; r = j; if (r<l) return(-1); do { m = (l+r)/2; if (lis[m].value>=x) r = m; else l = m; } while (r-l>1); if (lis[l].value>=x) return(l); else if (lis[r].value>=x) return(r); else return(-1); } //---------------------------- int solve() { int result = 0; for (int i=1; i<=n; i++) { int pos = BSearch(1,result,a[i].value); if (pos!=-1) { lis[pos] = a[i]; if (pos==1) trace[i] = -1; else trace[i] = lis[pos-1].index; } else { lis[++result] = a[i]; if (result==1) trace[i] = -1; else trace[i] = lis[result-1].index; } } int cnt = 0, x = lis[result].index; while (trace[x]!=-1) { ans[++cnt] = a[x].value; x = trace[x]; } ans[++cnt] = a[x].value; return(result); } //---------------------------- int main() { n = 0; int result; while (!cin.eof()) { cin >> a[++n].value; a[n].index = n; } result = solve(); cout << result << endl << "-" << endl; for (int i=result; i>=1; i--) cout << ans[i] << endl; return 0; }
Monday, August 18, 2014
UVA - 481 - What Goes Up
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment