QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#793237#9802. Light Up the GridskkkkkyTL 1ms5736kbC++233.0kb2024-11-29 17:50:532024-11-29 17:50:56

Judging History

你现在查看的是测评时间为 2024-11-29 17:50:56 的历史记录

  • [2024-11-29 22:58:39]
  • 管理员手动重测本题所有提交记录
  • 测评结果:TL
  • 用时:1ms
  • 内存:3804kb
  • [2024-11-29 17:50:56]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:5736kb
  • [2024-11-29 17:50:53]
  • 提交

answer

#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
#define ll int
#define endl "\n"
#define lowbit(x) ((x)&(-x))
using namespace std;
ll arr[4]{};
ll n;
ll sit[16]{};
ll val[65536][16]{};
bool check[65536][16]{};
ll small[2][2][2][2]{};
ll dp(ll done, ll less) {
	if (check[done][less]) {
		return val[done][less];
	}
	check[done][less] = true;
	ll answer = 1e9;
	ll memory = done;
	while (memory) {
		ll act = lowbit(memory);
		for (ll q = 0; q < 16; ++q) {
			ll del = log2(act);
			ll used = dp(done - act, q);
			ll changes = (sit[del] ^ 15 ^ q);
			used += small[bool(changes & 8)][bool(changes & 4)][bool(changes & 2)][bool(changes & 1)];
			ll now = changes ^ q;
			now ^= less;
			used += small[bool(now & 8)][bool(now & 4)][bool(now & 2)][bool(now & 1)];
			answer = min(used, answer);
		}
		memory -= act;
	}
	return val[done][less] = answer;
}
void solve() {
	bool flag = false;
	ll n;
	cin >> n;
	for (ll q = 0; q < (1ll << n); ++q) {
		for (ll i = 0; i < 16; ++i) {
			check[q][i] = false;
		}
	}
	for (ll q = 0; q < 16; ++q) {
		check[0][q] = true;
		val[0][q] = small[bool(q & 8)][bool(q & 4)][bool(q & 2)][bool(q & 1)];
	}
	for (ll q = 0; q < n; ++q) {
		char _;
		cin >> _;
		if (_ - '0') {
			sit[q] |= 8;
		}
		cin >> _;
		if (_ - '0') {
			sit[q] |= 4;
		}
		cin >> _;
		if (_ - '0') {
			sit[q] |= 2;
		}
		cin >> _;
		if (_ - '0') {
			sit[q] |= 1;
		}
		if (sit[q] == 15) {
			flag = true;
		}
	}
	ll answer = 1e9;
	for (ll q = 0; q < 16; ++q) {
		answer = min(answer, dp((1ll << n) - 1, q));
	}
	cout << answer + flag * 2 * (min(min(arr[0], arr[1]), min(arr[2], arr[3]))) << endl;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cout.tie(nullptr);
	ll t = 1;
	cin >> t;
	for (ll q = 0; q < 4; ++q) {
		cin >> arr[q];
	}
	for (ll a = 0; a < 2; ++a) {
		for (ll b = 0; b < 2; ++b) {
			for (ll c = 0; c < 2; ++c) {
				for (ll d = 0; d < 2; ++d) {
					small[a][b][c][d] = 1e9;
				}
			}
		}
	}
	small[0][0][0][0] = 0;
	for (ll q = 1; q < 512; ++q) {
		ll final[4]{};
		ll finalval = 0;
		if (q & 1) {
			finalval += arr[3];
			for (ll i = 0; i < 4; ++i) {
				final[i] ^= 1;
			}
		}
		if (q & 2) {
			finalval += arr[1];
			for (ll i = 0; i < 2; ++i) {
				final[i] ^= 1;
			}
		}
		if (q & 4) {
			finalval += arr[1];
			for (ll i = 2; i < 4; ++i) {
				final[i] ^= 1;
			}
		}
		if (q & 8) {
			finalval += arr[2];
			for (ll i = 0; i < 4; i += 2) {
				final[i] ^= 1;
			}
		}
		if (q & 16) {
			finalval += arr[2];
			for (ll i = 1; i < 4; i += 2) {
				final[i] ^= 1;
			}
		}
		if (q & 32) {
			finalval += arr[0];
			final[0] ^= 1;
		}
		if (q & 64) {
			finalval += arr[0];
			final[1] ^= 1;
		}
		if (q & 128) {
			finalval += arr[0];
			final[2] ^= 1;
		}
		if (q & 256) {
			finalval += arr[0];
			final[3] ^= 1;
		}
		small[final[0]][final[1]][final[2]][final[3]] = min(finalval, small[final[0]][final[1]][final[2]][final[3]]);
	}
	while (t--) {
		solve();
	}
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5688kb

input:

2 1000 100 10 1
4
10
00

01
00

00
10

00
01
1
11
11

output:

1121
2

result:

ok 2 number(s): "1121 2"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5736kb

input:

2 1 1 1 1
4
10
00

01
00

00
10

00
01
1
11
11

output:

5
2

result:

ok 2 number(s): "5 2"

Test #3:

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

input:

1 1000000 1000000 1000000 1000000
1
11
11

output:

2000000

result:

ok 1 number(s): "2000000"

Test #4:

score: -100
Time Limit Exceeded

input:

10000 8 2 7 8
8
00
01

00
11

00
10

11
11

10
10

01
10

01
00

10
11
8
11
01

11
00

01
10

11
11

00
01

01
01

01
00

11
10
9
00
00

01
01

10
11

00
01

11
10

11
00

11
11

00
11

01
10
9
11
11

10
00

11
00

11
01

00
10

01
11

00
01

01
01

10
01
11
00
01

01
01

10
10

00
11

11
11

11
10
...

output:


result: