魔法序列-upc

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

5
30 60 20 20 20

样例输出 Copy
80
提示
样例解释
选择区间[2,5],其中gcd(60,20,20,20)=20,故魔力值为(5-2+1)*20=80。
二话不说,先放上友链
传送门
学长的做法

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

#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
///#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
///char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const ll INF = 0x3f3f3f3f;
const int maxn = 2e6 + 7;
const int mod = 1e9 + 7;
ll gcd(ll a,ll b)
{
    ll t;
    while(b!=0)
    {
        t=a%b;
        a=b;
        b=t;
    }
    return a;
}
ll qPow(ll x, ll k)
{
    ll res = 1;
    while(k) {
        if(k&1)
            res=(res*x);
        k>>=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<ll,ll> mp;
map<ll,ll> mpt;
vector<ll> vet;
priority_queue <int ,vector<int> ,greater<int> > xiaogen;
queue <ll> duilie;
priority_queue <int ,vector<int> ,less<int> > que;
///https://www.pasteme.cn/35934
int 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<<res<<endl;
            ///cout<<ans<<endl;
            ///更新端点
            if(mpt.count(res)==0) mpt[res]=it->second;
            else if(mpt.count(res)!=0) mpt[res]=min(mpt[res],it->second);
        }
    }
    cout<<ans<<endl;
    return 0;
}
/**
5
30 60 20 20 20
**/

posted @ 2020-05-06 18:28  PushyTao  阅读(143)  评论(0编辑  收藏  举报