首页 > 其他分享 >C. Heavy Intervals

C. Heavy Intervals

时间:2023-12-27 22:44:41浏览次数:33  
标签:Heavy 10 cdot sum intervals int Intervals test

C. Heavy Intervals

You have $n$ intervals $[l_1, r_1], [l_2, r_2], \dots, [l_n, r_n]$, such that $l_i < r_i$ for each $i$, and all the endpoints of the intervals are distinct.

The $i$-th interval has weight $c_i$ per unit length. Therefore, the weight of the $i$-th interval is $c_i \cdot (r_i - l_i)$.

You don't like large weights, so you want to make the sum of weights of the intervals as small as possible. It turns out you can perform all the following three operations:

  • rearrange the elements in the array $l$ in any order;
  • rearrange the elements in the array $r$ in any order;
  • rearrange the elements in the array $c$ in any order.

However, after performing all of the operations, the intervals must still be valid (i.e., for each $i$, $l_i < r_i$ must hold).

What's the minimum possible sum of weights of the intervals after performing the operations?

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of intervals.

The second line of each test case contains $n$ integers $l_1, l_2, \ldots, l_n$ ($1 \le l_i \le 2 \cdot 10^5$) — the left endpoints of the initial intervals.

The third line of each test case contains $n$ integers $r_1, r_2, \ldots, r_n$ ($l_i < r_i \le 2 \cdot 10^5$) — the right endpoints of the initial intervals.

It is guaranteed that $\{l_1, l_2, \dots, l_n, r_1, r_2, \dots, r_n\}$ are all distinct.

The fourth line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^7$) — the initial weights of the intervals per unit length.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output

For each test case, output a single integer: the minimum possible sum of weights of the intervals after your operations.

Example

input

2
2
8 3
12 23
100 100
4
20 1 2 5
30 4 3 10
2 3 2 3

output

2400
42

Note

In the first test case, you can make

  • $l = [8, 3]$;
  • $r = [23, 12]$;
  • $c = [100, 100]$.

In that case, there are two intervals:

  • interval $[8, 23]$ with weight $100$ per unit length, and $100 \cdot (23-8) = 1500$ in total;
  • interval $[3, 12]$ with weight $100$ per unit length, and $100 \cdot (12-3) = 900$ in total.

The sum of the weights is $2400$. It can be shown that there is no configuration of final intervals whose sum of weights is less than $2400$.

In the second test case, you can make

  • $l = [1, 2, 5, 20]$;
  • $r = [3, 4, 10, 30]$;
  • $c = [3, 3, 2, 2]$.

In that case, there are four intervals:

  • interval $[1, 3]$ with weight $3$ per unit length, and $3 \cdot (3-1) = 6$ in total;
  • interval $[2, 4]$ with weight $3$ per unit length, and $3 \cdot (4-2) = 6$ in total;
  • interval $[5, 10]$ with weight $2$ per unit length, and $2 \cdot (10-5) = 10$ in total;
  • interval $[20, 30]$ with weight $2$ per unit length, and $2 \cdot (30-20) = 20$ in total.

The sum of the weights is $42$. It can be shown that there is no configuration of final intervals whose sum of weights is less than $42$.

 

解题思路

  首先为了使得分数最小,根据排序不等式应该让数组 $c$ 以值为关键字,区间以长度为关键字,一个升序另外一个降序,然后对应项相乘并对乘积的结果求和。

  另外容易知道对于任意合法的区间构造方案(即区间的右端点大于左端点),区间的总长度是一个定值 $\sum\limits_{i=1}^{n}{r_i} - \sum\limits_{i=1}^{n}{l_i}$。因此在这个前提下,应该如何构造区间才能使得分数最小。

  考虑例子 $l = [1, 2], \, r = [3, 5], \, c = [1, 2]$。有两种构造方案,分别是 $[1, 3]$ 和 $[2, 5]$,对应的分数是 $(5 - 2) \cdot 1 + (3 - 1) \cdot 2 = 7$。另外一种是 $[1, 5]$ 和 $[2, 3]$,对应的分数是 $(5 - 1) \cdot 1 + (3 - 2) \cdot 2 = 6$。很明显第二种方案会更优,可以发现第一种方案中两个区间是相交的(非包含),而第二种方案的区间是包含的,可以证明把两个相交的区间变成包含关系,分数会变小。

  假设现在有两个区间 $[l_1, r_1]$ 和 $[l_2, r_2]$,并且满足 $l_1 < l_2 < r_1 < r_2$,$r_1 - r_1 \leq r_2 - l_2$,$c_1 \leq c_2$,那么此时分数是 $(r_2 - l_2) \cdot c_1 + (r_1 - l_1) \cdot c_2 \ldots (1)$。将 $r_1$ 和 $r_2$ 交换,区间变成包含关系,分数变成了 $(r_2 - l_1) \cdot c_1 + (r_1 - l_2) \cdot c_2 \ldots (2)$。令 $(1) - (2)$ 得到 $(l_2 - l_1) \cdot (c_2 - c_1) \geq 0$,得证。

  因此只要存在两个区间是非包含的相交关系,则将其变成包含关系,那么最后得到的 $n$ 个区间中的任意两个区间都满足包含关系。要得到这些区间的方法是把所有的 $l_i$ 和 $r_i$ 放到一起排序,从小到大枚举,如果发现是左端点的值,则将其压入栈中,如果是右端点的值,则与栈顶元素匹配变成一个区间,并把栈顶元素弹出。最后把得到的 $n$ 个区间按长度排序,与数组 $c$ 对应项相乘并求和即可。

  AC 代码如下,时间复杂度为 $O(n \log{n})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int a[N], b[N], c[N], p[N];
