题目:https://vjudge.net/problem/UVA-1471
题意:
lrj思路:第一思路可以暴力枚举,先枚举i(i=1:len),i往右数,看以i开头的最大升序序列个数。然后再枚举j(j = 0:i),由于要将i右边的区间和j左边的区间连起来,枚举时要满足a[j]<a[i]。每次j都要往左数,看以j结尾的最大升序序列个数。总的复杂度为n^3。
改进1:可以提前用n的复杂度将 ”i开头的最大升序序列个数“和”j结尾的最大升序序列个数“记录到数组f和g中。这样总的复杂度变为n^2(超时).
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<stack>
#include<sstream>
using namespace std;
const int maxn = 200005;
int a[maxn],g[maxn],f[maxn];
struct node {
int a, f, g;
node(int a = 0, int f = 0, int g = 0) :a(a), f(f), g(g) {};
bool operator < (node n) const
{
return a < n.a;
}
};
int main() {
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
//g[i]:以i结尾的最长连续升序数长度
g[0] = 1;
for (int i = 1; i < n; i++)
{
if (a[i] > a[i - 1])
g[i] = g[i - 1] + 1;
else
g[i] = 1;
}
//f[i]:以i开始的最长连续升序数长度
f[n - 1] = 1;
for (int i = n - 2; i >= 0; i--)
{
if (a[i + 1] > a[i])
f[i] = f[i + 1] + 1;
else
f[i] = 1;
}
int ans = 1;
for (int i = 1; i < n; i++)
{
for (int j = 0; j < i; j++)
{
if (a[j] < a[i])
{
ans = max(ans, g[j] + f[i]);
}
}
}
cout << ans << endl;
}
return 0;
}
巧妙的进一步改进:上一步说到用数组g和f分别记录,不妨用结构体(a[j],g[j])表示以a[j]结尾的最大升序序列个数。用贪心的思想可以发现一个问题:当a[j] < a[j']时,如果g[j] >= g[j']时,此时(a[j'],g[j'])可以完全被(a[j],g[j])代替。因为a[j] < a[j'],说明a[j]肯定更容易和后面的a[i]连接起来,而且所构成的区间长度不会减少!!!按照这种思路可以抛弃一些结构体,使得最后结构体随着a[j]升序排列,对应的区间g[j]也是升序排列的。注意g[j]也是升序排列的,这点很重要。这样的话,要找到左边可以和a[i]连接起来的最大区间,可以二分查找出离a[i]最近的那个a[j]。然后g[j]+f[i]就是当前最长区间。
注意:当插入a[i]时,会利用lower_bound进行二分查找到>=a[i]的下标idx,此下标减1,即--idx即为小于a[i]中最接近a[i]的数。需要注意这种特殊情况:idx处的a[idx]可能等于a[i],此时插入a[i]会覆盖a[idx]。但是不要紧,因为此时g[idx]一定比g[--idx]大1,所以,如果g[i]>g[--idx],那么g[i]>=g[idx],不会影响单调性
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<map>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<stack>
#include<sstream>
using namespace std;
const int maxn = 200005;
int a[maxn], f[maxn], g[maxn], vis[maxn];
int t, n;
struct node {
int a, g, f;
node(int a, int g, int f) :a(a), g(g), f(f) {};
bool operator <(const node& nd) const {
return a < nd.a;
}
};
int main()
{
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
g[0] = 1;
for (int i = 1; i < n; i++)
if (a[i] > a[i - 1])
g[i] = g[i - 1] + 1;
else
g[i] = 1;
f[n - 1] = 1;
for (int i = n - 2; i >= 0; i--)
if (a[i] < a[i + 1])
f[i] = f[i + 1] + 1;
else
f[i] = 1;
set<node> s;
int ans = 1;
for (int i = 0; i < n; i++) {
node nd(a[i], g[i], f[i]);
int keep = 1;
set<node>::iterator it = s.lower_bound(nd);
if (it != s.begin()) {
node temp = *(--it);
ans = max(ans, temp.g + nd.f);
if (nd.g <= temp.g)
keep = 0;
}
if (keep) {
s.erase(nd);
s.insert(nd);
it = s.find(nd);
it++;
while (it!=s.end() && nd.g >= it->g) {
s.erase(it++);
}
}
}
cout << ans << endl;
/*int ans = 1;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (a[i] < a[j])
ans = max(ans,g[i] + f[j]);
else
ans = max(max(ans, g[i]), f[i]);
}
}
cout << ans<<endl;*/
}
return 0;
}