题目 › fcs-193

FrontierCS #193

FrontierCS 算法赛道 v1.1 · 2026-05 · L5 开放式 · C++17 · 时限 1 秒 · 0–100 连续分

查看原题跳到结果

题面

来源:FrontierCS 官方 statement.txt,与模型收到的题面相同。

# Max-2-SAT ## Problem You are given a Boolean formula in CNF form, where each clause contains exactly two literals. Your task is to assign truth values to variables to satisfy as many clauses as possible. ## Input Format - Line 1: two integers n and m (1 ≤ n ≤ 1000, 0 ≤ m ≤ 40000) - Next m lines: two integers a b - Each integer is in [-n, n], non-zero - Positive x means variable x - Negative -x means ¬x Each clause is (a ∨ b). ## Output Format - Output exactly one line: - n integers x₁ x₂ … xₙ - each xᵢ ∈ {0, 1} - 1 means TRUE, 0 means FALSE ## Scoring The physical objective is number of satisfied clauses t; larger is better. The physical reference is `a = 0.6*m`; matching a positive reference earns 60 points. Here m is the total number of clauses. Define the historical low-score expression (in points): P_low = 100*t/m. If `P_low<=60`, retain `max(0,P_low)`. For `P_low>60` and a positive physical reference and objective, award: r = t / a P = min(100, 60 + ln(r)). Here `r` is the improvement factor relative to the physical reference, and `ln` is the natural logarithm with coefficient 1. In the uncapped upper branch, `r = exp(P-60)`: each additional point means an `exp(1)`-fold improvement in the physical objective. The factor is computed from the physical objective and its reference, not from a transformed reward or the old point score. Invalid output retains its existing rejection or zero score; the zero-cost exception applies only after validation. Use one score per file. Both reported ratio channels use points divided by 100. Across official files, take the arithmetic mean. For m=0, the empty instance retains 100 points.

结果

Kimi K3.1 ProKimi

max

60.4

平均得分 ↑

  • 60.4采样 1 · 部分分
  • 16.7k平均输出 token(含思考) ↓
  • 29.5输出速度 tokens/s ↑
  • 566 秒延迟 ↓

Kimi K3.1 Pro(high)Kimi high

high

60.4

平均得分 ↑

  • 60.4采样 1 · 部分分
  • 7.9k平均输出 token(含思考) ↓
  • 30.2输出速度 tokens/s ↑
  • 264 秒延迟 ↓

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

60.4

平均得分 ↑

  • 60.4采样 1 · 部分分
  • 19.1k平均输出 token(含思考) ↓
  • 195.4输出速度 tokens/s ↑
  • 98 秒延迟 ↓

MiniMax M3.1 FlashMiniMax

auto(max)

60.4

