QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#569106#9310. Permutation Counting 4yylxWA 0ms3620kbC++143.3kb2024-09-16 20:33:332024-09-16 20:33:33

Judging History

你现在查看的是最新测评结果

  • [2024-09-18 14:56:40]
  • hack成功,自动添加数据
  • (/hack/835)
  • [2024-09-18 14:41:06]
  • hack成功,自动添加数据
  • (/hack/831)
  • [2024-09-17 12:14:52]
  • hack成功,自动添加数据
  • (/hack/825)
  • [2024-09-16 20:33:33]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3620kb
  • [2024-09-16 20:33:33]
  • 提交

answer

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Interval {
    int l1, r1; // 第一个区间
    int l2, r2; // 第二个区间(可能不存在)

    Interval(int l = 0, int r = -1) : l1(l), r1(r), l2(0), r2(-1) {}

    // 判断在某一列是否有 1
    bool hasOne(int col) const {
        return (l1 <= col && col <= r1) || (l2 <= col && col <= r2);
    }

    // 计算当前行与另一行的对称差(模 2 加法)
    void xorWith(const Interval& other) {
        vector<pair<int, int>> intervals;

        // 将当前行的区间和另一行的区间全部加入
        intervals.push_back({l1, r1});
        if (l2 <= r2) intervals.push_back({l2, r2});
        intervals.push_back({other.l1, other.r1});
        if (other.l2 <= other.r2) intervals.push_back({other.l2, other.r2});

        // 排序并合并区间,根据奇偶性决定是否保留
        sort(intervals.begin(), intervals.end());
        vector<pair<int, int>> result;
        int count = 0;
        int start = -1, end = -1;

        for (auto& interval : intervals) {
            if (count % 2 == 0) {
                // 开始新的区间
                start = interval.first;
                end = interval.second;
            } else {
                // 更新当前区间
                end = max(end, interval.second);
            }
            count++;
            if (count % 2 == 0) {
                // 完成一个区间
                result.push_back({start, end});
            }
        }

        // 更新当前行的区间
        if (result.size() == 0) {
            l1 = 0; r1 = -1;
            l2 = 0; r2 = -1;
        } else if (result.size() == 1) {
            l1 = result[0].first; r1 = result[0].second;
            l2 = 0; r2 = -1;
        } else {
            l1 = result[0].first; r1 = result[0].second;
            l2 = result[1].first; r2 = result[1].second;
        }
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<Interval> rows(n);
        for (int i = 0; i < n; ++i) {
            int l, r;
            cin >> l >> r;
            rows[i] = Interval(l - 1, r - 1); // 调整为 0 索引
        }

        bool detZero = false;
        for (int i = 0; i < n; ++i) {
            int pivot_col = rows[i].l1;

            // 如果当前行在 pivot_col 没有 1,尝试寻找下面的行进行交换
            if (!rows[i].hasOne(pivot_col)) {
                bool found = false;
                for (int j = i + 1; j < n; ++j) {
                    if (rows[j].hasOne(pivot_col)) {
                        swap(rows[i], rows[j]);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    detZero = true;
                    break;
                }
            }

            // 对下面的行进行消元
            for (int j = i + 1; j < n; ++j) {
                if (rows[j].hasOne(pivot_col)) {
                    rows[j].xorWith(rows[i]);
                }
            }
        }

        cout << (detZero ? 0 : 1) << '\n';
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3620kb

input:

4
5
1 2
1 5
1 2
1 2
2 2
5
1 1
2 4
2 3
5 5
3 4
5
3 5
1 2
3 4
3 5
3 3
5
1 5
1 4
4 5
5 5
1 2

output:

1
1
1
1

result:

wrong answer 1st words differ - expected: '0', found: '1'