QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#809849 | #9802. Light Up the Grid | snowysecret | WA | 36ms | 18072kb | C++20 | 3.4kb | 2024-12-11 17:43:02 | 2024-12-11 17:43:11 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define double long double
const int MAXN = 4e5 + 10, MOD = 1e9 + 7, HASHMOD = 1734232211;
int fac[MAXN], invfac[MAXN];
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) { return uniform_int_distribution<int>(x, y)(rng); }
int bm(int b, int p) {
if(p==0) return 1 % MOD;
int r = bm(b, p >> 1);
if(p&1) return (((r*r) % MOD) * b) % MOD;
return (r*r) % MOD;
}
int inv(int b) { return bm(b, MOD-2); }
vector<int> prefix_function(vector<int> t) {
int n = t.size(); vector<int> lps(n, 0);
for(int i=1; i<n; i++) {
int j = lps[i-1]; while(j > 0 && t[i] != t[j]) j = lps[j-1];
lps[i] = (t[i] == t[j] ? j+1 : 0);
} return lps;
}
int nCr(int n, int r) {
return (((fac[n] * invfac[r]) % MOD) * invfac[n-r]) % MOD;
}
void precomp() {
for(int i=0; i<MAXN; i++) fac[i] = (i ? (fac[i-1] * i) % MOD : 1);
invfac[MAXN - 1] = inv(fac[MAXN - 1]);
for(int i=MAXN-2; i>=0; i--) invfac[i] = (invfac[i+1] * (i+1)) % MOD;
}
void solve(int tc) {
int t, a[4];
cin >> t;
for(int i=0; i<4; i++) cin >> a[i];
a[1] = min(a[1], 2 * a[0]);
a[2] = min(a[2], 2 * a[0]);
a[3] = min(a[3], 2 * a[1]);
a[3] = min(a[3], 2 * a[2]);
int dist[16];
for(int i=0; i<16; i++) dist[i] = 1e18;
int table[16][16];
for(int i=0; i<16; i++) {
for(int j=0; j<16; j++) {
table[i][j] = (i==j ? 0 : 1e18);
}
}
for(int i=0; i<16; i++) table[i][i^15] = a[3];
for(int i=0; i<16; i++) table[i][i^10] = a[2];
for(int i=0; i<16; i++) table[i][i^5] = a[2];
for(int i=0; i<16; i++) table[i][i^12] = a[1];
for(int i=0; i<16; i++) table[i][i^3] = a[1];
for(int i=0; i<16; i++) table[i][i^8] = a[0];
for(int i=0; i<16; i++) table[i][i^4] = a[0];
for(int i=0; i<16; i++) table[i][i^2] = a[0];
for(int i=0; i<16; i++) table[i][i^1] = a[0];
for(int k=0; k<16; k++) {
for(int i=0; i<16; i++) {
for(int j=0; j<16; j++) {
table[i][j] = min(table[i][j], table[i][k] + table[k][j]);
}
}
}
for(int i=0; i<15; i++) dist[i] = table[0][i];
int dp[(1 << 16)][16];
for(int i=0; i<16; i++) dp[0][i] = 0;
for(int i=1; i<(1<<16); i++) {
for(int j=0; j<16; j++) {
dp[i][j] = 1e18;
}
if(__builtin_popcount(i) == 1) {
int bit = 32-__builtin_clz(i)-1;
assert(bit<16);
dp[i][bit] = dist[bit ^ 15];
if(bit == 15) {
dp[i][bit] = 1e18;
for(int j=0; j<4; j++) dp[i][bit] = min(dp[i][bit], a[j] * 2);
}
continue;
}
for(int j=0; j<16; j++) {
if(i & (1 << j)) {
for(int k=0; k<16; k++) {
if((i ^ (1 << j)) & (1 << k)) {
int state = k ^ 15;
dp[i][j] = min(dp[i][j], dp[i ^ (1 << j)][k] + dist[state ^ j ^ 15]);
}
}
}
}
}
while(t--) {
int m;
cin >> m;
int val = 0;
for(int i=0; i<m; i++) {
char x, y, z, w;
cin >> x >> y >> z >> w;
int hash = (x-'0') * 8 + (y-'0') * 4 + (z-'0') * 2 + (w-'0') * 1;
val ^= (1<<hash);
}
int ans = 1e18;
for(int i=0; i<16; i++) ans = min(ans, dp[val][i]);
cout << ans << '\n';
}
}
int32_t main() {
precomp();
ios::sync_with_stdio(0); cin.tie(0);
int t = 1; //cin >> t;
for(int i=1; i<=t; i++) solve(i);
}
/*
g++ code2.cpp -std=c++17 -O2 -o code2
./code2 < input.txt
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 28ms
memory: 18056kb
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: 35ms
memory: 17996kb
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: 36ms
memory: 18004kb
input:
1 1000000 1000000 1000000 1000000 1 11 11
output:
2000000
result:
ok 1 number(s): "2000000"
Test #4:
score: -100
Wrong Answer
time: 36ms
memory: 18072kb
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:
39 32 39 39 40 36 42 42 48 41 39 44 39 41 37 32 29 42 47 48 41 39 49 41 29 37 45 43 34 34 32 46 34 36 41 46 48 37 37 34 29 36 44 44 46 35 44 37 41 42 49 33 44 41 39 41 47 51 46 34 47 47 42 33 38 38 43 37 42 48 34 36 28 34 32 41 36 44 43 37 40 38 34 34 34 37 47 45 44 38 47 40 37 40 43 29 39 48 39 34 ...
result:
wrong answer 1st numbers differ - expected: '34', found: '39'