int stk[N];

void solve() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", b + i);
    }
    for (int i = 0; i < n; i++) {
        scanf("%d", c + i);
    }
    sort(a, a + n);
    sort(b, b + n);
    sort(c, c + n);
    for (int i = 0, j = 0, k = 0, tp = 0; k < n; ) {
        if (i < n && a[i] < b[j]) stk[++tp] = a[i++];
        else p[k++] = b[j++] - stk[tp--];
    }
    sort(p, p + n, greater<int>());
    LL ret = 0;
    for (int i = 0; i < n; i++) {
        ret += 1ll * p[i] * c[i];
    }
    printf("%lld\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Editorial of Codeforces Round 917 (Div. 2):https://codeforces.com/blog/entry/123721

  Codeforces Pinely Round 3 讲解(ABCDF1):https://www.bilibili.com/BV1iG411k7vX

标签:Heavy,10,cdot,sum,intervals,int,Intervals,test
From: https://www.cnblogs.com/onlyblues/p/17931232.html

相关文章

  • [题解] AT_dp_w Intervals
    Intervals有\(m\)条形如\((l,r,a)\)的限制,表示如果\(s_{[l,r]}\)中有1就会有\(a\)的价值。你要求长度为\(n\)的01串的价值的最大值。\(n,m\le2\times10^5\)。将每个限制挂到右端点上,在右端点处计算贡献。然后我们就只关心最后一个1出现的位置了。......
  • Soil pollution--Measures to control soil polluted by heavy metals
    Onespecificmeasure:strengthenpreventionandcontrolofsoilpollutionatitssource(fromOpinionsoftheCPCCentralCommitteeandTheStateCouncilonDeepeningtheBattleagainstPollution)Thedocumentrequiredthateffortswillbemadetoprevent......
  • 【刷题笔记】56. Merge Intervals
    题目Givenacollectionofintervals,mergealloverlappingintervals.Example1:Input:[[1,3],[2,6],[8,10],[15,18]]Output:[[1,6],[8,10],[15,18]]Explanation:Sinceintervals[1,3]and[2,6]overlaps,mergetheminto[1,6].Example2:Input:[[1,4],[4,5......
  • Best Heavy Duty Truck Diagnostic Software Of 2023 Completed List
    Diagnostictoolsareessentialintheautomotiveindustryforidentifyingandresolvingissueswithvehicles.Thesetoolsprovidetechnicianswiththenecessaryinformationtodiagnoseandrepairproblemsefficiently.Inthisarticle,wewillexplorethe......
  • poj 1716 Integer Intervals (贪心)
    题意:给定n个连续的区间,求一个集合。其中,n个区间的每一个区间都至少包含两个这个集合的元素。求这个集合元素的最小值。 题解:1、先将所有区间按终点从小到大排序。2、我们先取第一个区间(排好序的区间)的两个值,因为要使得结果最优,我们肯定选择最接近终点的那两个值。假设一个为Selem,......
  • R语言HAR和HEAVY模型分析高频金融数据波动率|附代码数据
    全文链接:http://tecdat.cn/?p=19129最近我们被客户要求撰写关于HAR和HEAVY模型的研究报告,包括一些图形和统计输出。在本文中,在学术界和金融界,分析高频财务数据的经济价值现在显而易见。摘要它是每日风险监控和预测的基础,也是高频交易的基础。为了在财务决策中高效利用高频数据......
  • POJ 1797 Heavy Transportation(迪杰斯特拉最短路变形)
    传送门题意分析:Hugo想要扩展他的公司,他有起重机要到目的地,到达目的地有很多条路径,但是,每一条路都有相应承重量,现在需要找出到达目的地的最大承重道路的承重质量。解题分析:首先,每一条路径的承重量取决于承重量最小的那条道路(短板效应),所以就是找所有路径的最小值,然后选择最小值最大的......
  • CF1034D Intervals of Intervals
    简要题意给定\(n\)个区间组成的序列,定义它的一个连续段的价值为这个段内所有区间的并覆盖的长度。求价值前\(k\)大的段的价值和。数据范围:\(1\len\le3\times10^5,1\lek\le\min\{\frac{n(n-1)}{2},10^9\}\)。题解考虑一个经典问题,\(q\)次询问求某个连续段的价值。......
  • CF1034D Intervals of Intervals 题解
    传送门CF1034DIntervalsofIntervals题目大意有\(n\)个线段,第\(i\)个是\([a_i,b_i]\)。定义区间\([l,r]\)的价值是第\(l\)个线段到第\(r\)个线段的并的长度。找出\(k\)个不同的区间,使得总价值最大。输出最大总价值。\(1\len\le3\times10^5,1\lek\le......
  • ARC111C Too Heavy 题解
    无解的情况:当且仅当一个人手上的物品不是自己的物品,并且这个物品的质量大于自己的体重,这个不是自己的东西就卡手了,换不出去,无解。甲手上是乙的物品。乙的手上是丙的物品,丙......