题目描述
Farmer John finds that his cows are each easier to milk when they have another cow nearby for moral support. He therefore wants to take his M cows (M≤1,000,000,000, M even) and partition them into M/2 pairs. Each pair of cows will then be ushered off to a separate stall in the barn for milking. The milking in each of these M/2 stalls will take place simultaneously.
To make matters a bit complicated, each of Farmer John’s cows has a different milk output. If cows of milk outputs Aand B are paired up, then it takes a total of A+Bunits of time to milk them both.
Please help Farmer John determine the minimum possible amount of time the entire milking process will take to complete, assuming he pairs the cows up in the best possible way.
输入
The first line of input contains N (1≤N≤100,000). Each of the next N lines contains two integers x and y, indicating that FJ has x cows each with milk output y (1≤y≤1,000,000,000). The sum of the x’s is M, the total number of cows.
输出
Print out the minimum amount of time it takes FJ’s cows to be milked, assuming they are optimally paired up.
样例输入
3
1 8
2 5
1 2
样例输出
10
数据范围限制
提示
Here, if the cows with outputs 8+2 are paired up, and those with outputs 5+5 are paired up, the both stalls take 10 units of time for milking. Since milking takes place simultaneously, the entire process would therefore complete after 10 units of time. Any other pairing would be sub-optimal, resulting in a stall taking more than 10 units of time to milk.
译文:
题目描述
农场主约翰发现他的母牛在他们附近有另一头母牛的时候,他们很容易被挤奶以获得精神上的支持。因此他想把他的M(M≤牛1000000000,M)和分割成M / 2对。然后,每对奶牛将被带到谷仓的一个单独的货摊挤奶。在每个这些M / 2档挤奶将同时发生。
使事情有点复杂,每个农民约翰的奶牛有不同的牛奶产量。如果牛奶输出A、B都是成对出现的牛,然后以共+ bunits时间牛奶都。
请帮助农民约翰确定尽可能小的时间,整个挤奶过程将完成,假设他对奶牛在尽可能最好的方式。
输入
输入的第一行包含N(1≤N≤100000)。接下来的n行每行包含两个整数x和y,表明福建X牛每牛奶产量Y(1≤Y≤1000000000)。x的总和是m,母牛总数。
输出
打印出的最小时间以FJ的奶牛挤奶,假设他们是最佳的配对。
样例输入
三
1 8
2 5
1 2
样例输出
十
数据范围限制
提示
在这里,如果奶牛产出8 + 2配对,并与输出5 + 5配对,这两个摊位需要挤奶的时间单位。由于挤奶同时发生,整个过程将因此完成后10个单位的时间。任何其他配对将是次优,导致一个摊位采取超过10个单位的时间牛奶。
思路:
题目问,m头奶牛两两配对,最大时间最小是多少
奶牛数m太大了,直接拆开数组装不下
那就按挤奶的时间升序排序
然后头尾配对就可以了(最大值最小)
实在不清楚,看一看代码
代码:
var
a,b:array[0..100000]of longint;
n,m,i,j,x,y,l,r,max:longint;
procedure qsort(l,r:longint);
var
i,j,mid:longint;
begin
i:=l;
j:=r;
mid:=b[i];
repeat
while b[i]<mid do inc(i);
while b[j]>mid do dec(j);
if i<=j then
begin
a[0]:=a[i];
a[i]:=a[j];
a[j]:=a[0];
b[0]:=b[i];
b[i]:=b[j];
b[j]:=b[0];
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
procedure solve;
begin
while l<=r do
begin
if a[l]>m then
begin
dec(a[r],m);
end
else
begin
dec(m,a[l]);
dec(a[r],a[l]);
end;
if b[l]+b[r]>max then max:=b[l]+b[r];
while a[r]<=0 do
begin
inc(a[r-1],a[r]);
dec(r);
if b[l]+b[r]>max then max:=b[l]+b[r];
end;
l:=l+1;
end;
end;
begin
assign(input,'pairup.in');reset(input);
assign(output,'pairup.out');rewrite(output);
readln(n);
for i:=1 to n do
begin
readln(a[i],b[i]);
inc(m,a[i]);
end;
qsort(1,n);
l:=1;
r:=n;
m:=m div 2;
solve;
writeln(max);
end.

本文探讨了如何通过最优配对策略减少农场主约翰的M头奶牛的挤奶时间。通过对奶牛挤奶时间进行排序并采用头尾配对的方法,确保了整个挤奶过程能在最短时间内完成。
352

被折叠的 条评论
为什么被折叠?



