博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
魔法序列-upc
阅读量:280 次
发布时间:2019-03-01

本文共 1533 字,大约阅读时间需要 5 分钟。

题目描述

小E为了完成公主的任务,需排布魔法阵,从中获得法力。
简单起见,魔法阵可以看成一个长度为n的序列。序列从左到右都摆放了一张符卡,符卡有一个强度ai。法术的释放要每个元素相互配合,取得共鸣效果。一个由一些符卡组成的咒语的魔力值为这个咒语中所有符卡的强度的最大公因数乘以符卡的个数。
小E会从魔法阵中选择一段连续符卡区间[l,r](包括l,r端点),作为吟唱的咒语。她想知道,咒语最大的魔力值是多少。
输入
第一行一个整数n,表示符卡个数。
第二行n个正整数,第i个数表示符卡的强度ai。
输出
输出一个整数,表示最大的魔力值。
样例输入 Copy

530 60 20 20 20

样例输出 Copy

80
提示
样例解释
选择区间[2,5],其中gcd(60,20,20,20)=20,故魔力值为(5-2+1)*20=80。
二话不说,先放上友链

大佬的想法

在这里插入图片描述
在这里插入图片描述
代码和某巨巨差不多,只是注释多了点

#include 
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define wuyt maintypedef long long ll;#define HEAP(...) priority_queue<__VA_ARGS__ >#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >template
inline T min(T &x,const T &y){ return x>y?y:x;}template
inline T max(T &x,const T &y){ return x
>=1; x=(x*x); } return res;}ll maxx=-1;ll minn=inf;ll num2[maxn];ll num[maxn];ll res,ans;int sum=0;map
mp;map
mpt;vector
vet;priority_queue
,greater
> xiaogen;queue
duilie;priority_queue
,less
> que;///https://www.pasteme.cn/35934int main(){ int n=read; for(int i=1;i<=n;i++) num[i]=read; ans=-1; for(int i=1;i<=n;i++)///right { mp=mpt,mpt.clear(); ans=max(ans,num[i]);///本身的最大 if(mpt.count(num[i])==0) mpt[num[i]]=i; for(auto it=mp.begin();it!=mp.end();it++){ res=gcd(num[i],it->first);///it->first里面是存储的数,it->second里面是下标对于mpt来说 ///对于前面区间的更新已经通过mp=mpt更新,所以对于mp亦是如此 ans=max(ans,res*(i-(it->second)+1)); ///test: ///cout<
<
second; else if(mpt.count(res)!=0) mpt[res]=min(mpt[res],it->second); } } cout<
<

转载地址:http://kloo.baihongyu.com/

你可能感兴趣的文章