ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Crossing River 题解(贪心)

2020-11-22 15:00:55  阅读:271  来源: 互联网

标签:typedef include const int 题解 long ans Crossing River


题目链接

题目大意

t组数据(t<=20)

给你n个人(n<=1000)过河,每个人都有权值,一条船,每次船最多运2个人,每次的花费为两个人的较大花费

求所有人都过河需要的最小花费

题目思路

经典的过河问题,记录一下

先将权值从小到大排序一下

每次运两个人显然有两种最优的方法

1:先运(a[1],a[2])过去,a[1]回来,再运(a[n],a[n-1])过去,a[2]回来

cost1=a[n]+2*a[2]+a[1]

2:先运(a[n],a[1])过去,a[1]回来,再运(a[n-1],a[1])过去,a[1]再回来

cost2=a[n]+a[n-1]+2*a[1]

然后当n只有三个的时候特判一下即可

代码

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
//#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1000+5,inf=0x3f3f3f3f,mod=1e9+7;
const double eps=1e-5;
int n,a[maxn];
int main(){
    int _;scanf("%d",&_);
    while(_--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        sort(a+1,a+1+n);
        int ans=0;
        while(n>3){
            ans+=min(a[n]+a[n-1]+2*a[1],a[n]+2*a[2]+a[1]);
            n-=2;
        }
        if(n==1){
            ans+=a[1];
        }else if(n==2){
            ans+=a[2];
        }else{
            ans+=a[1]+a[2]+a[3];
        }
        printf("%d\n",ans);
    }
    return 0;
}

标签:typedef,include,const,int,题解,long,ans,Crossing,River
来源: https://www.cnblogs.com/hunxuewangzi/p/14019397.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有