CF1799D2.Hot Start Up (hard version)
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is a hard version of the problem. The constraints of t, n, k are the only difference between versions.
You have a device with two CPUs. You also have k programs, numbered 1 through k, that you can run on the CPUs.
The i-th program (1≤i≤k) takes coldi seconds to run on some CPU. However, if the last program we ran on this CPU was also program i, it only takes hoti seconds (hoti≤coldi). Note that this only applies if we run program i multiple times consecutively — if we run program i, then some different program, then program i again, it will take coldi seconds the second time.
You are given a sequence a1,a2,…,an of length n, consisting of integers from 1 to k. You need to use your device to run programs a1,a2,…,an in sequence. For all 2≤i≤n, you cannot start running program ai until program ai−1 has completed.
Find the minimum amount of time needed to run all programs a1,a2,…,an in sequence.
这是一个该问题的困难版本。两个版本之间唯一的区别在于 t、n、k 的约束条件。
你拥有一台配备两个 CPU 的设备。此外,你还有 k 个编号为 1 至 k 的程序,可在 CPU 上运行。
第 i 个程序(1≤i≤k)在某个 CPU 上运行需要 coldi 秒。然而,若该 CPU 上次运行的程序也是程序 i,则本次运行仅需 hoti 秒(满足 hoti≤coldi)。注意,该优化仅适用于程序 i 在同一 CPU 上被连续多次运行的情形——例如,若先运行程序 i,再运行某个其他程序,之后再次运行程序 i,则第二次运行仍需 coldi 秒。
现给定一个长度为 n 的序列 a1,a2,…,an,其中每个元素均为 1 至 k 之间的整数。你需要使用该设备按顺序依次运行程序 a1,a2,…,an。对所有 2≤i≤n,程序 ai 的运行必须在程序 ai−1 完成后才能开始。
求按顺序运行全部程序 a1,a2,…,an 所需的最短总时间。
输入格式
Input consists of multiple test cases. The first line contains a single integer t, the number of test cases (1≤t≤105).
The first line of each test case contains n and k (1≤n,k≤3⋅105).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤k).
The third line of each test case contains k integers cold1,cold2,…,coldk (1≤coldi≤109).
The fourth line of each test case contains k integers hot1,hot2,…,hotk (1≤hoti≤coldi).
It is guaranteed the sum of n and the sum of k over all test cases do not exceed 3⋅105.
输入包含多个测试用例。第一行包含一个整数 t,表示测试用例的数量(1≤t≤105)。
每个测试用例的第一行包含两个整数 n 和 k(1≤n,k≤3⋅105)。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤k)。
每个测试用例的第三行包含 k 个整数 cold1,cold2,…,coldk(1≤coldi≤109)。
每个测试用例的第四行包含 k 个整数 hot1,hot2,…,hotk(1≤hoti≤coldi)。
保证所有测试用例中 n 的总和与 k 的总和均不超过 3⋅105。
输出格式
For each test case, print the minimum time needed to run all programs in the given order.
对于每个测试用例,输出按给定顺序运行所有程序所需的最短时间。
输入输出样例
输入#1
9 3 2 1 2 2 3 2 2 1 4 2 1 2 1 2 5 3 2 1 4 3 1 2 3 1 100 100 100 1 1 1 5 2 2 1 2 1 1 65 45 54 7 5 3 1 3 2 1 2 2 2 2 1 1 1 5 1 1 1 1 1 1 1000000000 999999999 5 6 1 6 1 4 1 3 6 4 1 4 5 1 1 1 1 4 1 1 3 3 4 5 6 1 2 3 8 3 3 3 3 1 2 3 2 1 10 10 8 10 10 5
输出#1
6 11 301 225 8 4999999996 11 6 63
说明/提示
In the first test case, we can do the following:
- Run program a1=1 on CPU 1. It takes cold1=3 seconds to run.
- Run program a2=2 on CPU 2. It takes cold2=2 seconds to run.
- Run program a3=2 on CPU 2. The last program run on this CPU was also program 2, so it takes hot2=1 second to run.
In total, we need 3+2+1=6 seconds to run them all. We can show this is optimal.
In the second test case, we can use do the following:
- Run program a1=1 on CPU 1. It takes cold1=5 seconds to run.
- Run program a2=2 on CPU 2. It takes cold2=3 seconds to run.
- Run program a3=1 on CPU 1. The last program run on this CPU was also program 1, so it takes hot1=2 seconds to run.
- Run program a4=2 on CPU 2. The last program run on this CPU was also program 2, so it takes hot2=1 second to run.
In total, we need 5+3+2+1=11 seconds. We can show this is optimal.
在第一个测试用例中,我们可以执行以下操作:
- 在 CPU 1 上运行程序 a1=1。其运行耗时为 cold1=3 秒。
- 在 CPU 2 上运行程序 a2=2。其运行耗时为 cold2=2 秒。
- 在 CPU 2 上运行程序 a3=2。该 CPU 上次运行的程序也是程序 2,因此其运行耗时为 hot2=1 秒。
总计需要 3+2+1=6 秒来完成全部运行。可以证明这是最优方案。
在第二个测试用例中,我们可以执行以下操作:
- 在 CPU 1 上运行程序 a1=1。其运行耗时为 cold1=5 秒。
- 在 CPU 2 上运行程序 a2=2。其运行耗时为 cold2=3 秒。
- 在 CPU 1 上运行程序 a3=1。该 CPU 上次运行的程序也是程序 1,因此其运行耗时为 hot1=2 秒。
- 在 CPU 2 上运行程序 a4=2。该 CPU 上次运行的程序也是程序 2,因此其运行耗时为 hot2=1 秒。
总计需要 5+3+2+1=11 秒。可以证明这是最优方案。
输入解题思路,AI测评打分。不知道怎么写?