我对\(Jhonson\)算法的理解:https://www.cnblogs.com/AKMer/p/9863620.html

题目传送门:https://www.luogu.org/problemnew/show/P1561

\(Jhonson\)裸题。

时间复杂度:\(O(nlogn)\)

空间复杂度:\(O(n)\)

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std; const int maxn=25005; int n,ans; int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
} struct cow {
int a,b; bool operator<(const cow &tmp)const {
return min(a,tmp.b)<=min(tmp.a,b);
}
}p[maxn]; int main() {
n=read();
for(int i=1;i<=n;i++)
p[i].a=read(),p[i].b=read();
sort(p+1,p+n+1);int T=0;
for(int i=1;i<=n;i++) {
ans+=p[i].a;
T=p[i].b+max(T-p[i].a,0);
}
printf("%d\n",ans+T);
return 0;
}

洛谷【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

    传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最 ...

  3. P1561 [USACO12JAN]爬山Mountain Climbing

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

  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. E - What Is Your Grade?

    E - What Is Your Grade? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  2. SELinux状态修改

    查看SELinux状态: 1./usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态 SELinux status:         ...

  3. 7.FactoryBean 和BeanFactory去区别

    FactoryBean源码: /* * Copyright 2002-2012 the original author or authors. * * Licensed under the Apach ...

  4. centos6下nginx配置php可用

    先查看下所有服务的状态,看看php-fpm有没有正在运行 [root@centos64 html]# service --status-all php-fpm (pid  3568) 正在运行... ...

  5. spring 注解管理

    一.注解准备 1.xml引入新的约束,并开启注解扫描 context:component-scan标签开启注解扫描 2.导入注解有关jar包 二.注解创建对象 1.User类 @Component( ...

  6. 7.Django模型类的定义和管理

    Django的模型类是给ORM层服务的 1.每个数据模型都是django.db.models.Model的子类. 2.它的父类Model包含了所有必要的和数据库交互的方法,并提供了定义数据库字段的语法 ...

  7. MyEclipse2014新增bug,尝鲜的朋友需注意NotFoundException: org.springframework.web.context.ContextLoaderListener

    事实上标题后面加上这个异常信息并不合适,可是为了方便和我遇到相同问题的童鞋搜到这篇文章.我不得不这样写啦. 这个异常和你的程序没有关系,假设你没有忘记增加spring Jar包的话,这是fucking ...

  8. Linux:分区

    Linux:分区 分区表 磁盘分区表主要有两种格式,一种是限制较多的 MBR 分区表,一种是较新且限制较少的 GPT 分区表. MBR MBR 中,第一个扇区最重要,里面有主要开机记录(Master ...

  9. Java多线程系列 面试题

    1. https://blog.csdn.net/jjj19891128/article/details/24393661  多线程经典面试题 2. https://blog.csdn.net/ll6 ...

  10. WampServer修改MySQL密码的问题

    WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就 一步步来操作. 首先,通过WAMP打开mysql控制台.     提示输入原密码,如果是空,直接按 ...