ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

对拍&数据生成器

2022-09-13 07:30:20  阅读:263  来源: 互联网

标签:return int printf 生成器 str time cpp 数据


1. 对拍

鸣谢 $\color{black}{E}\color{red}{afoo}$ 的拍!

然后我重写了一版功能更丰富的(

使用说明:

适用环境:Linux C++程序的对拍

首先输入

g++ duipai.cpp -o duipai

然后输入

./duipai your_code std_code data_generator compile_again testcases time_limit

不是直接输入上面的东西,上面每个参数都有意义:

$\qquad$ $your\_code$ $\to$ 一个字符串,表示你的程序名(不加.cpp后缀)

$\qquad$ $std\_code$ $\to$ 一个字符串,表示标程的程序名(不加.cpp后缀)

$\qquad$ $data\_generator$ $\to$ 一个字符串,表示数据生成器的程序名(不加.cpp后缀)

$\qquad$ $compile\_again$ $\to$ 一个字符串,表示是否重新编译(输入 $no$ 表示否定,别的什么东西表示肯定)(个人建议让它重新编译)

$\qquad$ $testcases$  $\to$ 一个数字,表示总数据点个数(输入小于等于 $0$ 的数字会无限评测)

$\qquad$ $time\_limit$ $\to$ 一个数字,表示时间限制(用毫秒表示)

比如如果你的程序叫 $A.cpp$ ,标程叫 $A-std.cpp$ ,数据生成器叫 $getdata$ ,想要重新编译,无限数据点,时限 $1s$

那么你可以输入

./duipai A A-std getdata yes 0 1000

具体功能:

  1. 对拍(
  2. 输出 $RE$ 的错误信息(数组越界或是别的什么,而不是只有一个 $Segmentation\; fault$ )
  3. 每隔 $100$ 个测试点输出平均耗时和最大耗时
  4. 是否 $TLE$
#include<bits/stdc++.h>
#include<chrono>
using namespace std;
using namespace chrono;
string str[10];
int time_limit;
double tot_cpp,tot_std,maxx_cst;
bool sys(string cmd){
    if(system(cmd.c_str())) return false;
    else return true;
}
bool Compile(string filename){
    printf("---------------------------\n");
    if(!sys("g++ "+filename+".cpp -o "+filename+" -O2 -Wall -Wno-unused-result -DDEBUG -fsanitize=address,undefined")) return false;
    printf("Compiled Successfully\n");
    printf("---------------------------\n");
    return true;
}
void print_average_time(int x){
    printf("---------------------------\n");
    printf("Accepeted %d testcases\n",x);
    printf("Average data:\n");
    printf("You cost       %.3lf ms\n",tot_cpp/x);
    printf("Standard cost  %.3lf ms\n",tot_std/x);
    printf("Max time cost  %.3lf ms\n",maxx_cst);
    printf("---------------------------\n");
}
int main(int argv,char **argc){
    for(int i=1;i<argv;i++) str[i]=argc[i];
    printf("---------------------------\n");
    printf("Your code: %s.cpp\n",str[1].c_str());
    printf("Standard code: %s.cpp\n",str[2].c_str());
    printf("Data generator: %s.cpp\n",str[3].c_str());
    if(str[4]=="no"||str[4]=="No"||str[4]=="NO") printf("Disable auto-compile\n");
    else printf("Enable auto-compile\n");
    if(str[5]!=""&&stoi(str[5])>0) printf("Total testcases: %d\n",stoi(str[5]));
    else printf("Infinate testcases\n");
    if(str[6]!="") printf("Time limit: %d\n",stoi(str[6]));
    else printf("No time limit\n");
    printf("---------------------------\n");
    if(str[4]!="no"&&str[4]!="No"&&str[4]!="NO"){
        printf("Compiling your code...\n");
        if(!Compile(str[1])) return 0;
        printf("Compiling standard code...\n");
        if(!Compile(str[2])) return 0;
        printf("Compiling data generator...\n");
        if(!Compile(str[3])) return 0;
    }
    int n=-1;
    if(str[5]!=""&&stoi(str[5])>0) n=stoi(str[5]);
    if(str[6]!="") time_limit=stoi(str[6]);
    else time_limit=0;
    printf("Now begin test\n");
    printf("---------------------------\n");
    steady_clock::time_point t1,t2;
    duration<double>ut;
    for(int i=1;i!=(n+1);i++){
        if((i-1)%100==0&&i!=1){
            print_average_time(i-1);
            printf("---------------------------\n");
        }
        printf("Testcase %d\n",i);
        if(!sys("./"+str[3]+" >duipai_rand.in")){
            printf("Data generator RE\n");
            return 1;
        }
        t1=steady_clock::now();
        if(!sys("./"+str[1]+" <duipai_rand.in >duipai_cpp.out")){
            printf("Your code RE\n");
            return 1;
        }
        t2=steady_clock::now();
        ut=duration_cast<duration<double> >(t2-t1);
        double tmp=ut.count()*1000;
        printf("You cost       %.3lf ms\n",tmp);
        if(tmp>(double)time_limit&&time_limit!=0){
            printf("Your code TLE\n");
            print_average_time(i-1);
            return 1;
        }
        maxx_cst=max(maxx_cst,tmp);tot_cpp+=tmp;
        t1=steady_clock::now();
        if(!sys("./"+str[2]+" <duipai_rand.in >duipai_std.out")){
            printf("Standard code RE\n");
            return 1;
        }
        t2=steady_clock::now();
        ut=duration_cast<duration<double> >(t2-t1);
        tmp=ut.count()*1000;tot_std+=tmp;
        printf("Standard cost  %.3lf ms\n",tmp);
        if(!sys("diff duipai_cpp.out duipai_std.out >diff.log")){
            printf("Wrong Answer\n");
            return 1;
        }
        printf("Accpeted testcase %d\n",i);
        printf("---------------------------\n");
    }
    print_average_time(n);
    printf("---------------------------\n");
    printf("Finished.\n");
    return 0;
}

 

2. 数据生成器

依旧 Linux 适用,C++版本

#include<bits/stdc++.h>
#include<sys/time.h>
#include<unistd.h>
#define int long long
using namespace std;
int rand(int l,int r){
    return l+(rand()%(r-l+1))*(rand()%(r-l+1))%(r-l+1);
}
signed main(){
    struct timeval time;
    gettimeofday(&time,0);
    srand((unsigned int)time.tv_usec);
    //这里写数据生成器
    for(int i=1;i<=100;i++){
        printf("%lld ",rand(1,100));
    }
    return 0;
}

 

标签:return,int,printf,生成器,str,time,cpp,数据
来源: https://www.cnblogs.com/WintersRain/p/16687916.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有