QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#439791 | #8770. Comparator | HKOI0# | AC ✓ | 843ms | 8048kb | C++14 | 3.5kb | 2024-06-12 18:07:34 | 2024-06-12 18:07:34 |
Judging History
answer
#include <bits/stdc++.h>
#define all(a) begin(a), end(a)
#define int long long
using namespace std;
int prior(char c){
if (c == '!') return 4;
if (c == '=') return 3;
if (c == '&') return 2;
if (c == '|') return 1;
if (c == '^') return 0;
if (c == '(' || c == ')') return -1;
return -2;
}
bool is_op(char c){
return prior(c) > -2;
}
bool calc(char c, bool x, bool y){
if (c == '=') return x == y;
if (c == '&') return x & y;
if (c == '|') return x | y;
if (c == '^') return x ^ y;
assert(false); return -1;
}
bool eval(string s, bool x, bool y){
s = "(" + s + ")";
vector<bool> val; vector<char> op;
for (auto c : s) {
// for (auto x : val) cout << x << ' '; cout << endl;
// for (auto c : op) cout << c << ' '; cout << endl;
// cout << "Process " << c << ' ' << is_op(c) << endl;
if (is_op(c)) {
if (c == '(') {
op.push_back('(');
continue;
} else if (c == ')') {
while (!op.empty() && prior(op.back()) > prior(c)) {
if (op.back() == '!') {
val.back() = !val.back();
op.pop_back();
continue;
}
assert(val.size() >= 2);
bool x = val[val.size() - 2], y = val.back(); val.pop_back();
val.back() = calc(op.back(), x, y); op.pop_back();
}
op.pop_back();
continue;
}
while (!op.empty() && prior(op.back()) > prior(c)) {
if (op.back() == '!') {
val.back() = !val.back();
op.pop_back();
continue;
}
assert(val.size() >= 2);
bool x = val[val.size() - 2], y = val.back(); val.pop_back();
val.back() = calc(op.back(), x, y); op.pop_back();
}
op.push_back(c);
} else {
bool b;
if (c == 'x' || c == 'y') {
b = c == 'x' ? x : y;
} else {
b = c - '0';
}
while (!op.empty() && op.back() == '!') {
b = !b; op.pop_back();
}
val.push_back(b);
}
}
return val[0];
}
struct Comparator{
int a, b, m, r;
bool term(int x, int y){
bool b1 = (x >> a) & 1;
bool b2 = (y >> b) & 1;
return (m >> (b1 * 2 + b2)) & 1;
}
};
int msk[11][11];
bool cmp[1 << 10][1 << 10];
void solve() {
int n, k; cin >> n >> k;
vector<Comparator> C;
for (int i = 0; i < n; i++) {
int a, b; cin >> a >> b;
string expr; cin >> expr;
int m = 0;
// cout << "*" << '\n';
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
m += eval(expr, i, j) * (1 << (i * 2 + j));
// cout << i << ' ' << j << ' ' << eval(expr, i, j) << '\n';
}
}
int r; cin >> r;
if ((msk[a][b] | m) == msk[a][b]) continue;
msk[a][b] |= m; C.push_back({a - 1, b - 1, m, r});
}
bool lst; cin >> lst;
for (int a = 0; a < (1 << k); a++) {
for (int b = 0; b < (1 << k); b++) {
bool done = false;
for (auto& c : C) {
if (c.term(a, b)) {
done = true; cmp[a][b] = c.r;
break;
}
}
if (!done) cmp[a][b] = lst;
}
}
const int K = 1 << k;
// for (int i = 0; i < K; i++) {
// for (int j = 0; j < K; j++) {
// cout << cmp[i][j] << ' ';
// }
// cout << '\n';
// }
int a1 = 0, a2 = 0, a3 = 0;
for (int a = 0; a < K; a++) {
a1 += cmp[a][a];
for (int b = 0; b < K; b++) {
if (cmp[a][b] && cmp[b][a]) a2++;
for (int c = 0; c < K; c++) {
if (cmp[a][b] && cmp[b][c] && !cmp[a][c])
a3++;
}
}
}
cout << a1 << ' ' << a2 << ' ' << a3 << '\n';
}
int32_t main(){
#ifndef LOCAL
cin.tie(0)->sync_with_stdio(false);
#endif
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3536kb
input:
3 2 1 1 (x=0)&(y=1) 1 1 1 (x=1)&(y=(x^x)) 0 2 2 (x=1)|(y=0) 0 1
output:
0 0 0
result:
ok single line: '0 0 0'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3612kb
input:
4 3 2 1 x=0&(y=1) 1 1 2 !x^!!y 0 2 3 ((x|1)=y)&1&1 1 3 1 !x&!x&!x 0 1
output:
3 25 52
result:
ok single line: '3 25 52'
Test #3:
score: 0
Accepted
time: 31ms
memory: 3716kb
input:
1413 3 1 3 0 0 3 3 !x 0 2 2 x=0 1 1 2 !y^0 1 2 3 (x^1) 0 3 2 ((!0)) 1 1 1 !!1=(y) 0 2 2 !(1^x)&y 1 3 2 (y)&1|!!1 0 3 1 !x=(y&y=y) 0 2 1 (((!1)^!x)) 1 2 3 !0=(0&y)=1&y 0 1 2 ((((!0)))|!1) 0 3 1 !(y=!1=x|(!x)) 0 1 1 ((((y=!y)))&!0) 0 2 3 ((y=1)^!1^!!1|0) 1 2 3 1|(!x)&!x|1|(x=1) 1 2 3 !0^!!!!y&0=(!1&!0...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #4:
score: 0
Accepted
time: 843ms
memory: 4536kb
input:
181737 10 5 2 1 1 1 10 !1=!x 0 10 1 (1^x) 0 2 4 !1 1 10 8 y=(!1)^1 1 6 2 !((x&!x)) 1 1 10 !!((!x)|x) 1 7 10 (((0))) 0 7 3 !(1)^!x 0 10 4 (!1)&x 0 7 7 !y&!0 1 8 8 !1=(x)|1^1 1 2 6 ((!!!x)) 0 7 2 1 1 2 2 y=1=0 0 6 3 (!0) 0 6 4 0 0 1 1 (!1) 1 1 8 y 1 3 5 !x|!x^!1 0 4 7 (!0) 0 3 4 !1&1&!1|!0 1 2 7 ((0|1...
output:
1024 1048576 0
result:
ok single line: '1024 1048576 0'
Test #5:
score: 0
Accepted
time: 19ms
memory: 8048kb
input:
1 3 1 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!...
output:
4 16 0
result:
ok single line: '4 16 0'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
1 1 1 1 x^y|1 0 1
output:
1 1 0
result:
ok single line: '1 1 0'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3760kb
input:
1 1 1 1 x&y|1 0 1
output:
0 0 0
result:
ok single line: '0 0 0'
Test #8:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
1 1 1 1 x=y|1 0 1
output:
0 0 0
result:
ok single line: '0 0 0'
Test #9:
score: 0
Accepted
time: 0ms
memory: 3836kb
input:
2 2 1 2 !x&!y 1 1 1 !x&y 0 1
output:
4 12 2
result:
ok single line: '4 12 2'
Test #10:
score: 0
Accepted
time: 340ms
memory: 4916kb
input:
2 10 9 8 ((((!((!x=1))^(!1&(x|x|!y))&((!y&!x=(x=y)))&!((((x=1))&(0=(y))^(!!(!!x^1=x)&(x)^y&1=!x&1=(((!0^(1)^1))^!(((((y))|x|!y))))^!!0=!y&(0)|(y=x&!y&y=x)))=((((!!y&!!0|!0^!0)=!x))))^0&(((!1=!(!x)))|(((((x=1|!y|y)=(!1^1^0^(0)))=!0^1)))=(!(!1^(((((!1)^!x^0))))=(1)^((((y=((x))|(0)|(!1^1)))|(!!!y))=((!...
output:
0 0 0
result:
ok single line: '0 0 0'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
4 3 1 1 !!!!!!x 0 2 1 !!y 1 1 2 !!!!!x 1 2 2 !!!x 0 1
output:
4 16 0
result:
ok single line: '4 16 0'
Test #12:
score: 0
Accepted
time: 0ms
memory: 4200kb
input:
1 1 1 1 ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((...
output:
1 1 0
result:
ok single line: '1 1 0'
Test #13:
score: 0
Accepted
time: 676ms
memory: 4644kb
input:
200000 10 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10 !x^!y 1 3 10...
output:
512 262144 134217728
result:
ok single line: '512 262144 134217728'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
10 3 3 1 (!x) 1 3 2 !1&x&1&!y 1 2 1 ((x)&y) 1 1 3 0 0 2 2 !1&0=y&0 1 3 3 (!y)^y 1 2 1 0|((!y)) 0 3 2 x 0 2 2 (y|1^x) 0 2 1 ((!0)|y) 0 1
output:
8 64 0
result:
ok single line: '8 64 0'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
0 3 1
output:
8 64 0
result:
ok single line: '8 64 0'