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

Median Heap

Diamond V 다이아몬드 V
난이도
2s
시간 제한
256MB
메모리 제한
0
맞았습니다!!
0
제출 수
0.0%
정답률
레이팅

의견: 0

설명

*Note: The time limit for this problem is 4s, twice the default.*

Farmer John has a binary tree with \(N\) nodes where the nodes are numbered from
\(1\) to \(N\) (\(1 \leq N < 2\cdot 10^5\) and \(N\) is odd). For \(i>1\), the parent of
node \(i\) is \(\lfloor i/2\rfloor\). Each node has an initial integer value \(a_i\),
and a cost \(c_i\) to change the initial value to any other integer value
(\(0\le a_i,c_i\le 10^9\)).

He has been tasked by the Federal Bovine Intermediary (FBI) with finding an
approximate median value within this tree, and has devised a clever algorithm to
do so.

He starts at the last node \(N\) and works his way backward. At every step of the
algorithm, if a node would not be the median of it and its two children, he
swaps the values of the current node and the child value that would be the
median. At the end of this algorithm, the value at node \(1\) (the root) is the
median approximation.

The FBI has also given Farmer John a list of \(Q\) \((1 \leq Q \leq 2\cdot 10^5)\)
independent queries each specified by a target value \(m\) (\(0\le m\le 10^9\)). For
each query, FJ will first change some of the node's initial values, and then
execute the median approximation algorithm. For each query, determine the
minimum possible total cost for FJ to make the output of the algorithm equal to
\(m\).

Problem credits: Suhas Nagar and Benjamin Qi

제약

SCORING

  • Inputs 2-4: \(N,Q\le 50\)
  • Inputs 5-7: \(N,Q\le 1000\)
  • Inputs 8-16: No additional constraints

Problem credits: Suhas Nagar and Benjamin Qi

입력 형식

The first line of input contains \(N\).

The next \(N\) lines each contain two integers \(a_i\) and \(c_i\).

The next line contains \(Q\).

The next \(Q\) lines each contain a target value \(m\).

출력 형식

Output \(Q\) lines, the minimum possible total cost for each target value \(m\).

예제 1
입력
5
10 10000
30 1000
20 100
50 10
40 1
11
55
50
45
40
35
30
25
20
15
10
5
출력
111
101
101
100
100
100
100
0
11
11
111
설명

To make the median approximation equal \(40\), FJ can change the value at node 3
to \(60\). This costs \(c_3=100\).

To make the median approximation equal \(45\), FJ can change the value at node 3
to \(60\) and the value at node 5 to \(45\). This costs \(c_3+c_5=100+1=101\).

문제 정보

riseoj 작성

출처 올림피아드 > USACO > 2024-2025 > January > Gold

태그

평가 및 의견

Median Heap

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

Log in to rate problems.

개별 의견

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

풀이 제출

Median Heap

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