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;
    }
    
  • 相关阅读:
    顺序容器添加,查询,删除元素
    使用fiddler对app做弱网测试
    工作总结
    软件测试面试题_3
    软件测试面试题_2
    软件测试面试题_1
    MySQL的下载及安装
    关于let以及var的区别
    关于获取各种浏览器可见窗口大小的一点点研究
    log4J指定类下面的日志分隔
  • 原文地址:https://www.cnblogs.com/mleautomaton/p/10569266.html
Copyright © 2011-2022 走看看