CCFCSP试题编号:202212-2试题名称:训练计划

CCFCSP试题编号:202212-2试题名称:训练计划_第1张图片

#include
#include
using namespace std;

const int N = 101;
int n, m;
int p[N], t[N];
int earliest[N], latest[N];

int main() {
    int mark = 1;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
        cin >> p[i];
    for (int i = 1; i <= m; i++)
        cin >> t[i];
    // 将每个科目的最早时间确定
    for (int i = 1; i <= m; i++) {
        if (p[i] == 0)
            earliest[i] = 1;
        else
            earliest[i] = earliest[p[i]] + t[p[i]];
        if (earliest[i] + t[i] - 1 > n)
            mark = 0;
    }
    for (int i = 1; i <= m; i++)
        cout << earliest[i] << " ";
    cout << endl;
    
    if (mark == 1) {
        
        for (int i = m; i >= 1; i--) {
            int temp = 366;
            for (int j = i + 1; j <= m; j++) 
            {
                if (p[j] == i)
                    temp = min(temp, latest[j]);
            }
            if (temp == 366)
                latest[i] = n - t[i] + 1;
            else
                latest[i] = temp - t[i];
        }
        for (int i = 1; i <= m; i++)
            cout << latest[i] << " ";
    }
    return 0;
}

 

你可能感兴趣的:(ccf-csp练习题题解,算法,c++)