a542簡單混色需求分解成三個累積器

CPE:Pixel Art (UVa 13171)

把每種顏色對應到所需原色,再逐一扣除。

前往題目練習 →回報教學錯誤每筆時間 O(|S|),額外空間 O(1)(不含輸入字串)。

解題思路

依顏色需求表分解:M、R、V、B 需洋紅,Y、R、G、B 需黃,C、G、V、B 需青。白色 W 不使用任何原色。三種顏料獨立統計,因此不需要模擬混色順序或尋找配方。

掃描完整像素字串,對所需原色各減 1。最後若任一剩餘值小於 0,表示有一種原料不足,輸出 NO;全部非負就依 M、Y、C 次序輸出 YES 與剩餘量。即使提早知道不足,仍要讀完整幅畫,不能讓後續資料錯位。

Step-by-step animation

示意:庫存 M=3、Y=2、C=2,畫 RVC

回報動畫錯誤
步驟 1 / 3

畫 R

紅色用 M+Y。

M:2
Y:1
C:2
已處理
1

Reference solution

回報程式錯誤

完整程式

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <tuple>
#include <functional>
#include <limits>
#include <cstdint>
#include <cctype>
#include <utility>
using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        long long m, y, c;
        string s;
        cin >> m >> y >> c >> s;
        for (char ch : s) {
            if (string("MRVB").find(ch) != string::npos)
                --m;
            if (string("YRGB").find(ch) != string::npos)
                --y;
            if (string("CGVB").find(ch) != string::npos)
                --c;
        }
        if (min({m, y, c}) < 0)
            cout << "NO\n";
        else
            cout << "YES " << m << ' ' << y << ' ' << c << '\n';
    }
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多