您的位置:首页 > 其它

poj3903 LIS的优化

2015-07-28 10:48 274 查看
题意:最长递增子序列

思路:二分优化 理解dp数组存的东西是关键
dp数组的值可能会更新

代码:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <list>
#include <map>
#include <set>
#define sss(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a) memset(a,0,sizeof(a))
#define ss(a,b) scanf("%d%d",&a,&b)
#define s(a) scanf("%d",&a)
#define INF 0x3f3f3f3f
#define w(a) while(a)
#define PI acos(-1.0)
#define LL long long
#define eps 10E-9
#define N 100010<<1
#define mod 100000000
using namespace std;
void mys(int& res)
{
int flag=0;
char ch;
while(!(((ch=getchar())>='0'&&ch<='9')||ch=='-'))
if(ch==EOF)  res=INF;
if(ch=='-')  flag=1;
else if(ch>='0'&&ch<='9')  res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')  res=res*10+ch-'0';
res=flag?-res:res;
}
void myp(int a)
{
if(a>9)
myp(a/10);
putchar(a%10+'0');
}
/*************************THE END OF TEMPLATE************************/
int arr[100010];
int dp[100001];
int sm[100010][30], bg[100010][30];
int getR(int R,  int cnt) {
int l = 1, r = R, m;
while(l <= r) {
m = (l + r) >> 1;
if(cnt > dp[m]) {
l = m + 1;
}
else r = m - 1;
}
return l;
}
int main(){
int n;
w(~s(n)){
for(int i=1; i<=n; i++) s(arr[i]);
int cnt = 0;
for(int i=1; i<=n; i++){
if(cnt == 0 ||arr[i] >dp[cnt])   dp[++cnt] = arr[i];
else{
int r = getR(cnt, arr[i]);
dp[r] = arr[i];
}
}
cout<<cnt<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: