QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#53942 | #94. Good Old Table | not_so_organic | AC ✓ | 91ms | 6016kb | C++11 | 4.6kb | 2022-10-06 13:29:00 | 2022-10-06 13:29:01 |
Judging History
answer
#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <memory.h>
using namespace std;
const int N = 3000;
typedef unsigned int u32;
int n, m;
u32 a[N][(N+31)/32];
u32 sumCross[N][(N+31)/32];
int sumHor[N];
u32 sumVert[(N+31)/32];
inline void set(u32*const a, int n) {
a[n/32] |= (1u<<(n&31));
}
void read() {
scanf("%d%d\n", &n, &m);
for (int i = 0; i < n; i++) {
char buf[N*2+10];
gets(buf);
for (int j = 0; j < m; j++)
if(buf[j*2] == '2')
set((u32*)a[i], j);
}
}
bool get(u32 *a, int n) {
return (a[n/32] >> n) & 1;
}
int popcnt(u32 *a, int n) {
int ans = 0;
for(int i = 0; i < n / 32; ++i)
ans += __builtin_popcount(a[i]);
if(n&31)
ans += __builtin_popcount(a[n/32]&((1<<(n&31))-1));
return ans;
}
void xor2(u32 *a, u32 *b, int n) {
for(int i = 0; i < n / 32; ++i)
a[i] ^= b[i];
if(n&31)
a[n/32] ^= b[n/32]&((1<<(n&31))-1);
}
void xor3(u32 *a, u32 *b, u32 *c, int n) {
for(int i = 0; i < n / 32; ++i)
a[i] = b[i] ^ c[i];
if(n&31)
a[n/32] = (b[n/32] ^ c[n/32])&((1<<(n&31))-1);
}
void inverse(u32 *a, int n) {
for(int i = 0; i < n/32; ++i)
a[i] = ~a[i];
if(n&31)
a[n/32] ^= ((1<<(n&31))-1);
}
void findSumCross(int n, int m) {
memset(sumHor, 0, sizeof(sumHor));
memset(sumVert, 0, sizeof(sumVert));
for (int i = 0; i < n; i++) {
sumHor[i] = popcnt(a[i], m);
xor2(sumVert, a[i], m);
}
for (int i = 0; i < n; i++) {
xor3(sumCross[i], a[i], sumVert, m);
if(sumHor[i]&1)
inverse(sumCross[i], m);
}
}
void quit(int x) {
cout << x << "\n";
exit(0);
}
int main() {
read();
if (n == 1 || m == 1) {
int kol[2] = {0, 0};
for (int i = 0; i < n; i++) {
int x = popcnt(a[i], m);
kol[0] += m - x;
kol[1] += x;
}
if (kol[0] && kol[1])
quit(-1);
else if (kol[0])
quit(0);
else
quit(1);
}
if (!(n&1) && !(m&1)) {
findSumCross(n, m);
int ans = 0;
for (int i = 0; i < n; i++)
ans += popcnt(sumCross[i], m);
quit(ans);
}
if (!(n&1) && (m&1)) {
findSumCross(n, m-1);
int kolOdd[n];
memset(kolOdd, 0, sizeof(kolOdd));
for (int i = 0; i < n; i++)
kolOdd[i] += popcnt(sumCross[i], m - 1);
int kolEq[2] = {0, 0};
for (int i = 0; i < n; i++)
kolEq[(kolOdd[i]&1)^get(a[i],m-1)]++;
if (kolEq[0] && kolEq[1])
quit(-1);
vector<int> deltas;
int ans = 1000000000;
int ans1 = 0;
for (int i = 0; i < n; i++)
if (kolEq[0]) {
ans1 += kolOdd[i];
int d = m - 1 - 2*kolOdd[i] + 1;
deltas.push_back(d);
} else {
ans1 += m - 1 - kolOdd[i];
int d = 1 + 2*kolOdd[i] - m + 1;
deltas.push_back(d);
}
if (kolEq[0])
ans = min(ans, ans1);
sort(deltas.begin(), deltas.end());
for (int i = 0; i < n; i++) {
ans1 += deltas[i];
if (kolEq[(i+1)&1])
ans = min(ans, ans1);
}
quit(ans);
}
if( (n&1) && !(m&1) ) {
findSumCross(n-1, m);
u32 kolOdd[m];
memset(kolOdd, 0, sizeof(kolOdd));
for (int i = 0; i+1 < n; i++)
for (int j = 0; j < m; ++j)
kolOdd[j] += get(sumCross[i], j);
int kolEq[2] = {0, 0};
for (int j = 0; j < m; j++)
kolEq[(kolOdd[j]&1)^get(a[n-1],j)]++;
if (kolEq[0] && kolEq[1])
quit(-1);
vector<int> deltas;
int ans = 1000000000;
int ans1 = 0;
for (int j = 0; j < m; j++)
if (kolEq[0]) {
ans1 += kolOdd[j];
int d = n - 1 - 2*kolOdd[j] + 1;
deltas.push_back(d);
} else {
ans1 += n - 1 - kolOdd[j];
int d = 1 + 2*kolOdd[j] - n + 1;
deltas.push_back(d);
}
if (kolEq[0])
ans = min(ans, ans1);
sort(deltas.begin(), deltas.end());
for (int i = 0; i < m; i++) {
ans1 += deltas[i];
if (kolEq[(i+1)&1])
ans = min(ans, ans1);
}
quit(ans);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 77ms
memory: 5996kb
input:
3000 3000 2 1 2 1 2 1 1 1 1 2 1 1 2 2 1 2 2 2 2 1 1 1 2 1 1 2 2 2 2 2 2 1 1 1 1 2 1 2 1 2 1 2 2 2 2 1 1 2 2 1 1 1 1 2 1 2 1 2 1 2 1 1 1 2 1 1 2 1 2 2 1 1 1 2 2 2 2 1 1 2 2 2 2 2 2 2 1 2 1 1 1 2 2 1 1 1 1 1 2 2 2 2 1 2 2 1 2 1 1 2 2 2 1 1 1 2 1 1 2 1 2 2 2 1 2 2 1 1 2 1 2 2 1 1 1 1 2 1 2 2 1 1 2 2 2 ...
output:
4499318
result:
ok 1 number(s): "4499318"
Test #2:
score: 0
Accepted
time: 62ms
memory: 5972kb
input:
3000 3000 1 2 2 2 1 2 2 1 1 1 2 1 2 1 2 2 2 1 2 1 1 2 2 2 2 2 1 2 2 2 2 2 2 1 2 1 2 2 2 1 1 2 2 1 2 2 1 2 2 2 1 2 1 1 1 2 2 1 2 1 2 1 2 2 1 2 2 1 1 2 2 1 2 2 2 2 2 1 1 2 2 2 1 1 1 1 2 2 1 1 2 1 1 2 1 1 1 1 2 2 2 2 2 2 1 1 2 2 1 1 1 1 2 1 2 2 2 1 2 2 1 2 1 1 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 1 2 2 2 1 2 ...
output:
4500268
result:
ok 1 number(s): "4500268"
Test #3:
score: 0
Accepted
time: 69ms
memory: 5752kb
input:
3000 3000 1 1 2 2 2 2 1 2 1 1 2 1 2 2 2 2 1 2 1 1 1 1 1 2 2 1 2 2 2 2 2 1 1 2 2 2 2 2 1 2 1 2 1 1 2 2 2 1 2 2 2 1 2 1 1 2 2 2 1 2 2 1 2 2 2 1 2 2 2 2 1 1 1 1 1 2 1 2 1 1 2 2 2 2 2 2 2 1 2 1 2 1 1 2 1 2 1 1 1 2 2 2 1 2 1 1 2 1 1 2 1 1 1 2 2 1 2 2 2 2 2 2 2 1 2 2 2 1 1 1 1 1 2 2 2 2 2 2 1 1 2 2 2 2 2 ...
output:
4499488
result:
ok 1 number(s): "4499488"
Test #4:
score: 0
Accepted
time: 37ms
memory: 4892kb
input:
3000 3000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
output:
0
result:
ok 1 number(s): "0"
Test #5:
score: 0
Accepted
time: 34ms
memory: 5828kb
input:
3000 3000 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 ...
output:
9000000
result:
ok 1 number(s): "9000000"
Test #6:
score: 0
Accepted
time: 14ms
memory: 4164kb
input:
712 2467 2 1 2 2 2 2 2 2 2 1 1 1 1 2 1 2 2 2 1 1 2 2 1 1 2 1 2 2 1 2 1 2 2 1 1 2 2 2 1 1 1 2 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 2 2 2 1 1 1 1 1 2 1 2 2 2 2 1 1 2 2 1 1 1 1 2 1 1 1 1 2 1 1 1 2 2 2 2 2 2 1 2 1 1 1 1 1 2 2 2 1 2 2 2 2 1 1 1 1 1 2 2 2 2 1 1 1 1 2 2 1 1 2 1 2 1 1 1 1 1 2 2 2 2 2...
output:
299224
result:
ok 1 number(s): "299224"
Test #7:
score: 0
Accepted
time: 5ms
memory: 4316kb
input:
842 643 1 1 2 1 2 1 2 1 2 1 2 2 1 1 2 2 2 2 2 1 1 1 2 1 1 1 2 1 2 2 1 2 1 1 1 1 1 2 2 1 1 1 1 1 2 1 2 1 2 2 2 1 1 2 2 1 2 1 2 1 1 1 2 1 2 2 2 1 1 1 1 2 2 1 1 2 2 2 2 2 1 2 1 2 1 1 1 1 1 2 1 2 1 1 1 2 2 2 1 1 1 1 1 1 2 2 1 2 2 2 2 1 1 2 1 2 1 1 1 2 1 2 1 1 2 1 1 1 2 2 2 2 1 1 1 2 1 1 1 2 2 1 1 1 2 1 ...
output:
184027
result:
ok 1 number(s): "184027"
Test #8:
score: 0
Accepted
time: 5ms
memory: 4156kb
input:
463 1850 2 1 1 2 2 1 2 1 1 1 1 1 1 1 2 1 1 2 2 2 2 2 1 2 2 2 2 2 1 2 2 2 2 1 2 1 2 1 1 1 1 2 1 2 2 2 1 1 2 1 2 2 1 2 1 1 2 1 2 2 1 2 1 2 1 1 2 2 1 2 2 1 1 2 1 2 2 1 1 2 2 1 2 1 1 2 2 2 2 2 2 2 1 2 1 2 1 1 2 1 2 1 2 1 2 2 2 1 2 1 1 2 2 2 1 2 2 2 1 2 2 1 1 2 1 1 2 1 1 1 2 2 1 1 1 1 2 1 2 1 2 1 2 1 1 1...
output:
411193
result:
ok 1 number(s): "411193"
Test #9:
score: 0
Accepted
time: 8ms
memory: 3916kb
input:
287 820 1 2 1 2 1 1 1 2 2 1 2 2 1 2 2 2 2 1 1 2 2 2 2 1 1 2 1 1 1 1 1 1 2 1 1 2 1 2 1 1 1 1 2 1 1 2 2 2 2 2 2 1 1 1 2 1 2 2 2 2 2 1 1 2 2 1 1 1 1 2 2 1 1 2 2 1 1 2 1 1 2 2 1 2 1 2 1 1 1 1 2 2 1 2 1 1 1 1 2 1 2 1 1 2 1 1 2 2 1 2 1 1 1 1 2 2 2 1 1 1 2 2 1 1 2 2 1 2 1 2 2 1 2 1 2 1 2 2 2 2 2 1 1 2 2 1 ...
output:
75191
result:
ok 1 number(s): "75191"
Test #10:
score: 0
Accepted
time: 0ms
memory: 3868kb
input:
109 160 2 2 1 2 1 1 2 2 1 1 1 1 2 2 2 2 1 2 1 1 2 2 2 1 1 1 1 2 2 1 1 2 1 1 2 1 2 2 2 1 2 2 1 2 2 2 2 2 2 1 1 2 1 1 1 1 2 1 1 2 1 1 1 2 1 2 1 1 2 2 2 2 2 1 2 2 1 2 1 1 2 2 2 2 1 2 1 2 1 1 2 1 2 1 1 2 1 2 2 2 1 1 2 1 1 1 1 2 1 2 1 1 1 2 1 2 1 2 1 2 2 1 2 2 1 2 2 1 2 2 1 2 1 2 2 1 2 1 1 2 1 2 1 2 2 1 ...
output:
2630
result:
ok 1 number(s): "2630"
Test #11:
score: 0
Accepted
time: 9ms
memory: 4112kb
input:
565 558 2 1 1 2 2 1 2 2 2 2 2 1 2 2 2 2 1 1 2 1 1 1 1 1 2 2 1 2 1 1 2 1 1 1 2 2 2 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 1 1 1 2 1 1 2 1 1 1 2 1 1 1 1 1 1 2 2 2 1 2 1 1 1 1 1 2 1 2 2 2 1 1 2 2 2 1 2 1 2 1 2 2 2 1 1 1 1 1 2 2 1 1 1 2 1 2 1 1 1 2 1 2 2 2 2 2 1 2 1 1 2 1 2 2 1 1 2 2 1 2 1 1 1 1 1 2 2 1 2 1 2 1 ...
output:
-1
result:
ok 1 number(s): "-1"
Test #12:
score: 0
Accepted
time: 2ms
memory: 4480kb
input:
1918 1 1 1 1 2 2 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 2 2 2 1 2 1 2 2 1 2 2 1 1 2 2 2 1 1 2 2 1 2 1 2 1 2 1 2 1 1 1 2 1 2 2 2 1 2 2 2 1 2 1 1 1 1 1 1 2 1 1 2 2 1 1 1 2 1 2 2 1 2 2 1 1 2 2 2 1 2 1 1 2 1 2 1 2 2 2 2 2 1 1 1 1 2 1 2 1 1 1 1 1 2 2 1 1 2 2 2 2 2 1 2 1 1 1 1 2 1 2 1 1 1 2 1 2 2 1 1 1 2 1 2 1 1 2...
output:
-1
result:
ok 1 number(s): "-1"
Test #13:
score: 0
Accepted
time: 1ms
memory: 5104kb
input:
1806 2 2 2 1 2 2 1 2 1 2 2 2 1 2 1 2 1 2 2 2 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 2 2 1 2 1 2 1 2 1 2 1 2 1 1 1 2 2 1 2 1 1 1 1 2 2 2 2 2 2 2 2 2 1 1 2 2 2 1 2 2 2 1 2 1 1 1 1 2 1 2 1 1 2 2 2 2 2 2 2 1 2 2 1 2 2 2 1 1 2 2 2 2 1 2 1 2 1 1 2 1 2 1 1 1 1 1 2 1 2 1 2 2 1 1 2 1 1 2 2 2 2 2 1 1 1 1 1 1 1 2 2...
output:
1758
result:
ok 1 number(s): "1758"
Test #14:
score: 0
Accepted
time: 1ms
memory: 4924kb
input:
1544 3 1 2 2 1 1 1 2 1 1 1 2 2 2 1 1 1 2 1 2 1 1 1 2 1 2 2 1 1 2 2 1 2 2 1 2 2 1 1 2 1 2 1 2 1 2 1 2 2 1 1 2 2 1 1 2 1 1 1 2 1 1 1 1 2 2 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 2 2 1 1 2 2 2 2 2 1 1 1 1 2 1 2 1 1 2 2 2 1 1 1 1 2 1 2 1 2 2 1 1 2 1 2 1 1 2 2 2 2 1 1 1 1 1 2 2 1 2 1 1 2 1 1 1 2 1 1 2 2 2 2 1 2 1...
output:
-1
result:
ok 1 number(s): "-1"
Test #15:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
3 2 2 1 2 1 1 1
output:
2
result:
ok 1 number(s): "2"
Test #16:
score: 0
Accepted
time: 2ms
memory: 3696kb
input:
4 1 2 2 2 2
output:
1
result:
ok 1 number(s): "1"
Test #17:
score: 0
Accepted
time: 2ms
memory: 3688kb
input:
3 2 1 2 1 2 1 1
output:
2
result:
ok 1 number(s): "2"
Test #18:
score: 0
Accepted
time: 2ms
memory: 3664kb
input:
1 2 1 1
output:
0
result:
ok 1 number(s): "0"
Test #19:
score: 0
Accepted
time: 2ms
memory: 3696kb
input:
3 2 1 2 1 2 1 1
output:
2
result:
ok 1 number(s): "2"
Test #20:
score: 0
Accepted
time: 83ms
memory: 5892kb
input:
3000 3000 1 1 1 1 1 1 2 1 2 1 1 2 1 2 1 1 2 2 2 1 2 1 1 2 1 1 2 1 1 2 1 1 1 2 2 1 1 1 1 2 1 2 2 2 2 1 1 2 2 2 1 2 2 2 1 1 2 1 1 2 2 2 2 1 2 1 1 1 1 2 2 2 2 1 2 2 1 1 1 2 2 2 1 2 2 1 1 1 1 2 1 2 1 2 2 2 1 1 1 1 1 2 2 1 2 2 1 1 1 1 1 1 1 2 2 1 1 2 1 1 1 2 2 2 2 1 2 2 1 1 1 1 1 2 1 2 2 1 2 1 2 2 2 2 1 ...
output:
4499996
result:
ok 1 number(s): "4499996"
Test #21:
score: 0
Accepted
time: 91ms
memory: 6016kb
input:
2999 3000 1 2 1 1 2 1 2 1 2 1 2 1 1 2 1 2 1 1 1 1 2 1 2 1 1 1 1 2 2 1 2 1 1 1 1 1 2 2 1 2 1 2 1 2 2 2 2 2 2 1 2 2 2 1 2 1 1 2 2 2 2 1 1 2 2 2 2 2 1 2 1 2 2 1 2 2 1 2 1 1 1 2 2 1 2 1 2 1 2 1 1 2 1 1 2 2 2 1 1 2 2 1 2 1 1 2 1 1 2 1 2 1 2 2 1 1 1 1 2 2 2 2 2 1 1 1 1 2 2 1 1 2 1 2 1 2 2 1 2 1 1 1 1 2 2 ...
output:
4423060
result:
ok 1 number(s): "4423060"
Test #22:
score: 0
Accepted
time: 77ms
memory: 6008kb
input:
3000 2999 2 2 2 1 2 1 1 2 2 1 1 2 1 1 2 1 1 2 2 1 2 1 2 1 2 1 2 1 1 2 2 1 1 2 2 1 2 2 2 1 2 2 2 2 1 1 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 1 1 2 2 1 1 1 1 1 2 2 2 2 1 2 1 2 2 1 1 1 2 2 2 2 1 2 1 2 1 1 2 2 1 2 1 2 2 1 1 1 1 2 2 2 2 2 1 1 2 1 2 2 1 2 2 2 1 2 2 1 1 1 1 2 1 2 2 2 1 1 2 2 2 2 2 1 1 2 1 1 2 2 1 ...
output:
4434271
result:
ok 1 number(s): "4434271"