QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#201728#5161. Last GuessTeam_name#WA 1ms4040kbC++204.0kb2023-10-05 16:28:202023-10-05 16:28:20

Judging History

你现在查看的是最新测评结果

  • [2023-10-05 16:28:20]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4040kb
  • [2023-10-05 16:28:20]
  • 提交

answer

#include <bits/stdc++.h>

#define endl '\n'
#define DEBUG_VAR(x) { cerr << "* "#x" = " << x << endl; }

using namespace std;
using LL = long long;


const int N = 1024;

int m, n;
char str[N];
bool isSure[N];

bool can[N][26];
int sureLimit[26]; // Y+G 确切
int unsure[26]; // Y+G 下界

template<class cap_t>
struct MaxFlowGraph
{
	vector<cap_t> cap;
	vector<int> one;
	vector<int> ver, nxt;
	int idx;

	int n;

	int S, T;
	vector<int> d, cur;

	static constexpr cap_t INF = numeric_limits<cap_t>::max();

	void reset(int n)
	{
		cap.clear(); ver.clear(); nxt.clear();
		idx = 0;
		this->n = n;
		one.clear(); one.resize(n); fill(one.begin(), one.end(), -1);
		d.clear(); d.resize(n);
	}

	MaxFlowGraph(int n) { this->reset(n); }

	void addEdge(int a, int b, cap_t c)
	{
		nxt.push_back(one[a]); ver.push_back(b); cap.push_back(c); one[a] = idx++;
		nxt.push_back(one[b]); ver.push_back(a); cap.push_back(0); one[b] = idx++;
	}

	bool bfs()
	{
		queue<int> q;
		fill(d.begin(), d.end(), -1);
		cur = one;

		q.push(S); d[S] = 1;
		while(q.size()) {
			int x = q.front(); q.pop();
			for(int i = one[x]; ~i; i = nxt[i]) {
				int y = ver[i];
				if(cap[i] > 0 && d[y] == -1) {
					d[y] = d[x]+1;
					q.push(y);
					if(y == T) return true;
				}
			}
		}
		return false;
	}
	
	cap_t dinic(int x, cap_t limit)
	{
		if(x == T) return limit;
		cap_t flow = 0;
		for(int i = cur[x]; ~i && flow < limit; i = nxt[i]) {
			int y = ver[cur[x] = i];
			if(cap[i] > 0 && d[y] == d[x]+1) {
				cap_t k = dinic(y, min(cap[i], limit-flow));
				if(!k) d[y] = -1;
				cap[i] -= k; cap[i^1] += k;
				flow += k;
			}
		}
		return flow;
	}

	cap_t maxFlow(int S, int T)
	{
		this->S = S, this->T = T;
		cap_t res = 0;
		while(bfs()) 
			res += dinic(S, INF);
		return res;
	}

	void bringOutAns(int n)
	{
		for(int j = 0; j < 26; j++) {
			int x = n+j+1;

			for(int i = one[x]; ~i; i = nxt[i]) {
				int y = ver[i];
				if(y == S) continue;
				if(cap[i]) continue;
				else str[y] = 'a'+j, isSure[y] = true;
			}
		}

		// for(int i = one[T]; ~i; i = nxt[i]) {
		// 	int y = ver[i];
		// 	if(cap[i]) continue;
		// 	y
		// }
	}
};


set<int> pos[26];

int main()
{
	// freopen("1.in", "r", stdin);
	cin >> m >> n;

	static char a[N], s[N];

	memset(sureLimit, -1, sizeof sureLimit);
	memset(unsure, 0, sizeof unsure);
	memset(can, true, sizeof can);

	while(--m) {
		scanf(" %s %s", a+1, s+1);

		int cnt[26];
		memset(cnt, 0, sizeof cnt);
		bool hasBlack[26];
		memset(hasBlack, false, sizeof hasBlack);

		for(int i = 1; i <= n; i++) {
			int c = a[i]-'a';
			if(s[i] == 'G') {
				str[i] = a[i];
				isSure[i] = true;
				cnt[c]++;
				pos[c].insert(i);
			} else {
				can[i][c] = false;
				if(s[i] == 'Y') {
					cnt[c]++;
				} else {
					hasBlack[c] = true;
				} 
			}
		}

		for(int i = 0; i < 26; i++) {
			if(hasBlack[i]) {
				sureLimit[i] = cnt[i];
			} else {
				unsure[i] = max(unsure[i], cnt[i]);
			}
		}
	}

	MaxFlowGraph<int> g(n+30);

	int S = n+27, T = n+28;

	// int sum = 0; // maxflow 
	for(int i = 0; i < 26; i++) {
		int id = n+i+1;
		if(sureLimit[i] != -1) {
			g.addEdge(S, id, sureLimit[i]-(int)pos[i].size());
			// sum += sureLimit[i];
		} else {
			g.addEdge(S, id, unsure[i]-(int)pos[i].size());
			// sum += unsure[i];
		}
	}

	for(int i = 1; i <= n; i++) {
		if(isSure[i]) continue;
		g.addEdge(i, T, 1);
	}

	for(int i = 1; i <= n; i++) {
		if(isSure[i]) {
			g.addEdge(n+(str[i]-'a')+1, i, 1);
			continue;
		}
		for(int j = 0; j < 26; j++) {
			if(!can[i][j]) continue;
			int id = n+j+1;
			g.addEdge(id, i, 1);
		}
	}

	g.maxFlow(S, T);
	// if(res < sum) {
	// 	assert(0);
	// }

	g.bringOutAns(n);
	// DEBUG_VAR(isSure[6]);
	for(int i = 1; i <= n; i++) {
		if(isSure[i]) continue;
		for(int j = 0; j < 26; j++) {
			if(!can[i][j] || sureLimit[j] != -1) 
				continue;
			str[i] = j+'a';
			break;
		}
	}

	printf("%s\n", str+1);
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3972kb

input:

4 5
reply YYGBB
refer BBBGG
puppy YYGBB

output:

upper

result:

ok 

Test #2:

score: 0
Accepted
time: 0ms
memory: 4040kb

input:

2 12
aabbccddeeff GGGYGBYYYBBB

output:

aabacaaabdde

result:

ok 

Test #3:

score: -100
Wrong Answer
time: 1ms
memory: 4000kb

input:

25 500
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqoqqqqqqqqqqqqqqqqqqqqqqoqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqjq...

output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

result:

FAIL Condition failed: "answer.size() == n"