"Explanations" Luo Valley P1314 smart quality supervision

Portal:

Portal1: Luogu

Portal2: LibreOJ

Portal3: Strands

Description

小THe is a quality supervisor, recently responsible for inspecting the quality of a number of minerals. These minerals There \ (n \) th ore from \ (1 \) to \ (n \) individually numbered, and each mineral has its own weight \ (w_i \) and the value of \ (v_i \) . Mineral inspection process is:

  1. Given \ (m \) intervals \ ([L_i, R_i] \) ;

  2. Select a parameter \ (W \) ;

  3. For an interval \ ([L_i, R_i] \) , mineral test value is calculated on this interval \ (Y_i \) :

$ Y_i = \ sum_j1 \ times \ sum_j {v_j}, \ j \ in [L_i, R_i] $ and $ w_j \ ge W, \ j $ ore number

Test results of these minerals \ (Y \) to test the value of each section and. Namely: \ (Y_1 + Y_2 + \ cdots + Y_M \) .

If these test results and minerals given standard value \ (S \) too much difference, you need to go check another batch of minerals. Small T do not want time-consuming to test another group of minerals, so he wanted to adjust the parameters \ (W \) values, so that the test results as much as possible close to the standard value \ (S \) , even if have to \ (S - Y \) the absolute minimum. Please help find the minimum value.

Input

Input of the first line contains three integers \ (n-\) , \ (m \) , \ (S \) , each number represents the number of standard values and ore interval;

The next \ (n-\) rows, each row \ (2 \) integers, separated by a space, the \ (i + 1 \) line represents \ (I \) by weight of the ore number \ (W_i \) and the value of \ (v_i \) ;

The next \ (m \) line, representing the interval, each row \ (2 \) integer, the intermediate separated by spaces, the \ (i + n + 1 \ ) line representing the interval \ ([L_i, R_i] \ ) two endpoints \ (L_i \) and \ (R_i \) . Note: Different intervals may coincide or overlap.

Output

An integer that indicates the minimum value required.

Sample Input

5 3 15
1 5
2 5
3 5
4 5
5 5
1 5
2 4
3 3

Sample Output

10

Sample Explain

When \ (W is \) selected from the group \ (4 \) when the value of the three test intervals were \ (20, 5, 0 \) , mineral test results for these \ (25 \) , then the standard value \ (S \) differ minimum \ (10 \) .

Hint

For \ (10 \% \) of data, there are \ (. 1 \ n-Le, m \ Le 10 \) ;

For \ (30 \% \) of data, there are \ (. 1 \ n-Le, m \ Le 500 \) ;

For \ (50 \% \) data, there are \ (. 1 \ n-Le, m \ Le 5,000 \) ;

For \ (70 \% \) of data, there are \ (. 1 \ n-Le, m \ 10,000 Le \) ;

对于\ (100 \% \)的数据,有\ (1 \ k, m \ 200,000, 0 <w_i, v_i \ 10 ^ 6,0 <P \ 10 ^ {12}, 1 \ The L_i \ the R_i \ k \) .

Solution

This question directly \ ([0, \ max { w [i]}] \) bipartite enumeration \ (W is \) , for each of the enumerated \ (W \) , each test force calculating section value and where the use of prefixes and optimization.

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long LL;
const LL INF = 0x7f7f7f7f7f7f7f7f7f7f;//把ans的初始值设大一点,否则会WA很多
const int MAXN = 200005;
int n, m, w[MAXN], v[MAXN], L[MAXN], R[MAXN];
LL S, l, r, mid, ans, sum1[MAXN], sum2[MAXN];//注意开long long
inline bool check(LL x) {
    for (int i = 1; i <= n; i++)
        if (x <= w[i]) {//如果符合要求的化
            sum1[i] = sum1[i - 1] + 1;
            sum2[i] = sum2[i - 1] + v[i];
        } else {
            sum1[i] = sum1[i - 1];
            sum2[i] = sum2[i - 1];
        }
    LL s = 0;
    for (int i = 1; i <= m; i++)
        s += (sum2[R[i]] - sum2[L[i] - 1]) * (sum1[R[i]] - sum1[L[i] - 1]);//暴力计算每一个区间,累加起来
    if (ans > fabs(s - S)) ans = fabs(s - S);//计算与标准值相差的最小值
    if (S > s) return 1; else return 0;
}
int main() {
    scanf("%d%d%lld", &n, &m, &S);
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &w[i], &v[i]);
        if (w[i] > r) r = w[i];//求区间的右边界(取w[i]的最大值)
    }
    for (int i = 1; i <= m; i++)
        scanf("%d%d", &L[i], &R[i]);
    r++;
    l = 0;
    ans = INF;
    while (l < r) {
        mid = l + r >> 1;//二分枚举
        if (check(mid)) r = mid; else l = mid + 1;//如果大于标准值就往降低要求,否则就提高要求
    }
    printf("%lld\n", ans);
    return 0;
}

Attachment

Test data Download: https://www.lanzous.com/i527v3i

Guess you like

Origin www.cnblogs.com/shenxiaohuang/p/11221159.html