传送门

题目大意:

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=; 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=;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 ;
}

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=;
for(int i=;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 ;
}

洛谷 P1561 [USACO12JAN]爬山Mountain Climbing的更多相关文章

  1. 洛谷—— P1561 [USACO12JAN]爬山Mountain Climbing

    https://daniu.luogu.org/problemnew/show/P1561 题目描述 Farmer John has discovered that his cows produce ...

  2. P1561 [USACO12JAN]爬山Mountain Climbing

    P1561 [USACO12JAN]爬山Mountain Climbing 题目描述 Farmer John has discovered that his cows produce higher q ...

  3. 洛谷【P1561】[USACO12JAN]爬山Mountain Climbing

    我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html 题目传送门:https://www.luogu.org/problemn ...

  4. [USACO12JAN]爬山Mountain Climbing

    题目描述 Farmer John has discovered that his cows produce higher quality milk when they are subject to s ...

  5. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  6. 洛谷 P1361 小猫爬山

    题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W ...

  7. 洛谷——P1361 小猫爬山

    https://www.luogu.org/problem/show?pid=1361#sub 题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲 ...

  8. 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share

    P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N & ...

  9. 洛谷 P3041 [USACO12JAN] Video Game Combos

    题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only v ...

随机推荐

  1. java判断集合list是为空

    if(null == list || list.size() ==0 ){ } list.isEmpty()和list.size()==0 没有区别 isEmpty()判断有没有元素而size()返回 ...

  2. MongoDB快速入门(十二) -- 索引

    MongoDB 索引 索引支持的解析度的查询效率.如果没有索引,MongoDB必须扫描每一个文档的集合,要选择那些文档相匹配的查询语句.这种扫描的效率非常低,会要求 mongod 做大数据量的处理. ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. EYES组——软件体系结构上机规划

    EYES组——软件体系结构上机规划 考勤助手 第九周: 需求分析的详细撰写,并在此基础上探讨与完善,讨论软件的体系结构风格,画出初步的UML类图. 第十周: 选择合适的软件构架风格(3层C/S架构风格 ...

  5. 导出成可运行jar包时所遇问题的解决办法 - 转载

    Could not find main method from given launch configuration 当我把我的Java工程导出为可运行的jar包时,遇到了“Could not fin ...

  6. Java 如何解析由String类型拼接的XML格式

    String xml = new String(a);打印的xml 的值是 <?xml version= 1.0 encoding=gb2312?><weighData>< ...

  7. NumPy在数组上的迭代

    NumPy - 数组上的迭代 NumPy 包包含一个迭代器对象numpy.nditer. 它是一个有效的多维迭代器对象,可以用于在数组上进行迭代. 数组的每个元素可使用 Python 的标准Itera ...

  8. JNIjw06

    1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw06.h" // 全局变量 jfieldID g_pro ...

  9. 图示:DOM元素各种位置属性

  10. WCF简单学习

    前两天学习了WCF的一些基础东西,好像不怎么用,但是学习一下还是有助自己增长知识滴.既然是学习就先偷一下别人的概念基础.wcf中有一个ABC的概念,就是第一: "A" 是地址,就是 ...