码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷 P1561 [USACO12JAN]爬山Mountain Climbing

时间:2017-11-06 15:02:18      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:names   str   std   bin   col   return   hid   stream   pen   

传送门

题目大意:

n头牛,上山时间为u(i),下山为d(i).

要求每一时刻最多只有一头牛上山,一头牛下山。

问每头牛都上下山后花费最少时间。

题解:贪心

推了推样例,发现上山时间一定,那找个下山最快

的当最后一头山上的牛。

 

技术分享
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define N 25009
using namespace std;

int n;

LL ans;
int hh=12345677;

struct Cows{
    int u,d;
}c[N];

bool cmp(Cows a,Cows b){
    return a.u<b.u;
}

int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&c[i].u,&c[i].d);
        ans+=c[i].u;hh=min(hh,c[i].d);
    }
    cout<<ans+hh;
    return 0;
}
70骗分

 

发现正解和上面的结论是差不多的。

a、总上山时间大于总下山时间

这说明牛的总上山时间是恒定的,一定要记录答案的。

记录答案的还有最后一头牛的下山时间,所以最终结果是

总上山时间+牛最快的下山时间

b、总下山时间大于总上山时间

这说明牛的下山时间是恒定的,一定要记录答案的。

记录答案的还有一头牛的上山时间,所以最终的结果

是:总下山时间+最快牛的上山时间

代码:

技术分享
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;

int n;

LL su,sd,mnu,mnd;

int main(){
    scanf("%d",&n);
    mnu=mnd=100000000;
    for(int i=1;i<=n;i++){
        int up,down;
        scanf("%d%d",&up,&down);
        su+=up;sd+=down;
        if(up<mnu)mnu=up;
        if(down<mnd)mnd=down;
    }
    if(su>sd)cout<<su+mnd;
    else cout<<sd+mnu;
//    cout<<max(su+mnd,sd+mnu);
    return 0;
}
View Code

 

洛谷 P1561 [USACO12JAN]爬山Mountain Climbing

标签:names   str   std   bin   col   return   hid   stream   pen   

原文地址:http://www.cnblogs.com/zzyh/p/7792868.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!