QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#779949 | #9802. Light Up the Grid | pei | RE | 0ms | 3824kb | C++23 | 3.9kb | 2024-11-24 22:58:48 | 2024-11-29 22:50:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define inf INT32_MAX
#define x first
#define y second
#define endl '\n'
struct Node {
pair<int, int> a, b;
};
bool operator==(Node aa, Node bb) {
return aa.a == bb.a && aa.b == bb.b;
}
int res;
int a, b, c, d;
int cnt1, cnt2, cnt3, cnt4;
Node x, y;
void dfs(int sum) {
if (x == y) {
res = min(res, sum);
return;
}
if (cnt1 >= 4 || cnt2 >= 2 || cnt3 >= 2 || cnt4 >= 1 || sum >= res)
return;
cnt1++;
x.a.x ^= 1;
dfs(sum + a);
cnt1--;
x.a.x ^= 1;
cnt1++;
x.a.y ^= 1;
dfs(sum + a);
cnt1--;
x.a.y ^= 1;
cnt1++;
x.b.x ^= 1;
dfs(sum + a);
cnt1--;
x.b.x ^= 1;
cnt1++;
x.b.y ^= 1;
dfs(sum + a);
cnt1--;
x.b.y ^= 1;
cnt2++;
x.a.x ^= 1;
x.a.y ^= 1;
dfs(sum + b);
cnt2--;
x.a.x ^= 1;
x.a.y ^= 1;
cnt2++;
x.b.x ^= 1;
x.b.y ^= 1;
dfs(sum + b);
cnt2--;
x.b.x ^= 1;
x.b.y ^= 1;
cnt3++;
x.a.x ^= 1;
x.b.x ^= 1;
dfs(sum + c);
cnt3--;
x.a.x ^= 1;
x.b.x ^= 1;
cnt3++;
x.a.y ^= 1;
x.b.y ^= 1;
dfs(sum + c);
cnt3--;
x.a.y ^= 1;
x.b.y ^= 1;
cnt4++;
x.a.x ^= 1;
x.a.y ^= 1;
x.b.x ^= 1;
x.b.y ^= 1;
dfs(sum + d);
cnt4--;
x.a.x ^= 1;
x.a.y ^= 1;
x.b.x ^= 1;
x.b.y ^= 1;
}
void solve() {
int t;
cin >> t;
cin >> a >> b >> c >> d;
while (t--) {
int n;
cin >> n;
vector<Node> arr(n + 1);
for (int i = 1; i <= n; i++) {
char aa, bb, cc, dd;
cin >> aa >> bb;
cin >> cc >> dd;
arr[i].a.x = aa - '0';
arr[i].a.y = bb - '0';
arr[i].b.x = cc - '0';
arr[i].b.y = dd - '0';
getchar();
}
int temp, sum;
int ans1 = 0;
vector<Node> brr = arr;
deque<Node> q;
Node tmp = {{1, 1},
{1, 1}};
q.push_back(tmp);
while (arr.size() > 1) {
int pos = -1;
temp = inf;
res = inf;
for (int j = 1; j < arr.size(); j++) {
cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
if (q.back() == arr[j])
res = min(res, 2 * min(a, min(b, min(c, d))));
else {
y = arr[j];
x = q.back();
dfs(0);
}
if (res < temp) {
temp = res;
pos = j;
}
}
ans1 += temp;
q.push_back(arr[pos]);
arr.erase(arr.begin() + pos);
}
int ans2 = 0;
for (int i = 1; i <= n; i++) {
if (brr[i] == tmp) {
ans2 += 2 * min(a, min(b, min(c, d)));
brr.erase(brr.begin() + i);
break;
}
}
while (brr.size() > 1) {
int pos = -1;
temp = inf;
res = inf;
for (int j = 1; j < brr.size(); j++) {
cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
y = arr[j];
x = tmp;
dfs(0);
if (res < temp) {
temp = res;
pos = j;
}
}
ans2 += temp;
tmp = brr[pos];
brr.erase(brr.begin() + pos);
}
cout << min(ans1, ans2) << endl;
}
}
signed main() {
#ifdef ONLINE_JUDGE
#else
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#endif
ios::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3824kb
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: 0ms
memory: 3564kb
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: 3788kb
input:
1 1000000 1000000 1000000 1000000 1 11 11
output:
2000000
result:
ok 1 number(s): "2000000"
Test #4:
score: -100
Runtime Error
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 ...