Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #define dim 100001
- using namespace std;
- ifstream fin("scmax.in");
- ofstream fout("scmax.out");
- int n,v[dim];
- int sc[dim],urm[dim],len;
- void readData()
- {
- fin>>n;
- for (int i=1; i<=n; ++i)
- fin>>v[i];
- }
- int binarySearch(int x)
- {
- int i,step;
- for (step=1; step<len; step<<=1);
- for (i=0; step; step>>=1)
- if (i+step<=len && v[sc[i+step]]<x) i+=step;
- return i;
- }
- void printRec(int x)
- {
- if (urm[x]) printRec(urm[x]);
- fout<<v[x]<<' ';
- }
- void dp()
- {
- sc[1]=1;
- len=1;
- for (int i=2; i<=n; ++i)
- {
- int x=v[i];
- int poz=binarySearch(x);
- if (poz==len) sc[++len]=i;
- else if (v[sc[poz+1]]>x) sc[poz+1]=i;
- urm[i]=sc[poz];
- }
- fout<<len<<'\n';
- printRec(sc[len]);
- }
- int main()
- {
- readData();
- dp();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement