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!dom
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.ide
Please determine the least possible amount of time for all N cows to make the entire journey.ui
農場主約翰發現他的奶牛劇烈運動後產奶的質量更高,因此他決定讓N頭(1 <= N <= 25,000)奶牛去附近登山再返回來。spa
第i頭奶牛用時U(i)爬上山,用時D(i)下山。做爲家畜,奶牛們每段路都要有農夫的幫助,但是因爲經濟疲軟,農場裏只有兩個農夫John和Don。John計劃引導奶牛登山,Don引導奶牛下山。雖然每一個奶牛都須要嚮導,但每段旅途只有一名農夫。全部任什麼時候刻只有一頭奶牛登山也只能有一頭奶牛下山,奶牛爬上山後,能夠暫時停留在山頂上等待Don的幫助。奶牛上山的順序和下山的順序不必定要相同。pwa
請計算出全部N 頭牛完成旅程的最短期。code
輸入格式:blog
第一行,一個整數Nci
第2 到第N+1 行,每行兩個用空格隔開的整數U(i)和D(i)。get
(1 <= U(i), D(i) <= 50,000).it
輸出格式:
一行一個整數,表示全部N 頭牛完成旅程的最短期。
3 6 4 8 1 2 3
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){;}