Luogu P2474 [SCOI2008]天平

题目描述

https://www.luogu.com.cn/problem/P2474

Solution

题目要求 $A+B>C+D$ 的数量

移项得到 $A-C>D-B$ 和 $A-D>C-B$

这个东西就是差分约束求出来的东西,由于题目只要求统计结果唯一的情况

所以我们拿差分约束算出 $i-j$ 的取值范围即可

时间复杂度 $O(n^3)$

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#define maxn 51
#define INF 1000000000
using namespace std;

int n, A, B;

int f[maxn][maxn], g[maxn][maxn];
void floyd() {
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j) {
if (i == j || i == k || j == k) continue;
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
g[i][j] = max(g[i][j], g[i][k] + g[k][j]);
}
}

int s1, s2, s3;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);

cin >> n >> A >> B;
for (int i = 1; i <= n; ++i) {
char s[maxn]; cin >> s + 1;
for (int j = 1; j <= n; ++j)
if (s[j] == '+') g[i][j] = -2, f[i][j] = -1;
else if (s[j] == '-') g[i][j] = 1, f[i][j] = 2;
else if (s[j] == '?') g[i][j] = -2, f[i][j] = 2;
else f[i][j] = g[i][j] = 0;
} floyd();
for (int C = 1; C <= n; ++C)
for (int D = C + 1; D <= n; ++D) {
if (C == A || D == A || C == B || D == B) continue;
if (g[C][A] > f[B][D] || g[D][A] > f[B][C]) ++s1;
if (f[C][A] < g[B][D] || f[D][A] < g[B][C]) ++s3;
if (f[C][A] == g[C][A] && f[B][D] == g[B][D] && f[C][A] == f[B][D]) ++s2;
else if (f[D][A] == g[D][A] && f[B][C] == g[B][C] && f[D][A] == f[B][C]) ++s2;
}
cout << s1 << " " << s2 << " " << s3 << "\n";
return 0;
}