跳到正文

个人记录

对拍实现的详细介绍

Maggie114514约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年11月9日
收录
2026年11月9日
更新
2026年11月14日
篇幅
约 8 分钟
获赞
1

版本

  1. v1当前

起因

诡异题目

#15225. Bracelet

因为下发大样例数据过水,所以产生了这篇文章……

因为赛后发现自己思路假了,但场上下发的大样例全过了,所以我咬咬牙去学了晾了两年半没学的对拍qwq。

那就让我们以这个让人难绷的题目为例吧~

数据生成器

首先你肯定得有个东西生成数据对吧,所以我们需要编写一个程序可以随机产生数据,以现在的时间产生随机数然后按照自己想要的范围取模就行~

代码 gen.cppgen.cpp

C++
#include<bits/stdc++.h>
using namespace std;
const int mod=1e4;
int main(){
	srand((unsigned)time(0));
	int a,b,c,n;
	a=rand()%mod;
	b=rand()%mod;
	c=rand()%mod;
	n=rand()%mod;
	cout<<a<<" "<<b<<" "<<c<<"\n";
	for(int i=1;i<=n;i++){
		int now=rand()%2;
		char c=now+'0';
		cout<<c;
	}
	cout<<"\n";
	return 0;
}

暴力

这个没啥好说的。写就完了。暴戾语言直接耍起~

代码 pearl1.cpppearl1.cpp

C++
#include<bits/stdc++.h>
using namespace std;
int a,b,c,n,ans,sum;
string s;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>a>>b>>c;
	cin>>s;
	n=s.size();
	s=s+s;
	s=" "+s;
	int ta=a,tb=b,tc=c;
	for(int i=1;i<=2*n;i++){
		ans=0,ta=a,tb=b,tc=c;
		for(int j=i;j<=2*n;j+=2){
			if(j<=2*n&&j+1<=2*n){
				if(s[j]=='0'&&s[j+1]=='0'&&ta){
					ta--;
					ans++;
				}else if(((s[j]=='1'&&s[j+1]=='0')||(s[j]=='0'&&s[j+1]=='1'))&&tb){
					tb--;
					ans++;
				}else if(s[j]=='1'&&s[j+1]=='1'&&tc){
					tc--;
					ans++;
				}else{
					break;
				}
			}
			sum=max(sum,ans*2);
		}
	}
	if(n%2)n--;
	sum=min(sum,n);
	cout<<sum<<"\n";
	return 0;
} 

正解

就是你不确定是否完全正确的代码。爽写就完了。

代码 pearl.cpppearl.cpp

C++
#include<bits/stdc++.h>
using namespace std;
int a,b,c,n,ans,sum;
string s;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>a>>b>>c;
	cin>>s;
	n=s.size();
	s=s+s;
	s=" "+s;
	int ta=a,tb=b,tc=c;
	int j=1,ans=0;
	for(int i=1;i<=2*n;i+=2){
		if(i>j)j=i;
		while(j+1<=2*n){
			if(s[j]=='0'&&s[j+1]=='0'&&ta){
				ta--;
				ans++;
				sum=max(sum,ans*2);
				j+=2;
			}else if(((s[j]=='1'&&s[j+1]=='0')||(s[j]=='0'&&s[j+1]=='1'))&&tb){
				tb--;
				ans++;
				sum=max(sum,ans*2);
				j+=2;
			}else if(s[j]=='1'&&s[j+1]=='1'&&tc){
				tc--;
				ans++;
				sum=max(sum,ans*2);
				j+=2;
			}else{
				int f=0;
				if(s[i]=='0'&&s[i+1]=='0'&&ta<a)ta++,f=1;
				else if(((s[i]=='1'&&s[i+1]=='0')||(s[i]=='0'&&s[i+1]=='1'))&&tb<b)tb++,f=1;
				else if(s[i]=='1'&&s[i+1]=='1'&&tc<c)tc++,f=1;
				sum=max(sum,ans*2);
				if(f)ans--;
				break;
			}
		}
	} 
	j=2,ans=0;
	ta=a,tb=b,tc=c;
	for(int i=2;i<=2*n;i+=2){
		if(i>j)j=i;
		while(j+1<=2*n){
			if(s[j]=='0'&&s[j+1]=='0'&&ta){
				ta--;
				ans++;
				sum=max(sum,ans*2);
				j+=2;
			}else if(((s[j]=='1'&&s[j+1]=='0')||(s[j]=='0'&&s[j+1]=='1'))&&tb){
				tb--;
				ans++;
				sum=max(sum,ans*2);
				j+=2;
			}else if(s[j]=='1'&&s[j+1]=='1'&&tc){
				tc--;
				ans++;
				sum=max(sum,ans*2);
				j+=2;
			}else{
				int f=0;
				if(s[i]=='0'&&s[i+1]=='0'&&ta<a)ta++,f=1;
				else if(((s[i]=='1'&&s[i+1]=='0')||(s[i]=='0'&&s[i+1]=='1'))&&tb<b)tb++,f=1;
				else if(s[i]=='1'&&s[i+1]=='1'&&tc<c)tc++,f=1;
				sum=max(sum,ans*2);
				if(f)ans--;
				break;
			}
		}
	} 
	if(n%2)n--;
	sum=min(n,sum);
	cout<<sum<<"\n";
	return 0;
} 

我就说很诡异吧……

对拍脚本

完事具备,只欠比对。(单押damn)

你要有一个程序能快速比对两者的输出是否完全相同。正巧我们的 systemsystem 就能干这种事情。

把我们的数据生成器生成的数据放在一个文档中,让你的暴戾语言和正解(不确定版)都从这个文档中读入数据,然后分别在不同的文档中输出,最后比对二者的输出就可以了。

最后一点,如果输出结果相同就继续进行下一次比对,不同则退出并不再生成新的数据,而导致结果不同的那一组数据最终也会被保存在数据生成器输出数据的那个文档中。

当然在进行对拍之前,你需要将之前的所有代码都放到同一文件夹下,再编译运行一遍。毕竟真正使用的是 .exe.exe 文件。然后再运行我们这个对拍脚本就可以了。

具体细节和实现方法请看代码~

代码 stress.cppstress.cpp

C++
#include<bits/stdc++.h>
using namespace std;
int main(){
	int t=0;
	while(1){
		t++;
		cout<<"test case #"<<t<<": ";
		system("gen.exe > pearl.in");
		system("pearl.exe <pearl.in> pearl.out");
		system("pearl1.exe <pearl.in> pearl1.out");
		if(system("fc pearl.out pearl1.out > diff.log")!=0){
			cout<<"WA"<<"\n";
			break;
		}else{
			cout<<"AC"<<"\n";
		}
	}
	return 0;
}

结尾

然后我们就轻松愉悦得完成了一份对拍啦!

有没有人和我一样觉得对拍的过程很爽啊,看着一大堆 ACAC 从眼前闪过。(当然如果 WAWA 了就很让人红温了)

但不管怎么样我还是想说建议增强下发样例强度!!!

好啦,到这里就结束了哦

感谢观看~

原文