QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#439788 | #8770. Comparator | HKOI0# | RE | 0ms | 3616kb | C++14 | 3.2kb | 2024-06-12 18:02:47 | 2024-06-12 18:02:48 |
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)) {
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)) {
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: 3540kb
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: 3616kb
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: -100
Runtime Error
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...