UVa 481 - What Goes Up
// Accepted
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#define sf scanf
#define pf printf
#define mx4 1000100
using namespace std;
typedef long long LL;
LL seq[mx4], lisseq[mx4], L[mx4], I[mx4];
const long long inf = 2147483648;
LL Binary_Search(LL lo, LL hi, LL itm)
{
LL mid;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if(I[mid] < itm)
{
lo = mid+1;
}
else
{
hi = mid-1;
}
}
return lo;
}
LL LIS(int n)
{
int i , Llen=0;
LL r , len=0 ;
for(i=0; i<n; i++)
{
r = Binary_Search(0, len, seq[i]);
I[r] = seq[i];
L[Llen++] = r;
if(len < r)
{
len = r;
}
} //cout << Llen;
return len;
}
void find_sequence(LL mxlen , int n)
{
int i = 0, j;
for(j=1; j<n; j++)
{
if(L[j] > L[i])
{
i = j;
}
}
//cout << i;
int top = L[i];
top--;
lisseq[top] = seq[i];
for(j=i-1; j>=0; j--)
{
if(seq[i] > seq[j] and L[j] == L[i] - 1)
{
i = j;
top--;
lisseq[top] = seq[i];
}
}
for(i=0;i<mxlen;i++)
{
pf("%lld\n" , lisseq[i]);
}
}
int main()
{
//freopen("input.txt" , "r" , stdin);
int i , ilen=0 ;
I[ilen++] = -inf;
for(i=0; ; i++)
{
if(sf("%lld",&seq[i]) != 1)
{
break;
}
I[ilen++] = seq[i];
}
LL lislength = LIS(i);
//pf("LIS Length=");
pf("%lld\n-\n",lislength);
find_sequence(lislength , i);
return 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#define sf scanf
#define pf printf
#define mx4 1000100
using namespace std;
typedef long long LL;
LL seq[mx4], lisseq[mx4], L[mx4], I[mx4];
const long long inf = 2147483648;
LL Binary_Search(LL lo, LL hi, LL itm)
{
LL mid;
while(lo <= hi)
{
mid = (lo + hi) / 2;
if(I[mid] < itm)
{
lo = mid+1;
}
else
{
hi = mid-1;
}
}
return lo;
}
LL LIS(int n)
{
int i , Llen=0;
LL r , len=0 ;
for(i=0; i<n; i++)
{
r = Binary_Search(0, len, seq[i]);
I[r] = seq[i];
L[Llen++] = r;
if(len < r)
{
len = r;
}
} //cout << Llen;
return len;
}
void find_sequence(LL mxlen , int n)
{
int i = 0, j;
for(j=1; j<n; j++)
{
if(L[j] > L[i])
{
i = j;
}
}
//cout << i;
int top = L[i];
top--;
lisseq[top] = seq[i];
for(j=i-1; j>=0; j--)
{
if(seq[i] > seq[j] and L[j] == L[i] - 1)
{
i = j;
top--;
lisseq[top] = seq[i];
}
}
for(i=0;i<mxlen;i++)
{
pf("%lld\n" , lisseq[i]);
}
}
int main()
{
//freopen("input.txt" , "r" , stdin);
int i , ilen=0 ;
I[ilen++] = -inf;
for(i=0; ; i++)
{
if(sf("%lld",&seq[i]) != 1)
{
break;
}
I[ilen++] = seq[i];
}
LL lislength = LIS(i);
//pf("LIS Length=");
pf("%lld\n-\n",lislength);
find_sequence(lislength , i);
return 0;
}
Comments
Post a Comment