O(nlogn)做法即贪心+二分,dp数组里存的是最长的不上升序列。
第二问求需要多少系统,这个可以参考狄尔沃斯定理。
dilworth
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
vector<int> a;
int res = 1;
void dp1()
{
int dp[N];
int len = 0;
dp[0] = a[0];
for (int j = 1; j < a.size(); j++)
{
if (dp[len] >= a[j])
dp[++len] = a[j];
else
{
int pos = upper_bound(dp, dp + len, a[j], greater<int>()) - dp;
dp[pos] = a[j];
}
}
cout << len + 1 << endl;
}
void dp2()
{
int dp[N];
int len = 0;
dp[0] = a[0];
for (int j = 1; j < a.size(); j++)
{
if (dp[len] < a[j])
dp[++len] = a[j];
else
{
int pos = lower_bound(dp, dp + len, a[j]) - dp;
dp[pos] = a[j];
}
}
cout << len + 1 << endl;
}
void run()
{
int x;
while (cin >> x)
a.push_back(x);
dp1();
dp2();
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
run();
return 0;
}