51分求条
查看原帖
51分求条
1461183
haohao_com楼主2025/1/26 12:03

只有51分

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Constraint {
    long long count; // 数量
    long long value; // 保镖数量
};

// 比较函数,用于排序
bool compare(const Constraint &a, const Constraint &b) {
    return a.value > b.value;
}

bool solve() {
    long long R, C;
    cin >> R;

    vector<Constraint> rows(R);
    for (long long i = 0; i < R; ++i) {
        cin >> rows[i].value >> rows[i].count;
    }

    cin >> C;
    vector<Constraint> cols(C);
    for (long long i = 0; i < C; ++i) {
        cin >> cols[i].value >> cols[i].count;
    }

    // 计算总保镖数
    long long total_row_guards = 0, total_col_guards = 0;
    for (const auto &row : rows) {
        total_row_guards += row.value * row.count;
    }
    for (const auto &col : cols) {
        total_col_guards += col.value * col.count;
    }

    // 如果总保镖数不一致,直接返回无解
    if (total_row_guards != total_col_guards) {
        return false;
    }

    // 按照保镖数量从大到小排序
    sort(rows.begin(), rows.end(), compare);
    sort(cols.begin(), cols.end(), compare);

    long long row_index = 0, col_index = 0;

    // 贪心分配
    while (row_index < R && col_index < C) {
        long long a = rows[row_index].value;
        long long b = rows[row_index].count;
        long long x = cols[col_index].value;
        long long y = cols[col_index].count;

        if (a > x) {
            return false; // 无法满足约束
        }

        if (a == x) {
            if (b > y) {
                return false; // 无法满足约束
            } else {
                y -= b;
                if (y == 0) {
                    ++col_index;
                }
                ++row_index;
            }
        } else {
            if (b > y) {
                return false; // 无法满足约束
            } else {
                b -= y;
                if (b == 0) {
                    ++row_index;
                }
                ++col_index;
            }
        }
    }

    // 如果所有行和列的约束都能满足
    return row_index == R && col_index == C;
}

int main() {
    if (solve()) {
        cout << "1" << endl;
    } else {
        cout << "0" << endl;
    }
    return 0;
}
2025/1/26 12:03
加载中...