zoukankan      html  css  js  c++  java
  • 【UOJ244】 【UER #7】短路(贪心)

    传送门

    uoj

    Solution

    简单题?
    考虑一条路径长什么样子,一定是经过某一个字母环的左上角,那么答案就很简单了。
    我们记一个前缀最小值,这样子让他一路走下去一定是最优!
    然后扫一遍就好了。

    代码实现

    /*
      mail: mleautomaton@foxmail.com
      author: MLEAutoMaton
      This Code is made by MLEAutoMaton
    */
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<queue>
    #include<set>
    #include<map>
    #include<iostream>
    using namespace std;
    #define ll long long
    #define re register
    #define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
    inline int gi()
    {
    	int f=1,sum=0;char ch=getchar();
    	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    	return f*sum;
    }
    inline ll gl()
    {
    	ll f=1,sum=0;char ch=getchar();
    	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    	return f*sum;
    }
    ll ans=1e18+10,Min=1e18+10;
    ll a[400010],sum,n;
    int main()
    {
    	n=gl()+1;
    	for(int i=1;i<=n;i++)a[i]=gl();reverse(a+1,a+n+1);
    	for(int i=1;i<=n;i++)
    	{
    		Min=min(Min,a[i]);
    		ans=min(ans,a[i]*(4*(n-i)+1)+sum*2);
    		sum+=a[i]+Min;
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    
  • 相关阅读:
    便 加权并查集
    bzoj 4565 状压区间dp
    bzoj 2242 [SDOI2011]计算器 快速幂+扩展欧几里得+BSGS
    poj 3243 扩展BSGS
    bzoj 3239 poj 2417 BSGS
    51nod 1135 原根 就是原根...
    bzoj 2005 能量采集 莫比乌斯反演
    约会 倍增lca
    bzoj 2186 [Sdoi2008]沙拉公主的困惑 欧拉函数
    IE下textarea去除回车换行符
  • 原文地址:https://www.cnblogs.com/mleautomaton/p/10569266.html
Copyright © 2011-2022 走看看