QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#267942#7733. Cool, It’s Yesterday Four Times MorePetroTarnavskyi#WA 3ms10744kbC++202.5kb2023-11-27 21:25:302023-11-27 21:25:31

Judging History

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

  • [2023-11-27 21:25:31]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:10744kb
  • [2023-11-27 21:25:30]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); i--)
#define SZ(a) int(a.size())
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;

const int N = 1 << 17;

string s;
VI g[N];
VI dp[N];
int c0[N], c1[N];
int ans[N];

VI transform(VI a, int to)
{
	VI b(SZ(a) + c0[to] + c1[to], N);
	deque<PII> dq1, dq2;
	FOR(k, 0, SZ(b))
	{
		int i = k - c1[to];
		if (i >= 0 && i < SZ(a))
		{
			int val = a[i] - i;
			while (!dq1.empty() && dq1.back().S >= val)
				dq1.pop_back();
			dq1.PB({i, val});
			
			val = a[i] + i;
			while (!dq2.empty() && dq2.back().S >= val)
				dq2.pop_back();
			dq2.PB({i, val});
		}
		if (!dq1.empty() && k - c1[to] - c0[to] == dq1.front().F)
			dq1.pop_front();
		if (!dq2.empty() && k - 2 * c1[to] == dq2.front().F)
			dq2.pop_front();
		if (!dq1.empty())
			b[k] = min(b[k], k - c1[to] + dq1.front().S);
		if (!dq2.empty())
			b[k] = min(b[k], c1[to] - k + dq2.front().S);
	}
	return b;
}

int dfs(int v)
{
	if (g[v].empty())
	{
		c0[v] = s[v] == '0';
		c1[v] = s[v] == '1';
		dp[v] = {0};
		ans[v] = 0;
		return v;
	}
	if (SZ(g[v]) == 1)
	{
		int to = g[v][0];
		int j = dfs(to);
		c0[v] = c0[to] + (s[v] == '0');
		c1[v] = c1[to] + (s[v] == '1');
		ans[v] = ans[to];
		return j;
	}
	int m = N;
	VI indices;
	for (int to : g[v])
	{
		indices.PB(dfs(to));
		m = min(m, SZ(dp[indices.back()]) + c0[to] + c1[to]);
	}
	FOR(i, 0, SZ(g[v]))
	{
		int to = g[v][i];
		int j = indices[i];
		while (SZ(dp[j]) + c0[to] + c1[to] > m)
			dp[j].pop_back();
		dp[j] = transform(dp[j], to);
	}
	VI dpSons(m);
	for (int j : indices)
		FOR(k, 0, SZ(dp[j]))
			dpSons[k] += dp[j][k];
	dp[v].resize(m + 1);
	FOR(k, 0, m + 1)
	{
		dp[v][k] = dpSons[k] + (s[v] == '1');
		if (k > 0)
			dp[v][k] = min(dp[v][k], dpSons[k - 1] + (s[v] == '0'));
	}
	ans[v] = *min_element(ALL(dp[v]));
	return v;
}

void solve()
{
	int n;
	cin >> n >> s;
	FOR(i, 1, n)
	{
		int p;
		cin >> p;
		p--;
		g[p].PB(i);
	}
	dfs(0);
	FOR(i, 0, n)
	{
		if (i > 0)
			cout << " ";
		cout << ans[i];
	}
	cout << "\n";
	FOR(i, 0, n)
	{
		g[i].clear();
		dp[i].clear();
		c0[i] = c1[i] = 0;
	}
}

int main()
{
	ios::sync_with_stdio(0); 
	cin.tie(0);	
	int t;
	cin >> t;
	while (t--)
		solve();
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 10744kb

input:

4
2 5
.OO..
O..O.
1 3
O.O
1 3
.O.
2 3
OOO
OOO

output:

0 0
0 0
0 0
0 0

result:

wrong answer 1st lines differ - expected: '3', found: '0 0'