提供交互库
查看原帖
提供交互库
556366
FFTotoro楼主2024/6/28 12:10

RT,题面有说:

你可以编写一个函数 getProgram() 并在程序开始包含 grader.h 来完成交互过程。该函数无返回值,也没有参数。或者直接编写一个完整程序来完成交互。

显然洛谷不太能支持前者的方式(把 IO 交互放在函数里面进行 神金),所以只能考虑后者。

后者的交互库我从 LOJ 上面搞了一份:

#include "testlib.h"
using namespace std;

const int MAXN = 200 * 1000 + 7;
const int MAXQ = 200 * 1000 + 7;

int bal[MAXN], nxt[MAXN];
string S;

bool isCorrect(int l, int r) {
	if (nxt[l] <= r + 1)
		return false;
	if (bal[r + 1] != bal[l])
		return false;
	return true;
}

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

	int n = inf.readInt();
	int MXQ = inf.readInt();
	S = inf.readToken("[()]+");

	for (int i = 0; i < n; i++) {
		S[i] ^= __builtin_popcount(i) & 1;
		bal[i + 1] = bal[i] + (S[i] == '(') - (S[i] == ')');
	}

	vector<pair<int, int> > st;
	st.push_back(make_pair(-MAXN, n + 1));
	for (int i = n; i >= 0; i--) {
		while (st.back().first >= bal[i])
		    st.pop_back();
		nxt[i] = st.back().second;
		st.push_back(make_pair(bal[i], i));
	}

	int usedQueries = 0;
	printf("%d\n", n);
	fflush(stdout);

	while (true) {
		string opt = ouf.readToken("[?!]");

		if (opt == "?") {
			usedQueries++;
			quitif(usedQueries > MXQ, _wa, "Contestant used more than %d queries", MXQ);
			int l = ouf.readInt(1, n);
			int r = ouf.readInt(1, n);
			--l; --r;
			quitif(l > r, _wa, "l should be less than or equal to r, but l = %d, r = %d", l, r);
			puts(isCorrect(l, r) ? "Yes" : "No");
			fflush(stdout);
		} else {
			string pans = ouf.readToken("[()]+");
			quitif((int)pans.length() != n, _wa, "Answer's length should be %d, but it is %d", n, (int)pans.length());
			for (int i = 0; i < n; i++)
				quitif(pans[i] != S[i] , _wa, "Mismatch at position %d. Expected %c, found %c", i + 1, S[i], pans[i]);
			break;
		}
	}

	quitf(_ok, "ok (%d queries)", usedQueries);

	return 0;
}
2024/6/28 12:10
加载中...