a911困難

98 全國資訊學科能力競賽(2009)|計算執行路徑的可行性

利用迴圈次數縮小初始參數。

解題思路

令路徑中19與21的總次數為 L。進入迴圈時 a 必須恰為 L(或零次時非正),而迴圈前 a 只可能是10x₀²、10x₀²−10、10x₀²+90,故必有10x₀²≤L+10。只需枚舉極少的 x₀。

賦值2後 x=A=10x₀²。若原 y≤A,枚舉0到A;若原 y>A,行4將其改成A,後續不再依賴原 y,最小原值必為A+1。因此只需枚舉 y₀=0..A+1。

沿指定路徑模擬 x、y、a。遇到涉及 z 的6、12、14時,依下一節點將嚴格不等式換成 z 的整數上下界;其他分支和所有控制流程邊直接檢查。最後取非負下界 z 並最小化原 x₀+y₀+z。若上下界矛盾、流程錯誤或迴圈次數不符就輸出inf。

若路徑長P、L為迴圈次數,時間 O(P(L+10)^(3/2)),空間 O(P);題目P≤1000。

Step-by-step animation

教學示意(以步驟描述中的輸入為準)

回報動畫錯誤
步驟 1 / 3

示意零次路徑

1 2 3 5 6 12 13 16 17 23 24。

1
2
3
5
6
12
13
16
17
23
24
L
0

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
long long test(const vector<int> &p, long long x, long long y) {
    long long a = 0, lo = 0, hi = INT_MAX;
    map<int, int> edge = {{1, 2},   {2, 3},   {4, 5},   {5, 6},   {8, 11},
                          {9, 10},  {10, 11}, {11, 16}, {13, 16}, {15, 16},
                          {16, 17}, {19, 22}, {20, 21}, {21, 22}, {22, 23}};
    for (int i = 0; i + 1 < (int)p.size(); i++) {
        int u = p[i], v = p[i + 1], want = -1;
        if (u == 2)
            a = 10 * x * x, x = a;
        if (u == 4)
            y = a;
        if (u == 8)
            x += 10;
        if (u == 10)
            y -= 9;
        if (u == 13)
            a -= 10;
        if (u == 15)
            a += 90;
        if (u == 19)
            x++, a--;
        if (u == 21)
            y--, a--;
        if (u == 3)
            want = x < y ? 4 : 5;
        else if (u == 7)
            want = x < y ? 8 : 9;
        else if (u == 17)
            want = a > 0 ? 18 : 23;
        else if (u == 18)
            want = x < y ? 19 : 20;
        else if (u == 6) {
            if (v == 7)
                hi = min(hi, 100 * x + 20 * y - 1);
            else if (v == 12)
                lo = max(lo, 100 * x + 20 * y);
            else
                return -1;
            want = v;
        } else if (u == 12) {
            if (v == 13)
                hi = min(hi, 2 * x * x - y);
            else if (v == 14)
                lo = max(lo, 2 * x * x - y + 1);
            else
                return -1;
            want = v;
        } else if (u == 14) {
            if (v == 15)
                lo = max(lo, 101 - x);
            else if (v == 16)
                hi = min(hi, 100 - x);
            else
                return -1;
            want = v;
        } else if (u == 23)
            want = i + 2 == (int)p.size() ? 24 : 17;
        else if (edge.count(u))
            want = edge[u];
        if (v != want)
            return -1;
    }
    return lo <= hi ? lo : -1;
}
int main() {
    int n;
    cin >> n;
    string line;
    getline(cin, line);
    for (int q = 0; q < n; q++) {
        getline(cin, line);
        istringstream in(line);
        vector<int> p;
        int t, L = 0;
        while (in >> t) {
            p.push_back(t);
            if (t == 19 || t == 21)
                L++;
        }
        long long ans = LLONG_MAX;
        if (p.size() >= 2 && p.front() == 1 && p.back() == 24)
            for (long long x = 0; 10 * x * x <= L + 10; x++) {
                long long A = 10 * x * x;
                for (long long y = 0; y <= A + 1; y++) {
                    long long z = test(p, x, y);
                    if (z >= 0)
                        ans = min(ans, x + y + z);
                }
            }
        if (q % 15)
            cout << ',';
        if (ans == LLONG_MAX)
            cout << "inf";
        else
            cout << ans;
        if (q % 15 == 14 || q + 1 == n)
            cout << '\n';
    }
}

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

更多