Checker

tdk_07 2022-08-15 11:45:58

Cho em xin checker bài này với ạ, em cảm ơn

Tổng cộng 2 trả lời

Dao Nhat Tan

Em có viết cả checker này sử dụng thư viện testlibs cho cả trường hợp không tồn tại thứ tự sắp xếp mn tham khảo thử ạ:

#include "testlib.h"
#include<bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    registerTestlibCmd(argc, argv);

    int n = inf.readInt();
    int m = inf.readInt();

    vector<vector<int>> s(n + 1, vector<int>());
    map<int, bool> M;
    int cnt = 0;

    for(int i = 1, x, y; i <= m; i++) {
        x = inf.readInt();
        y = inf.readInt();
        s[y].push_back(x);
    }

    if (ans.readToken() == "IMPOSSIBLE") {
        if (ouf.readToken() == "IMPOSSIBLE") {
            quitf(_ok, "Correct: No solution exists");
        } else {
            quitf(_wa, "Wrong answer: Solution exists, but participant claims impossible");
        }
    }

    for(int i = 1, x; i <= n; i++) {
        x = ouf.readInt();
        if(M[x]) {
            quitf(_wa, "So xuat hien nhieu hon 1 lan");
        }
        for(auto y : s[x]) {
            if(!M[y])
                quitf(_wa, "yeu kem");
        }
        M[x] = 1;
    }

    quitf(_ok, "good");
}
Trùm CUỐI

Đây bạn nhé.

#ifndef __EMITTED_VALIDATE_H
#define __EMITTED_VALIDATE_H

#include <bits/stdc++.h>

typedef void (*feedback_function)(const char*, ...);

const int EXITCODE_AC = 0;
const int EXITCODE_WA = 0;

// declare a constant to be used as user_out.
const std::string FILENAME_USER_OUTPUT = "user_out";
// declare a constant to be used as input.
const std::string FILENAME_INPUT = "input";

#define USAGE "./%s\n"
// we have to define USAGE as dummy because we don't want to modify the lib. too much.

std::ifstream judge_in, judge_ans;
// another ifstream must be used for user_out;
std::ifstream user_out;

char *feedbackdir = NULL;

void vreport_feedback(const char* msg, va_list pvar) {
    assert(stderr);
    vfprintf(stderr, msg, pvar);
}

void report_feedback(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
}

void judge_message(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
}

void judge_error(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
    assert(0);
}

// cleaning up.
void cleanup() {
    fflush(stderr);
    fclose(stderr);
    fflush(stdout);
    fclose(stdout);
    judge_in.close();
    user_out.close();
}

// write another function ``scoring()`` for ac or wa.
void scoring(double score) {
    fprintf(stdout, "%.f", score + 1e-9);
}

void accept() {
    scoring(100.);
    cleanup();
    exit(EXITCODE_AC);
}

void accept_with_score(double scorevalue) {
    scoring(scorevalue);
    cleanup();
    exit(EXITCODE_AC);
}

void wrong_answer(const char* msg, ...) {
    va_list pvar;
    va_start(pvar, msg);
    vreport_feedback(msg, pvar);
    scoring(0.);
    cleanup();
    exit(EXITCODE_WA);
}

void init_io() {
    judge_in.open(FILENAME_INPUT, std::ios_base::in);
    if(judge_in.fail()) {
        judge_error("failed to open input file.\n");
    }
    user_out.open(FILENAME_USER_OUTPUT, std::ios_base::in);
    if(user_out.fail()) {
        judge_error("failed to open user output file.\n");
    }
    judge_ans.open("answer", std::ios_base::in);
    if(judge_ans.fail()) {
        judge_error("failed to open answer file.\n");
    }
}
using namespace std;
void OK(istream &ouf, feedback_function waf) {
    string t;
    if (ouf >> t)
        waf("Sai. Du lieu ra khong dung dinh dang.\n");
    judge_message("OK. Dap an duoc chap nhan!\n");
}

bool check(istream &inf, istream &ans, istream &ouf, feedback_function waf) {
    int n, m;
    inf >> n >> m;
    vector<pair<int, int> > e(m, {0, 0});
    for (int i = 0; i < m; i++)
        inf >> e[i].first >> e[i].second;
    int id[101];
    bool used[101];
    memset(used, 0, sizeof used);
    for (int i = 1; i <= n; i++) {
        if (!(ouf >> id[i]))
            waf("Sai. Du lieu ra khong dung dinh dang!\n");
        if (id[i] < 1 || id[i] > n)
            waf("Sai. Dinh thu %d là %d khong dung!\n", i, id[i]);
        if (used[id[i]])
            waf("Sai. Dinh %d da duoc dung!\n", id[i]);
        used[id[i]] = 1;
    }
    for (pair<int, int> x : e)
        if (id[x.first] > id[x.second])
            waf("Sai. Khong thoa man dieu kien!\n");
    OK(ouf, waf);
    return 1;
}
#endif

int main(void) {
    init_io();
    check(judge_in, judge_ans, user_out, wrong_answer);
    accept();
}