RiseOJ는 solved.ac와 제휴 관계가 없습니다. 티어 아이콘 © solved.ac. solved.ac
포럼
USACO0587

Haybale Distribution

Platinum V 플래티넘 V
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

Farmer John is distributing haybales across the farm!

Farmer John's farm has \(N\) \((1\le N\le 2\cdot 10^5)\) barns, located at integer
points \(x_1,\dots, x_N\) \((0 \le x_i \le 10^6)\) on the number line. Farmer John's
plan is to first have \(N\) shipments of haybales delivered to some integer point
\(y\) \((0 \le y \le 10^6)\) and then distribute one shipment to each barn.

Unfortunately, Farmer John's distribution service is very wasteful. In
particular, for some \(a_i\) and \(b_i\) \((1\le a_i, b_i\le 10^6)\), \(a_i\) haybales
are wasted per unit of distance left each shipment is transported, and \(b_i\)
haybales are wasted per unit of distance right each shipment is transported.
Formally, for a shipment being transported from point \(y\) to a barn at point
\(x\), the number of haybales wasted is given by

$$ \begin{cases} a_i\cdot (y-x) & \text{if } y \ge x \\ b_i\cdot (x-y) & \text{if } x > y \end{cases}. $$

Given \(Q\) \((1\le Q\le 2\cdot 10^5)\) independent queries each consisting of
possible values of \((a_i,b_i)\), please help Farmer John determine the fewest
amount of haybales that will be wasted if he chooses \(y\) optimally.

Problem credits: Benjamin Qi

제약

SCORING

  • Input 2: \(N,Q\le 10\)
  • Input 3: \(N,Q\le 500\)
  • Inputs 4-6: \(N,Q\le 5000\)
  • Inputs 7-16: No additional constraints.

Problem credits: Benjamin Qi

입력 형식

The first line contains \(N\).

The next line contains \(x_1\dots x_N\).

The next line contains \(Q\).

The next \(Q\) lines each contain two integers \(a_i\) and \(b_i\).

출력 형식

Output \(Q\) lines, the \(i\)th line containing the answer for the \(i\)th query.

예제 1
입력
5
1 4 2 3 10
4
1 1
2 1
1 2
1 4
출력
11
13
18
30
설명

For example, to answer the second query, it is optimal to select \(y=2\). Then the
number of wasted haybales is equal to
\(2(2-1)+2(2-2)+1(3-2)+1(4-2)+1(10-2)=1+0+1+2+8=13\).

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2023-2024 > December > Gold

태그

평가 및 의견

Haybale Distribution

개요
출제자 난이도 Platinum V 플래티넘 V 의견 0 / 50 공개 집계 (커뮤니티 난이도, 주요 주제, 품질)는 의견이 충분히 모이면 공개됩니다.

Log in to rate problems.

개별 의견

아직 의견이 없습니다. 자격이 된다면 위 양식에서 가장 먼저 평가해 보세요.

풀이 제출

Haybale Distribution

게스트로 둘러보고 있습니다. 로그인하면 풀이를 제출하고 진행 상황을 확인할 수 있습니다. 로그인하고 제출하기
공개
C++20 Tab 들여쓰기 · Ctrl+/ 주석 토글 · Enter 자동 들여쓰기
1 1 1 0 공백: 4 · UTF-8