POI2012SQU-Squarks
POI #Year2012 #数学
考虑如果将 和 都排序,那么 ,
考虑枚举一个 ,此时就可以确定
假设当前确定到 ,将已经确定的 组成的 去掉,剩下的最小的 一定为
考虑 实际上不可能是 以后的数,所以只要枚举 个
// Author: xiaruize const int N = 5e4 + 10; int n, tot; int sum[N]; int res[305][305], t; int tmp[305]; multiset<int> s; void calc(int x) { s.clear(); mms(tmp, 0); if (((sum[1] + sum[2] + sum[x]) & 1)) return; tmp[1] = (sum[1] + sum[2] + sum[x]) / 2 - sum[x]; tmp[2] = ((sum[1] + sum[2] + sum[x]) / 2 - sum[2]); tmp[3] = ((sum[1] + sum[2] + sum[x]) / 2 - sum[1]); if (tmp[1] < 0 || tmp[2] < 0 || tmp[3] < 0) return; rep(i, 1, tot) s.insert(sum[i]); s.erase(s.find(sum[1])); s.erase(s.find(sum[2])); s.erase(s.find(sum[x])); // debug(tmp, s); rep(i, 4, n) { tmp[i] = ((*s.begin()) - tmp[1]); if (tmp[i - 1] >= tmp[i]) return; rep(j, 1, i - 1) { auto it = s.find(tmp[i] + tmp[j]); if (it == s.end()) return; s.erase(it); } } t++; rep(i, 1, n) res[t][i] = tmp[i]; } void solve() { cin >> n; tot = (n - 1) * n / 2; rep(i, 1, tot) cin >> sum[i]; stable_sort(sum + 1, sum + tot + 1); rep(i, 3, tot) { if (i == 3 || sum[i] != sum[i - 1]) calc(i); if ((double)clock() / CLOCKS_PER_SEC * 1000.0 > 500) break; } cout << t << endl; rep(j, 1, t) { rep(i, 1, n) { cout << res[j][i] << ' '; } cout << endl; } } #ifndef ONLINE_JUDGE bool end_of_memory_use; #endif signed main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int testcase = 1; // cin >> testcase; while (testcase--) solve(); #ifndef ONLINE_JUDGE cerr << "Memory use:" << (&end_of_memory_use - &start_of_memory_use) / 1024.0 / 1024.0 << "MiB" << endl; cerr << "Time use:" << (double)clock() / CLOCKS_PER_SEC * 1000.0 << "ms" << endl; #endif return 0; }
本文作者:xiaruize's Blog
本文链接:https://www.cnblogs.com/xiaruize/p/18156738
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步