The Heaviest Non-decreasing Subsequence Problem
解题心得
- 这个题就是一个简单的动态规划,非递减最长子序列的改版(加一个权重),只要把权重为5的改成5个权重为1的然后dp就可以解决了,注意要用nlogn的复杂度才可以。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+100;
typedef long long ll;
ll dp[maxn],num[maxn],w[maxn];
ll get_ans(ll t)
{
ll len = 0;
ll sum = 0;
for(int i=0;i<t;i++)
{
if(dp[len] <= num[i])
{
dp[++len] = num[i];
sum += w[i];
}
else
{
int pos = upper_bound(dp,dp+len,num[i]) - dp;
dp[pos] = num[i];
}
}
return sum;
}
int main()
{
ll now;
ll t = 0;
while(scanf("%lld",&now) != EOF)
{
if(now < 0)
continue;
else if(now < 10000)
{
num[t] = now;
w[t++] = 1;
}
else
{
int Now = now - 10000;
for(int j=0;j<5;j++)
{
num[t] = Now;
w[t++] = 1;
}
}
}
ll ans = get_ans(t);
printf("%lld
",ans);
return 0;
}