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

https://daniu.luogu.org/problemnew/show/P1561

题目描述

Farmer John has discovered that his cows produce higher quality milk when they are subject to strenuous exercise. He therefore decides to send his N cows (1 <= N <= 25,000) to climb up and then back down a nearby mountain!

Cow i takes U(i) time to climb up the mountain and then D(i) time to climb down the mountain. Being domesticated cows, each cow needs the help of a farmer for each leg of the climb, but due to the poor economy, there are only two farmers available, Farmer John and his cousin Farmer Don. FJ plans to guide cows for the upward climb, and FD will then guide the cows for the downward climb. Since every cow needs a guide, and there is only one farmer for each part of the voyage, at most one cow may be climbing upward at any point in time (assisted by FJ), and at most one cow may be climbing down at any point in time (assisted by FD). A group of cows may temporarily accumulate at the top of the mountain if they climb up and then need to wait for FD‘s assistance before climbing down. Cows may climb down in a different order than they climbed up.

Please determine the least possible amount of time for all N cows to make the entire journey.

农场主约翰发现他的奶牛剧烈运动后产奶的质量更高,所以他决定让N头(1 <= N <= 25,000)奶牛去附近爬山再返回来。

第i头奶牛用时U(i)爬上山,用时D(i)下山。作为家畜,奶牛们每段路都要有农夫的帮助,可是由于经济疲软,农场里只有两个农夫John和Don。John计划引导奶牛爬山,Don引导奶牛下山。虽然每个奶牛都需要向导,但每段旅途只有一名农夫。所有任何时刻只有一头奶牛爬山也只能有一头奶牛下山,奶牛爬上山后,可以暂时停留在山顶上等待Don的帮助。奶牛上山的顺序和下山的顺序不一定要相同。

请计算出所有N 头牛完成旅程的最短时间。

输入输出格式

输入格式:

第一行,一个整数N

第2 到第N+1 行,每行两个用空格隔开的整数U(i)和D(i)。

(1 <= U(i), D(i) <= 50,000).

输出格式:

一行一个整数,表示所有N 头牛完成旅程的最短时间。

输入输出样例

输入样例#1: 复制

3
6 4
8 1
2 3

输出样例#1: 复制

17
 1 #include <cstdio>
 2
 3 #define min(a,b) (a<b?a:b)
 4 #define max(a,b) (a>b?a:b)
 5
 6 inline void read(int &x)
 7 {
 8     x=0; register char ch=getchar();
 9     for(; ch>‘9‘||ch<‘0‘; ) ch=getchar();
10     for(; ch>=‘0‘&&ch<=‘9‘; ch=getchar()) x=x*10+ch-‘0‘;
11 }
12
13 int Presist()
14 {
15     int n,totu=0,totd=0;
16     int minu=1e9,mind=1e9;
17
18     read(n);
19     for(int ui,di,i=1; i<=n; ++i)
20     {
21         read(ui),read(di);
22         totu+=ui,minu=min(minu,ui);
23         totd+=di,mind=min(mind,di);
24     }
25     printf("%d\n",max(totu+mind,totd+minu));
26     return 0;
27 }
28
29 int Aptal=Presist();
30 int main(int arg,char**argv){;}
时间: 2024-08-02 02:02:47

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

洛谷 P1561 [USACO12JAN]爬山Mountain Climbing

传送门 题目大意: n头牛,上山时间为u(i),下山为d(i). 要求每一时刻最多只有一头牛上山,一头牛下山. 问每头牛都上下山后花费最少时间. 题解:贪心 推了推样例,发现上山时间一定,那找个下山最快 的当最后一头山上的牛. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define LL long long #define N 25009 using

P1561 [USACO12JAN]爬山Mountain Climbing

P1561 [USACO12JAN]爬山Mountain Climbing 题目描述 Farmer John has discovered that his cows produce higher quality milk when they are subject to strenuous exercise. He therefore decides to send his N cows (1 <= N <= 25,000) to climb up and then back down a

洛谷——P1361 小猫爬山

https://www.luogu.org/problem/show?pid=1361#sub 题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W,而N只小猫的重量分别是C1.C2……CN.当然,每辆缆车上的小猫的重量之和不能超过W.每租用一辆缆车,WD和LHX就要付1美元,所以他们想知道,最少需要付多少美元才能把这N只小猫都运送下山? 输入输出格

洛谷 P1361 小猫爬山

题目描述 WD和LHX饲养了N只小猫,这天,小猫们要去爬山.经历了千辛万苦,小猫们终于爬上了山顶,但是疲倦的它们再也不想徒步走下山了. WD和LHX只好花钱让它们坐索道下山.索道上的缆车最大承重量为W,而N只小猫的重量分别是C1.C2……CN.当然,每辆缆车上的小猫的重量之和不能超过W.每租用一辆缆车,WD和LHX就要付1美元,所以他们想知道,最少需要付多少美元才能把这N只小猫都运送下山? 输入输出格式 输入格式: 第一行包含两个用空格隔开的整数,N和W. 接下来N行每行一个整数,其中第i+1行

洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally decided that they are going to start connecting their farms together by trails in an effort to form an alliance against the farmers. The cows in each

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

P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N <= 20) bales of hay, where bale i has size S_i (1 <= S_i <= 100). He wants to divide the bales between his three barns as fairly as possible. After

洛谷 P3041 [USACO12JAN] Video Game Combos

题目描述 Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only valid buttons. Bessie may press the buttons in any order she likes; however, there are only N distinct combos possible (1 <= N <= 20). Combo i is repr

洛谷冬令营随想

洛谷春令营结束不久,就我个人而言实在是收益良多. 课后与洛谷的讲师们略有交谈,并且稍微接触了别地的信竞生,顿感弱小--决定把一些了解到的信息和感想放在这里.比较杂,将就着看吧. 首先说下近年竞赛的发展吧. 近年信息学竞赛带来的升学优势增大了不少,机会增加了,这是好事.但这同样也意味着竞争将激烈许多. 举几个例子: 浙江小学便培育"种子选手",浙江初赛分数线一度达到90分. 著名的南山中学,C类买不停,此处不细说. 很大一部分选手以完全停课备战比赛. 切实参与到竞赛中,能感觉到许多学校已

洛谷 P2709 BZOJ 3781 小B的询问

题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重复次数.小B请你帮助他回答询问. 输入输出格式 输入格式: 第一行,三个整数N.M.K. 第二行,N个整数,表示小B的序列. 接下来的M行,每行两个整数L.R. 输出格式: M行,每行一个整数,其中第i行的整数表示第i个询问的答案. 输入输出样例 输入样例#1: 6 4 3 1 3 2 1 1 3