平均得分 ↑

  • 60.4采样 1 · 部分分
  • 51.7k平均输出 token(含思考) ↓
  • 104.0输出速度 tokens/s ↑
  • 498 秒延迟 ↓

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 374 行
#include <bits/stdc++.h>
using namespace std;

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

    int n, m;
    if (!(cin >> n >> m)) return 0;

    vector<pair<int, int>> clauses(m);
    auto literalOf = [](int a) {
        int v = abs(a) - 1;
        return 2 * v + (a < 0);
    };

    for (int i = 0; i < m; ++i) {
        int a, b;
        cin >> a >> b;
        clauses[i] = {literalOf(a), literalOf(b)};
    }

    if (m == 0) {
        for (int i = 0; i < n; ++i) {
            if (i) cout << ' ';
            cout << 0;
        }
        cout << '\n';
        return 0;
    }

    // First check whether all clauses can be satisfied using 2-SAT.
    int N = 2 * n;
    vector<vector<int>> g(N), rg(N);
    auto addImplication = [&](int a, int b) {
        g[a].push_back(b);
        rg[b].push_back(a);
    };

    for (auto [a, b] : clauses) {
        addImplication(a ^ 1, b);
        addImplication(b ^ 1, a);
    }

    vector<int> order;
    order.reserve(N);
    vector<char> used(N, 0);

    function<void(int)> dfs1 = [&](int v) {
        used[v] = 1;
        for (int to : g[v]) {
            if (!used[to]) dfs1(to);
        }
        order.push_back(v);
    };

    for (int v = 0; v < N; ++v) {
        if (!used[v]) dfs1(v);
    }

    vector<int> comp(N, -1);
    function<void(int, int)> dfs2 = [&](int v, int id) {
        comp[v] = id;
        for (int to : rg[v]) {
            if (comp[to] == -1) dfs2(to, id);
        }
    };

    int componentCount = 0;
    for (int i = N - 1; i >= 0; --i) {
        int v = order[i];
        if (comp[v] == -1) dfs2(v, componentCount++);
    }

    bool satisfiable = true;
    for (int i = 0; i < n; ++i) {
        if (comp[2 * i] == comp[2 * i + 1]) {
            satisfiable = false;
            break;
        }
    }

    if (satisfiable) {
        vector<uint8_t> answer(n);
        for (int i = 0; i < n; ++i) {
            answer[i] = comp[2 * i] > comp[2 * i + 1];
        }

        bool verified = true;
        for (auto [a, b] : clauses) {
            int va = answer[a >> 1] ^ (a & 1);
            int vb = answer[b >> 1] ^ (b & 1);
            if (!(va | vb)) {
                verified = false;
                break;
            }
        }

        if (verified) {
            for (int i = 0; i < n; ++i) {
                if (i) cout << ' ';
                cout << int(answer[i]);
            }
            cout << '\n';
            return 0;
        }
    }

    // Incremental local search for the unsatisfiable case.
    vector<vector<int>> adj(n);
    for (int c = 0; c < m; ++c) {
        int v1 = clauses[c].first >> 1;
        int v2 = clauses[c].second >> 1;
        adj[v1].push_back(c);
        if (v2 != v1) adj[v2].push_back(c);
    }

    mt19937 rng(0xC0FFEE);
    vector<uint8_t> x(n), bestAssignment(n);
    vector<int> satCount(m), unsat, positionInUnsat(m, -1);
    int currentScore = 0, bestScore = -1;

    auto literalValue = [&](int lit) -> int {
        return int(x[lit >> 1]) ^ (lit & 1);
    };

    auto addUnsat = [&](int c) {
        positionInUnsat[c] = int(unsat.size());
        unsat.push_back(c);
    };

    auto removeUnsat = [&](int c) {
        int p = positionInUnsat[c];
        int last = unsat.back();
        unsat[p] = last;
        positionInUnsat[last] = p;
        unsat.pop_back();
        positionInUnsat[c] = -1;
    };

    auto rebuild = [&]() {
        unsat.clear();
        fill(positionInUnsat.begin(), positionInUnsat.end(), -1);
        for (int c = 0; c < m; ++c) {
            int s = literalValue(clauses[c].first) +
                    literalValue(clauses[c].second);
            satCount[c] = s;
            if (s == 0) addUnsat(c);
        }
        currentScore = m - int(unsat.size());
    };

    auto updateBest = [&]() {
        if (currentScore > bestScore) {
            bestScore = currentScore;
            bestAssignment = x;
        }
    };

    auto calculateGain = [&](int v) {
        int delta = 0;
        for (int c : adj[v]) {
            int before = satCount[c];
            int after = before;

            int a = clauses[c].first;
            int b = clauses[c].second;
            if ((a >> 1) == v) after += literalValue(a) ? -1 : 1;
            if ((b >> 1) == v) after += literalValue(b) ? -1 : 1;

            if (before == 0 && after > 0) ++delta;
            else if (before > 0 && after == 0) --delta;
        }
        return delta;
    };

    auto flip = [&](int v) {
        x[v] ^= 1;
        for (int c : adj[v]) {
            int before = satCount[c];
            int after = literalValue(clauses[c].first) +
                        literalValue(clauses[c].second);
            satCount[c] = after;

            if (before == 0 && after > 0) {
                removeUnsat(c);
                ++currentScore;
            } else if (before > 0 && after == 0) {
                addUnsat(c);
                --currentScore;
            }
        }
    };

    auto startTime = chrono::steady_clock::now();
    const double TIME_LIMIT = 0.80;
    auto timeUp = [&]() {
        return chrono::duration<double>(
                   chrono::steady_clock::now() - startTime
               ).count() >= TIME_LIMIT;
    };

    vector<int> seen(n, 0);
    int seenToken = 0;

    struct QueueNode {
        int gain, variable, version;
        bool operator<(const QueueNode& other) const {
            return gain < other.gain;
        }
    };

    auto greedyAscent = [&]() {
        priority_queue<QueueNode> pq;
        vector<int> gains(n), versions(n, 0);

        for (int v = 0; v < n; ++v) {
            gains[v] = calculateGain(v);
            pq.push({gains[v], v, 0});
        }

        vector<int> affected;
        long long flips = 0;

        while (!pq.empty()) {
            if ((flips & 31) == 0 && timeUp()) return;

            QueueNode node = pq.top();
            pq.pop();

            int v = node.variable;
            if (node.version != versions[v] || node.gain != gains[v]) continue;
            if (node.gain <= 0) break;

            flip(v);
            updateBest();

            affected.clear();
            ++seenToken;
            auto touch = [&](int u) {
                if (seen[u] != seenToken) {
                    seen[u] = seenToken;
                    affected.push_back(u);
                }
            };

            touch(v);
            for (int c : adj[v]) {
                touch(clauses[c].first >> 1);
                touch(clauses[c].second >> 1);
            }

            for (int u : affected) {
                gains[u] = calculateGain(u);
                ++versions[u];
                pq.push({gains[u], u, versions[u]});
            }

            ++flips;
        }
    };

    // Try several inexpensive starting assignments.
    vector<int> positive(n, 0), negative(n, 0);
    for (auto [a, b] : clauses) {
        for (int lit : {a, b}) {
            if (lit & 1) ++negative[lit >> 1];
            else ++positive[lit >> 1];
        }
    }

    for (int v = 0; v < n; ++v) x[v] = positive[v] >= negative[v];
    rebuild();
    updateBest();

    fill(x.begin(), x.end(), 0);
    rebuild();
    updateBest();

    fill(x.begin(), x.end(), 1);
    rebuild();
    updateBest();

    for (int rep = 0; rep < 4; ++rep) {
        for (int v = 0; v < n; ++v) x[v] = rng() & 1;
        rebuild();
        updateBest();
    }

    x = bestAssignment;
    rebuild();
    if (!timeUp()) greedyAscent();

    x = bestAssignment;
    rebuild();

    long long iterations = 0;
    long long sinceImprovement = 0;
    long long restarts = 0;
    const long long maxIterations = max(300000LL, 3000LL * n);
    const long long stagnationLimit = max(20000LL, 40LL * n);

    while (iterations < maxIterations) {
        if ((iterations & 1023) == 0 && timeUp()) break;
        if (unsat.empty()) {
            updateBest();
            break;
        }

        if (sinceImprovement >= stagnationLimit) {
            ++restarts;

            if (restarts % 6 == 0) {
                for (int v = 0; v < n; ++v) x[v] = rng() & 1;
                rebuild();
                updateBest();
            } else {
                x = bestAssignment;
                rebuild();

                int range = max(1, n / 10);
                int perturbations = 1 + int(rng() % range);
                for (int j = 0; j < perturbations; ++j) {
                    flip(int(rng() % n));
                }
                updateBest();
            }

            if (restarts % 3 == 0 && !timeUp()) greedyAscent();
            sinceImprovement = 0;
            continue;
        }

        int c = unsat[rng() % unsat.size()];
        int a = clauses[c].first;
        int b = clauses[c].second;
        int v1 = a >> 1;
        int v2 = b >> 1;
        int chosen;

        int noise = 150 + int(350LL * min(sinceImprovement, stagnationLimit) /
                              stagnationLimit);

        if (v1 == v2) {
            chosen = v1;
        } else if (int(rng() % 1000) < noise) {
            chosen = (rng() & 1) ? v1 : v2;
        } else {
            int g1 = calculateGain(v1);
            int g2 = calculateGain(v2);
            if (g1 > g2) chosen = v1;
            else if (g2 > g1) chosen = v2;
            else chosen = (rng() & 1) ? v1 : v2;
        }

        flip(chosen);

        if (currentScore > bestScore) {
            updateBest();
            sinceImprovement = 0;
        } else {
            ++sinceImprovement;
        }

        ++iterations;
    }

    for (int i = 0; i < n; ++i) {
        if (i) cout << ' ';
        cout << int(bestAssignment[i]);
    }
    cout << '\n';
    return 0;
}