CF889E Mod Mod Mod
CF889E Mod Mod Mod
题意
题解
很有意思的一题啊。
首先我们想一下已经固定了
显然我们在序列中找到第一个比自己小的数进行操作。
取完模之后值域会变为前面的一半,所以最多只会跳
但这样并没有什么栾用,不好确定
设
这个
这样太难搞了,值域太大,而且没有什么特殊性质去做,很难办。
发现一个性质。
若
根据这个性质我们就可以推出另一个结论,一个比较有的初始值至少会到一个
现在计算答案的方式有些复杂,注意到答案可以写成
修改一下
这个的转移容易通过一个简单的增量得到,
这个状态结合之前得到的性质之后就有一个优雅的性质。
所以可以再次优化状态
根据第二个性质可以得到最优决策点,可得转移:
上面是直接转移,后面是找到满足
用 map
转移即可。
点击查看代码
#include <bits/stdc++.h>
#define int long long
#define rg register
#define pc putchar
#define gc getchar
#define pf printf
#define space pc(' ')
#define enter pc('\n')
#define me(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define FOR(i,k,t,p) for(rg int i(k) ; i <= t ; i += p)
#define ROF(i,k,t,p) for(rg int i(k) ; i >= t ; i -= p)
using namespace std ;
bool s_gnd ;
inline void read(){}
template<typename T,typename ...T_>
inline void read(T &x,T_&...p)
{
x = 0 ;rg int f(0) ; rg char c(gc()) ;
while(!isdigit(c)) f |= (c=='-'),c = gc() ;
while(isdigit(c)) x = (x<<1)+(x<<3)+(c^48),c = gc() ;
x = (f?-x:x) ;
read(p...);
}
int stk[30],tp ;
inline void print(){}
template<typename T,typename ...T_>
inline void print(T x,T_...p)
{
if(x < 0) pc('-'),x = -x ;
do stk[++tp] = x%10,x /= 10 ; while(x) ;
while(tp) pc(stk[tp--]^48) ; space ;
print(p...) ;
}
bool S_GND ;
const int N = 2e5+5 ;
int n,ans ;
int a[N] ;
map<int,int>f ;
signed main()
{
//cerr<<(double)(&s_gnd-&S_GND)/1024.0/1024.0 ;
// freopen(".in","r",stdin) ;
// freopen(".out","w",stdout) ;
read(n) ;
FOR(i,1,n,1) read(a[i]) ;
f[a[1]-1] = 0 ;
FOR(i,1,n-1,1)
{
for(auto it = f.lower_bound(a[i+1]) ; it != f.end() ; f.erase(it++))
{
auto [x,y] = *it ;
f[x%a[i+1]] = max(f[x%a[i+1]],y+(x-x%a[i+1])*i) ;
f[a[i+1]-1] = max(f[a[i+1]-1],y+((x+1)/a[i+1]*a[i+1]-a[i+1])*i) ;
}
}
for(auto [x,y]:f) ans = max(ans,x*n+y) ; print(ans) ;
return 0 ;
}
本文作者:snow-panther
本文链接:https://www.cnblogs.com/snow-panther/p/17630111.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步