QOJ.ac

QOJ

Time Limit: 2.0 s Memory Limit: 256 MB Total points: 100 Hackable ✓

#14096. 彩色排列排序

Statistics

畀你一個由 $1$ 到 $n$ 嘅整數組成嘅排列 $p_1,p_2,\ldots,p_n$。由 $1$ 到 $n$ 嘅每個位置都塗咗 $k$ 種顏色其中一種。我哋想將個排列排序,為此可以做任意多次以下類型嘅操作:

  • 交換任意兩個元素。呢個操作要花 $S$ 個硬幣;
  • 揀任意一種顏色 $i$,隨意重排顏色係 $i$ 嘅位置上面嘅元素。呢個操作要花 $C_i$ 個硬幣。

留意,有顏色嘅係位置,唔係元素,所以交換兩個元素嗰陣,位置嘅顏色唔會變。

求將個排列排序最少要花幾多個硬幣。

輸入格式

輸入第一行有一個整數 $T$($1\le T\le10^3$),代表你要處理嘅獨立測試案例數目。之後係各個測試案例嘅描述。

每個測試案例第一行有兩個整數 $n,k$($1\le n\le10^5$,$1\le k\le5$),代表排列大小同顏色數目。

每個測試案例第二行有 $k+1$ 個整數 $S,C_1,C_2,\ldots,C_k$($0\le S,C_i\le10^9$),代表操作嘅費用。

每個測試案例第三行有 $n$ 個整數 $p_1,p_2,\ldots,p_n$($1\le p_i\le n$,所有 $p_i$ 都唔同),代表個排列。

每個測試案例第四行有 $n$ 個整數 $col_i$($1\le col_i\le k$),代表各個位置嘅顏色。

同一個輸入檔案入面,所有測試案例嘅 $n$ 加埋唔超過 $10^5$。

輸出格式

每個測試案例輸出一個整數,代表將個排列排序最少要花嘅硬幣數目。

樣例

輸入格式 1

4
4 1
1 10
2 3 4 1
1 1 1 1
4 1
10 1
2 3 4 1
1 1 1 1
6 2
10 1 1
5 2 4 6 1 3
1 2 1 2 1 2
4 3
6 7 8 9
1 2 3 4
2 2 3 2

輸出格式 1

3
1
12
0

說明

第一個測試案例,我哋可以做 3 次「交換」操作嚟排序:$(2,3,4,1)\to(4,3,2,1)\to(4,2,3,1)\to(1,2,3,4)$。咁樣會花 3 個硬幣。

另一個方法係重排所有顏色係 $1$ 嘅位置上面嘅元素,但咁樣要花 10 個硬幣,而我哋可以用更少錢做到。

不過,第二個測試案例(同第一個案例只有操作費用唔同)直接花 1 個硬幣重排所有顏色係 $1$ 嘅位置上面嘅元素會更平。

第三個測試案例,其中一個最優操作序列如下:

  • 重排顏色係 $2$ 嘅位置上面嘅元素,得到排列 $(5,2,4,3,1,6)$。呢個操作花 1 個硬幣。
  • 交換元素 $p_3,p_4$。個排列而家係 $(5,2,3,4,1,6)$。呢個操作花 10 個硬幣。
  • 重排顏色係 $1$ 嘅位置上面嘅元素,得到排列 $(1,2,3,4,5,6)$。呢個操作花 1 個硬幣。

總共花咗 12 個硬幣。

第四個測試案例嘅排列已經排好序,所以唔使花任何硬幣。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.