P5123 [USACO18DEC]Cowpatibility G 题解
P5123 [USACO18DEC]Cowpatibility G
这题正着直接做显然不行,所以要反着容斥做。
记录
这个匹配的个数显然可以用
时间复杂度:
代码
#include <bits/stdc++.h> using namespace std; using ll = long long; const int kMaxN = 5e4 + 5; map <tuple <int>, int> mp1; map <tuple <int, int>, int> mp2; map <tuple <int, int, int>, int> mp3; map <tuple <int, int, int, int>, int> mp4; map <tuple <int, int, int, int, int>, int> mp5; int n; int s[6], a, b, c, d, e; ll ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d%d%d%d%d", &s[1], &s[2], &s[3], &s[4], &s[5]); sort(s + 1, s + 6); a = s[1], b = s[2], c = s[3], d = s[4], e = s[5]; ans += i - 1; ans -= mp1[make_tuple(a)] + mp1[make_tuple(b)] + mp1[make_tuple(c)] + mp1[make_tuple(d)] + mp1[make_tuple(e)]; ans += mp2[make_tuple(a, b)] + mp2[make_tuple(a, c)] + mp2[make_tuple(a, d)] + mp2[make_tuple(a, e)] + mp2[make_tuple(b, c)] + mp2[make_tuple(b, d)] + mp2[make_tuple(b, e)] + mp2[make_tuple(c, d)] + mp2[make_tuple(c, e)] + mp2[make_tuple(d, e)]; ans -= mp3[make_tuple(a, b, c)] + mp3[make_tuple(a, b, d)] + mp3[make_tuple(a, b, e)] + mp3[make_tuple(a, c, d)] + mp3[make_tuple(a, c, e)] + mp3[make_tuple(a, d, e)] + mp3[make_tuple(b, c, d)] + mp3[make_tuple(b, c, e)] + mp3[make_tuple(b, d, e)] + mp3[make_tuple(c, d, e)]; ans += mp4[make_tuple(a, b, c, d)] + mp4[make_tuple(a, b, c, e)] + mp4[make_tuple(a, b, d, e)] + mp4[make_tuple(a, c, d, e)] + mp4[make_tuple(b, c, d, e)]; ans -= mp5[make_tuple(a, b, c, d, e)]; ++mp1[make_tuple(a)], ++mp1[make_tuple(b)], ++mp1[make_tuple(c)], ++mp1[make_tuple(d)], ++mp1[make_tuple(e)]; ++mp2[make_tuple(a, b)], ++mp2[make_tuple(a, c)], ++mp2[make_tuple(a, d)], ++mp2[make_tuple(a, e)], ++mp2[make_tuple(b, c)], ++mp2[make_tuple(b, d)], ++mp2[make_tuple(b, e)], ++mp2[make_tuple(c, d)], ++mp2[make_tuple(c, e)], ++mp2[make_tuple(d, e)]; ++mp3[make_tuple(a, b, c)], ++mp3[make_tuple(a, b, d)], ++mp3[make_tuple(a, b, e)], ++mp3[make_tuple(a, c, d)], ++mp3[make_tuple(a, c, e)], ++mp3[make_tuple(a, d, e)], ++mp3[make_tuple(b, c, d)], ++mp3[make_tuple(b, c, e)], ++mp3[make_tuple(b, d, e)], ++mp3[make_tuple(c, d, e)]; ++mp4[make_tuple(a, b, c, d)], ++mp4[make_tuple(a, b, c, e)], ++mp4[make_tuple(a, b, d, e)], ++mp4[make_tuple(a, c, d, e)], ++mp4[make_tuple(b, c, d, e)]; ++mp5[make_tuple(a, b, c, d, e)]; } printf("%lld\n", ans); cerr << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl; return 0; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步