A strip of N cells must each be painted one of 3 c...

Tạo vào: 26 tháng 4, 2026

Trả lời bằng GPT-5.5 Thinking bởi Chat01

Câu hỏi

A strip of N cells must each be painted one of 3 colors (R, G, B

Painting cell i with color c costs cost didanis

[c]

A streak of color c is a maximal run of consecutive cells all painted c. A streak of length Learns a streak bonus of Lx Q (Q is a given constant)

Minimize total painting cost-total streak bonuses

Find the minimum total cost.

Input Format

The first line contains a integer, n, denoting the number of cells.

alic

The second line contains a integer, q, denoting the bonus per cell in any same-color streak.

Each of the n lines contains 3 space-separated integers, representing row i of cost.

alia.

Constraints

1 <= n <= 10^5

0 <= q <= 10^4

ish066

1 <= cost[i][j] <=

10^4

Sample Test Cases

Case 1
Input:

3

10

5 20 30

50 5 40

100 100 5

alidanish0666

Output:

-15

Explanation:

n = 3 q = 10 Cheapest per cell: R(5), G(5), B(5) paint cost 3 * 10 = 30 Net = 15 alidanish06625 boxed 3theta = - 15 . . Bonus =

Case 2

Input:

2

0

10 20 30

30 20 10

Output:

20

alidanish0666

Explanation:

n = 2 q = 0 Cheapest per cell: R(10), B(10) paint cost 20. Bonus = 2 x 0 0. Net 200 = 20.

Case 3

Input:

4

3

8 15 6

10 4 20

7 12 9

3 18 11

alidanish0666

Output:

8

Explanation:

pish0666

Total paint cost Streak bonusev +4+7+ 3 = 20 every cell contributes exactly Q to the total bonus regardless of coloring, so total bonus =NxQ= 4 x 3 12 Net 2012 = 8

#include <bits/stdc++.h>

using namespace std;

int solve(int n, int q, vector<vector<int>>& cost) ( } // Write your solution here

int main() {

ios_base::sync_with_stdio(false);

cin.tie(NULL);

int n; cin >> n;

int q; cin >> q;

vector<vector<int>> cost(n, vector<int>(3));

for (int i = 0; i < n; i++)

for (int j = 0; j < 3; j++) cin >> cost[i][j];

auto result = solve(n, q, cost);

cout << result << endl;

return 0;

}
Please Give me proper code without explanation also I mentioned in the last

Suy nghĩ

Thought for 5s

Trả lời

cpp
#include <bits/stdc++.h> using namespace std; long long solve(int n, int q, vector<vector<int>>& cost) { long long totalCost = 0; for (int i = 0; i < n; i++) { totalCost += min({cost[i][0], cost[i][1], cost[i][2]}); } long long totalBonus = 1LL * n * q; return totalCost - totalBonus; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int q; cin >> q; vector<vector<int>> cost(n, vector<int>(3)); for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { cin >> cost[i][j]; } } cout << solve(n, q, cost) << endl; return 0; }

Chia sẻ Q&A này