P1970 花匠
题意 求最长抖动序列 我们用LIS的方法类似容易列出方程 f[i][0]代表以i结尾下降的长度,
f[i][1]代表以i结尾上升的长度
初始f[1][0] = f[1][1] = 1
然后开始类似LIS的方法找 O(n^2)
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
int f[100025][2],arr[100025];
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,ans = 0;
scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]),f[i][0] = f[i][1] = 1;
for(int i = 2;i<=n;++i)
{
for(int j = i-1;j>=1;--j)
{
if(arr[i]>arr[j])
{
f[i][1] = max(f[i][1],f[j][0]+1);
}
if(arr[i]<arr[j])
{
f[i][0] = max(f[i][0],f[j][1]+1);
}
if(f[i][0]!=1&&f[i][1]!=1) break;
}
}
printf("%d\n",max(f[n][0],f[n][1]));
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
因为小break条件 竟然能过 n是1e5
我们考虑优化 其实可以用线段树维护那么就是nlogn的 完全能过
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
#define N 1000010
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 100025;
int arr[MAX_N],maxx[N<<2][2];
int Max(int a,int b){if(a>b) return a;return b;}
void up(int rt,int v)
{
maxx[rt][v] = Max(maxx[rt<<1][v],maxx[rt<<1|1][v]);
}
void build(int rt,int l,int r,int v)
{
maxx[rt][v] = 0;
if(l==r)
{
return ;
}
build(rt<<1,l,mid,v);
build(rt<<1|1,mid+1,r,v);
}
void update(int rt,int l,int r,int x,int v,int V)
{
if(l==r)
{
maxx[rt][V] = v;
return ;
}
if(x<=mid) update(rt<<1,l,mid,x,v,V);
else update(rt<<1|1,mid+1,r,x,v,V);
up(rt,V);
}
int query(int rt,int l,int r,int x,int y,int V)
{
if(x<=l&&r<=y)
{
return maxx[rt][V];
}
if(y<=mid) return query(rt<<1,l,mid,x,y,V);
else if(x>mid) return query(rt<<1|1,mid+1,r,x,y,V);
else return Max(query(rt<<1,l,mid,x,y,V),query(rt<<1|1,mid+1,r,x,y,V));
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n,ans=0;
scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]),arr[i]+=5;
build(1,1,N,0);build(1,1,N,1);
for(int i = 1;i<=n;++i)
{
int maxx1 = query(1,1,N,1,arr[i]-1,0) + 1;
int maxx2 = query(1,1,N,arr[i]+1,N,1) + 1;
update(1,1,N,arr[i],maxx2,0);
update(1,1,N,arr[i],maxx1,1);
ans = Max(ans,Max(maxx1,maxx2));
}
printf("%d\n",ans);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}
这题跟别人学是有O(n)的方法的
设dp[i]为i为结尾上升的长度 dp_[i]为i为结尾下降的长度
那么
如果 arr[i]>arr[i-1] dp[i] = Max(dp[i-1],dp_[i-1]+1),dp_[i] = dp_[i-1];
如果 arr[i]==arr[i-1] dp[i] = dp[i-1],dp_[i] = dp_[i-1];
如果 arr[i]<arr[i-1] dp_[i] = Max(dp_[i-1],dp[i-1]+1),dp[i] = dp[i-1];
O(n)解决
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 100025;
int dp[MAX_N],dp_[MAX_N],arr[MAX_N];
int Max(int a,int b) {if(a>b) return a;return b;}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
dp[1] = dp_[1] = 1;
int n;scanf("%d",&n);
for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
for(int i = 2;i<=n;++i)
{
if(arr[i]>arr[i-1]) dp[i] = Max(dp[i-1],dp_[i-1]+1),dp_[i] = dp_[i-1];
else if(arr[i]==arr[i-1]) dp[i] = dp[i-1],dp_[i] = dp_[i-1];
else if(arr[i]<arr[i-1]) dp_[i] = Max(dp_[i-1],dp[i-1]+1),dp[i] = dp[i-1];
}
printf("%d\n",Max(dp[n],dp_[n]));
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}