DescriptionDescription
Mr. T is a quality supervisor who recently inspected the quality of a group of minerals. This group of minerals has a total of N ore numbers from 1 to n, each of which has its own weight WI and value VI. The mineral inspection process is as follows: if the inspection results of this batch of minerals are too different from the given standard value S, we need to test another batch of minerals. Small t does not want to spend time to test another batch of minerals, so he wants to adjust the value of the parameter W, so that the test results as close as possible to the standard value s, even if the absolute value of the S-Y is the smallest. Please help find the minimum value.
Idea: Question description... Like a newly defined question... I am really... It is relatively simple. The larger the value of W, the smaller the value of Y. In binary mode, the prefix and array are updated each time. Then, the relationship between the left and right endpoints and the calculated value and S is compared. Then, the relationship continues until the Left and Right endpoints are adjacent.
Code:
# Include <iostream> # include <cstdio> # include <cmath> using namespace STD; int V [200001] = {0}, W [200001] = {0 }, li [200001] = {0}, RI [200001] = {0}; long sum [200001] = {0}, VV [200001] = {0 }; int main () {int I, j, n, m, mid, LL = 0, RR = 0; long s = 0, ANS = 0, ans1 = 0, ans2 = 0; scanf ("% d % LLD", & N, & M, & S); for (I = 1; I <= N; ++ I) {scanf ("% d", & W [I], & V [I]); If (W [I]> RR) RR = W [I]; sum [I] = sum [I-1] + 1; VV [I] = VV [I-1] + V [I];} for (I = 1; I <= m; ++ I) {scanf ("% d", & Li [I], & Ri [I]); -- Li [I]; ans1 = ans1 + (VV [ri [I]-VV [Li [I]) * (sum [ri [I]-sum [Li [I]);} + + RR; LL = 1; while (LL <rr-1) {mid = (LL + RR)/2; for (I = 1; I <= N; + + I) {sum [I] = sum [I-1]; if (W [I]> = mid) ++ sum [I] ;}for (I = 1; I <= N; ++ I) {VV [I] = VV [I-1]; If (W [I]> = mid) VV [I] + = V [I];} ans = 0; for (I = 1; I <= m; ++ I) ans + = (sum [ri [I]-sum [Li [I]) * (VV [ri [I]-VV [Li [I]); If (ANS> = S & S> = ans2) {ll = mid; ans1 = ans;} If (ANS <= S & S <= ans1) {RR = mid; ans2 = ans;} ans = min (ABS (ans1-s ), ABS (ans2-s); printf ("% LLD \ n", ANS );}
Codevs1138 smart QC staff: T5 1t5