2020牛客寒假算法基础集训营2 I题建通道
牛客网链接:https://ac.nowcoder.com/acm/contest/3003/I
题解:第一眼最小生成树,然后算一波时间复杂度超时...(比赛完大佬说最小生成树可以过......)
放一下官方题解:
tai 6 le
#include <cstdio> #include <algorithm> #include <cassert> using namespace std; const int N = 2e5 + 5; int n, k; int v[N], va, vo, ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &v[i]); sort(v + 1, v + n + 1); va = 0x7fffffff; for (int i = 1; i <= n; i++) { va &= v[i]; vo |= v[i]; } v[n + 1] = 2e9; for (int i = n; i; i--) k += (v[i] != v[i + 1]); va ^= vo; for (int i = 0; i <= 30; i++) { int cur = 1 << i; if (va & cur) { ans = cur * (k - 1); break; } } printf("%d\n", ans); return 0; }