CSP 2022 提高组第一轮 - CSP/S 2022初试题 完善程序第二题解析

(1)(归并第 k 小) 已知两个长度均为 n 的有序数组 a1 和 a2(均为递增序,但不保证严 格单调递增),并且给定正整数 k(1≤k≤2n),求数组 a1 和 a2 归并排序后的数组里 第 k 小的数值,试补全程序。

一、查看题目

#include <bits/stdc++.h>
using namespace std;
const int N = 110;

int f[N][N];
int ans;
int a, b, c;
int init;

int dfs(int x, int y) {
    if (f[x][y] != init)
        return f[x][y];
    if (x == c || y == c)
        return f[x][y] = 0;
    f[x][y] = init - 1;
    f[x][y] = min(f[x][y], dfs(a, y) + 1);
    f[x][y] = min(f[x][y], dfs(x, b) + 1);
    f[x][y] = min(f[x][y], dfs(0, y) + 1);
    f[x][y] = min(f[x][y], dfs(x, 0) + 1);
    int t = min(a - x, y);
    f[x][y] = min(f[x][y], ①);
    t = min(x, b - y);
    f[x][y] = min(f[x][y], ②);
    return f[x][y];
}

void go(int x, int y) {
    if (③)
        return;
    if (f[x][y] == dfs(a, y) + 1) {
        cout << "FILL(1)" << endl;
        go(a, y);
    } else if (f[x][y] == dfs(x, b) + 1) {
        cout << "FILL(2)" << endl;
        go(x, b);
    } else if (f[x][y] == dfs(0, y) + 1) {
        cout << "DROP(1)" << endl;
        go (0, y);
    } else if (f[x][y] == dfs(x, 0) + 1) {
        cout << "DROP(2)" << endl;
        go(x, 0);
    } else {
        int t = min(a - x, y);
        if(f[x][y] == ④) {
            cout << "POUR(2,1)" << endl;
            go(x + t, y - t);
        } else {
            t = min(x, b - y);
            if (f[x][y] == ⑤) {
                cout << "POUR(1,2)" << endl;
                go(x - t, y + t);
            } else
                assert(0);
        }
    }
}

int main() {
    cin >> a >> b >> c;
    ans = 1 << 30;
    memset(f, 127, sizeof f);
    init = **f;
    if ((ans = dfs (0, 0)) == init - 1)
        cout << "impossible";
    else {
        cout << ans << endl;
        go (0, 0);
    }
}

二、题目分析

①~⑤处应填:

  1.  A. dfs(x + t, y - t) + 1
     B. dfs(x + t, y - t) - 1
     C. dfs(x - t, y + t) + 1
     D. dfs(x - t, y + t) - 1

    【正确答案: A,因为此处要把y倒给x,其中t是x剩余能到空间和y剩余水量的最小值,一免出现负数情况。】

  2.  A. dfs(x + t, y - t) + 1
     B. dfs(x + t, y - t) - 1
     C. dfs(x - t, y + t) + 1
     D. dfs(x - t, y + t) - 1

    【正确答案: C,因为此处要把x倒给y,与上一题同理。】

  3.  A. x == c || y == c
     B. x == c && y == c
     C. x >= c || y >= c
     D. x >= c && y >= c

    【正确答案: A,此处应该判断是否已经实现有一个容器中的水量到达c】

  4.  A. dfs(x + t, y - t) + 1
     B. dfs(x + t, y - t) - 1
     C. dfs(x - t, y + t) + 1
     D. dfs(x - t, y + t) - 1

    【正确答案: A,此处与第一题同理】

  5.  A. dfs(x + t, y - t) + 1
     B. dfs(x + t, y - t) - 1
     C. dfs(x - t, y + t) + 1
     D. dfs(x - t, y + t) - 1

    【正确答案: C,此处与第二题同理】

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值