CF1440A Buy the String 題解
阿新 • • 發佈:2021-12-16
CF1440A Buy the String 題解
,把所有的 \(1\) 刪掉的最小代價是 \(c_1\times\min(cnt_1,cnt_0+h)\)。為什麼呢?因為你要把所有的 \(0\) 改成 \(1\) 或者把所有的 \(1\) 改成 \(0\),都需要額外的代價,所以我們取最小值就可以使得代價最小。
Content
有 \(t\) 組詢問,每組詢問給出一個長度為 \(n\) 的 \(0/1\) 串,你可以花 \(h\) 的代價把 \(0\) 修改成 \(1\) 或者把 \(1\) 修改成 \(0\),也可以花 \(c_x\) 的代價刪除一個 \(x\in[0,1]\)。求使得 \(0/1\) 串為空的最小代價。
資料範圍:\(1\leqslant t\leqslant 10,1\leqslant n,c_0,c_1,h\leqslant 1000\)。
Solution
設 \(x\) 在字串中出現的次數為 \(cnt_x\),那麼我們很明顯地發現,把所有的 \(0\) 刪掉的最小代價是 \(c_0\times\min(cnt_0,cnt_1+h)\)
Code
int t, n, c0, c1, h, a[1007]; int main() { t = Rint; while(t--) { n = Rint, c0 = Rint, c1 = Rint, h = Rint; int cnt0 = 0, cnt1 = 0, ans = 0; F(i, 1, n) {scanf("%1d", &a[i]); cnt0 += (a[i] == 0), cnt1 += a[i];} ans = cnt0 * min(c0, c1 + h) + cnt1 * min(c1, c0 + h); printf("%d\n", ans); } return 0; }