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

Balancing the Barns

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

의견: 0

설명

Farmer John has \(N\) (\(1\le N\le 5\cdot 10^4\)) barns arranged along a road. The
\(i\)-th barn contains \(a_i\) bales of hay and \(b_i\) bags of feed
\((0\le a_i,b_i\le 10^9\)).

Bessie has been complaining about the inequality between barns. She defines the
"imbalance" of the farm as the difference between the maximum hay in any barn
and the minimum feed in any barn. Formally, the imbalance is
\(\max(a) - \min(b)\).

To address Bessie's concerns, Farmer John can perform exactly \(K\)
(\(1\le K\le 10^{18}\)) transfers. In each transfer, he selects a barn \(i\), sells
one of its haybales, and buys it a new bag of feed for the same barn. Note that
there can be negative amounts in his farm (he is not afraid of debt). Formally,
\(K\) times, you choose an index \(i\in [1,N]\), decrement \(a_i\), and increment
\(b_i\).

Help Farmer John determine the minimum possible imbalance after performing
exactly \(K\) transfers.

Problem credits: Rohin Garg

제약

SCORING

  • Inputs 2-4: \(K\le 500\), sum of \(N\) over all testcases is \(\le 500\)
  • Inputs 5-8: Sum of \(N\) over all testcases is \(\le 500\)
  • Inputs 9-13: No additional constraints.

Problem credits: Rohin Garg

입력 형식

The first line contains \(T\) (\(1 \leq T \leq 10^3\)), the number of independent
test cases.

The first line of each test case contains \(N\) and \(K\).

The following line contains \(a_1\dots a_N\).

The following line contains \(b_1\dots b_N\).

The sum of \(N\) over all test cases is at most \(5 \cdot 10^4\).

출력 형식

For each test case, output a single integer, the minimum possible value of
\(\max(a) - \min(b)\) after performing \(K\) operations.

예제 1
입력
4
1 10
5
3
2 6
100 96
0 4
3 3
1 1 2
0 0 1
3 3
1 2 2
0 1 1
출력
-18
90
0
0
설명

In the first test case, Farmer John can transfer \(10\) haybales from barn \(1\)
into bags of feed. This leaves \(a = [-5]\) and \(b = [13]\). The imbalance is
\(\max(a) - \min(b) = -5 - 13 = -18\).

In the second test case, Farmer john can transfer \(5\) haybales from barn \(1\) and
\(1\) haybale from barn \(2\). This leaves \(a = [95, 95]\) and \(b = [5, 5]\). The
imbalance is \(95 - 5 = 90\). This is the minimum imbalance Farmer John can
achieve.

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2025-2026 > Second Contest > Gold

태그

평가 및 의견

Balancing the Barns

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

Log in to rate problems.

개별 의견

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

풀이 제출

Balancing the Barns

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