题目
题意: 给定n个数,要求用魔法把n个数全都变成1。魔法:将一段连续区间的数ai变成sqrt(a[i]/2+1)
思路: 一个数最多施展魔法6次即可,可以预处理出每个数变到1的路径。实质是找到两个数之间的LCDS,最长公共下降子序列,只计算开头那个数的花费,其他数的花费记为0。但是我们发现,如果两个数的路径上只要有一个数相同,那么后边的数就会全都相同,因为变换的函数都一样的。所以只要从大到小比对两个数的变换路径,找到第一个相同的数,之后的数都不会产生贡献。
时间复杂度: O(n)
代码:
// Problem: 最长公共上升子序列
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/274/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
typedef long long ll;
ll a[N];
vector<ll> va[N];
int n,m,k,T;
ll ans = 0;
ll fun(ll x)
{
return sqrt(x/2+1);
}
void solve()
{
cin>>n;
for(int i=1;i<=n;++i)
{
cin>>a[i];
bool flag = 0;
while(a[i] > 1)
{
if(!flag)
for(auto j:va[i-1])
{
if(a[i] == j)
{
flag = 1;
break;
}
}
if(!flag) ans++;
va[i].push_back(a[i]);
a[i] = fun(a[i]);
}
}
cout<<ans;
}
signed main(void)
{
solve();
return 0;
}