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;
    }
    
  • 相关阅读:
    POJ 3348 Cows (凸包面积)
    P4126 [AHOI2009]最小割
    P1903 [国家集训队]数颜色
    P3749 [六省联考2017]寿司餐厅
    CF666E Forensic Examination
    P2336 [SCOI2012]喵星球上的点名
    P1117 [NOI2016]优秀的拆分
    P3747 [六省联考2017]相逢是问候
    CF1062F Upgrading Cities
    P3243 [HNOI2015]菜肴制作
  • 原文地址:https://www.cnblogs.com/mleautomaton/p/10569266.html
Copyright © 2011-2022 走看看