QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#782263 | #9802. Light Up the Grid | solunar | TL | 1ms | 3628kb | C++14 | 1.9kb | 2024-11-25 19:32:37 | 2024-11-25 19:32:57 |
Judging History
answer
// 2024ICPCShenyangE
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int m;
int a1, a2, a3, a4;
char ope[9] = {1, 2, 4, 8, 3, 12, 5, 10, 15 };
int a[9];
char b[16];
int x[9];
int cCost, cOK;
int bestCost;
void dfs(int t) {
if (cOK == (1 << m) - 1) {
if (cCost < bestCost)
bestCost = cCost;
return;
}
if (t >= 9)
return;
for (int i = t; i < 9; i++) {
if (cCost >= bestCost)
continue;
swap(x[t], x[i]);
int oldOK = cOK;
for (int j = 0; j < m; j++) {
b[j] ^= ope[x[t]];
if (b[j] == 15)
cOK |= (1 << j);
}
cCost += a[x[t]];
dfs(t + 1);
cCost -= a[x[t]];
cOK = oldOK;
for (int j = 0; j < m; j++)
b[j] ^= ope[x[t]];
swap(x[t], x[i]);
}
}
int main() {
int t, init15;
char ch;
cin >> t >> a1 >> a2 >> a3 >> a4;
a[0] = a[1] = a[2] = a[3] = a1;
a[4] = a[5] = a2;
a[6] = a[7] = a3;
a[8] = a4;
while (t--) {
cin >> m;
init15 = false;
for (int i = 0; i < m; i++) {
b[i] = 0;
for (int j = 0; j < 4; j++) {
cin >> ch;
if (ch == '1')
b[i] |= (1 << j);
}
if (b[i] == 15)
init15 = true;
}
if (init15) {
int ans = 0x7fffffff;
for (int j = 0; j < 9; j++) {
cOK = 0;
for (int i = 0; i < m; i++) {
b[i] ^= ope[j];
if (b[i] == 15)
cOK |= (1 << i);
}
cCost = a[j];
bestCost = 0x7fffffff;
for (int i = 0; i < 9; i++)
x[i] = i;
dfs(0);
for (int i = 0; i < m; i++)
b[i] ^= ope[j];
if (bestCost < ans)
ans = bestCost;
}
cout << ans << endl;
} else {
cOK = 0;
cCost = 0;
bestCost = 0x7fffffff;
for (int i = 0; i < 9; i++)
x[i] = i;
dfs(0);
cout << bestCost << endl;
}
}
return 0;
}
/*
1 1000 100 10 1
1
11
11
*/
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3624kb
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: 3624kb
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: 3628kb
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:
44 45 52 52 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647 52 2147483647 2147483647 2147483647 2147483647 44 2147483647 52 2147483647 2147483647 2147483647 2147483647 2147483647 2147483647