Codeforces Round #432 (Div. 1, based on IndiaHacks Final Round 2017) D. Tournament Construction(dp + 构造)
题意
一个竞赛图的度数集合是由该竞赛图中每个点的出度所构成的集合。
现给定一个 \(m\) 个元素的集合,第 \(i\) 个元素是 \(a_i\) 。(此处集合已经去重)
判断其是否是一个竞赛图的度数集合,如果是,找到点数最小的满足条件的竞赛图,并构造方案。
\(m, a_i \le 30\) ,\(a_i\) 互不相同。
题解
首先给出结论:假如给出每个点的出度,那么这些点能形成一个竞赛图当且仅当排序后的序列 \(d_1, d_2, d_3, \dots , d_n\) 满足对于所有 \(k < n\) 有 \(\displaystyle \sum _{k_i=1}^{k} d_i ≥ {k \choose 2}\) 且 \(\displaystyle \sum_{i = 1} ^ {n} d_i = {n \choose 2}\) 。
考虑证明,不难发现对于竞赛图的任意点集 \(S\) ,都必须满足 \(\displaystyle \sum_{i \in S} d_i \ge {|S| \choose 2}\) ,因为点集 \(S\) 之间的一对点至少会存在一条边贡献一个出度。
并且由于
\]
所以发现最后的点数是 \(O(\max \{a_i\})\) 级别的,其实最多就是 \(2 \max \{a_i\} + 1\) 。
那么就可以 \(dp\) 了,先将给定集合中的元素从小到大排序。
设 \(f[i][j][sum]\) 表示考虑完前 \(i\) 个元素,当前图中已经有 \(j\) 个点了,度数之和为 \(sum\) 是否可行。
对于这个 \(dp\) 转移时枚举下一个元素出现了几次就行了。
然后我们只需要找到一个最小的 \(i\) 使得 \(\displaystyle f[n][i][{i \choose 2}]\) 满足就行了,这个就是合法点数。
似乎一定可以构造出方案。。不存在无解qwq 至于原因,可以去问 zhou888 。
然后需要构造方案,我们转移 \(dp\) 的时候多记一下当前如何转移的就行了,最后逆推回去就得到了度数可重集合。
然后我们用这个度数可重集合来构造答案,每次将度数从小到大排序,然后把第一个点向后面出度个点连边就行了,不难发现这样一定可以构造出一组合法解。
最后复杂度就是 \(O(n{a_i}^3)\) 的,可以通过qwq
总结
对于竞赛图可以考虑任意一个子集度数都不少于 \(\displaystyle {n \choose 2}\) 的性质。
然后考虑构造图的时候可以贪心的去暴力分配度数,或者用网络流模拟这个过程也行qwq
代码
用了 goto 以及 lambda 函数等骚操作。。
#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
template<typename T> inline bool chkmin(T &a, T b) {return b < a ? a = b, 1 : 0;}
template<typename T> inline bool chkmax(T &a, T b) {return b > a ? a = b, 1 : 0;}
inline int read() {
int x(0), sgn(1); char ch(getchar());
for (; !isdigit(ch); ch = getchar()) if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48);
return x * sgn;
}
void File() {
#ifdef zjp_shadow
freopen ("D.in", "r", stdin);
freopen ("D.out", "w", stdout);
#endif
}
const int Maxn = 35, N = 65;
bitset<2510> dp[Maxn][N], pre[Maxn][N];
int n, m, a[N], d[N], sum[N]; bitset<N> vis, G[N];
inline int Comb(int x) {
return x * (x - 1) / 2;
}
int main () {
File();
n = read();
For (i, 1, n) a[i] = read();
sort(a + 1, a + n + 1);
For (i, 1, n) sum[i] = sum[i - 1] + a[i];
dp[0][0][0] = true;
int pos = n;
For(j, 1, 61) For (i, 1, n) {
For (k, max(sum[i], Comb(j)), j * 30) {
if (dp[i - 1][j - 1][k - a[i]]) dp[i][j][k] = pre[i][j][k] = true;
if (dp[i][j - 1][k - a[i]]) dp[i][j][k] = !(pre[i][j][k] = false);
}
if (dp[n][j][Comb(j)]) { n = j; goto tag; }
}
tag: ;
int deg = Comb(n);
Fordown (i, n, 1)
d[i] = a[pos], pos -= pre[pos][i][deg], deg -= d[i];
int id[N], len;
For (i, 1, n) {
len = 0;
For (j, 1, n) if (!vis[j]) id[++ len] = j;
sort(id + 1, id + len + 1, [&](int lhs, int rhs) { return d[lhs] < d[rhs]; });
vis[id[1]] = true;
For (j, 2, d[id[1]] + 1) G[id[1]][id[j]] = true;
For (j, d[id[1]] + 2, len) G[id[j]][id[1]] = true, -- d[id[j]];
}
printf ("%d\n", n);
For (i, 1, n)
For (j, 1, n + 1)
putchar (j == jend ? '\n' : G[i][j] + '0');
return 0;
}
Codeforces Round #432 (Div. 1, based on IndiaHacks Final Round 2017) D. Tournament Construction(dp + 构造)的更多相关文章
- D. Arpa and a list of numbers Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)
http://codeforces.com/contest/851/problem/D 分区间操作 #include <cstdio> #include <cstdlib> # ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017)ABCD
A. Arpa and a research in Mexican wave time limit per test 1 second memory limit per test 256 megaby ...
- 【前缀和】【枚举倍数】 Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) D. Arpa and a list of numbers
题意:给你n个数,一次操作可以选一个数delete,代价为x:或者选一个数+1,代价y.你可以进行这两种操作任意次,让你在最小的代价下,使得所有数的GCD不为1(如果全删光也视作合法). 我们从1到m ...
- 【推导】【暴力】Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) C. Five Dimensional Points
题意:给你五维空间内n个点,问你有多少个点不是坏点. 坏点定义:如果对于某个点A,存在点B,C,使得角BAC为锐角,那么A是坏点. 结论:如果n维空间内已经存在2*n+1个点,那么再往里面添加任意多个 ...
- 【推导】Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) B. Arpa and an exam about geometry
题意:给你平面上3个不同的点A,B,C,问你能否通过找到一个旋转中心,使得平面绕该点旋转任意角度后,A到原先B的位置,B到原先C的位置. 只要A,B,C构成等腰三角形,且B为上顶点.那么其外接圆圆心即 ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) D
Arpa has found a list containing n numbers. He calls a list bad if and only if it is not empty and g ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) C
You are given set of n points in 5-dimensional space. The points are labeled from 1 to n. No two poi ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) B
Arpa is taking a geometry exam. Here is the last problem of the exam. You are given three points a, ...
- Codeforces Round #432 (Div. 2, based on IndiaHacks Final Round 2017) A
Arpa is researching the Mexican wave. There are n spectators in the stadium, labeled from 1 to n. Th ...
随机推荐
- maven工程下get的URI中带中文名称乱码解决
在用maven做项目时,出现了乱码问题: http://localhost:8086/search.html?keyword=手机 经过检查发现已经在web.xml配置request等字符编码 < ...
- java开发中使用枚举表述数据字典
一.用枚举表述数据字典 1.代码: package com.inspire.jdk.caculate; /** * Created by yaming * 用枚举表述常量数据字段 */ public ...
- [转][mysql]创建函数失败(1418错误)mysql双主模式导致的问题
https://blog.csdn.net/qq523786283/article/details/75102170
- 三、如何设置npm镜像
一.临时使用 npm --registry https://registry.npm.taobao.org install express 二.永久使用 npm config set registry ...
- javascript中的 return false和return true
关于javascript中的 return false和return true,return 是javascript里函数返回值的关键字,一个函数内处理的结果可以使用return 返回,这样在调用函数 ...
- [转帖]Linux:cut命令详解
Linux:cut命令详解 https://www.cnblogs.com/Spiro-K/p/6361646.html cut -f cut -f -d cut -c1- 这三个命令好像最常见, 记 ...
- AngularJS基于MVC的复杂操作案例
AngularJS基于MVC的复杂操作案例 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- Day 6-1计算机网络基础&TCP/IP
按照功能不同,人们将互联网协议分为osi七层或tcp/ip五层或tcp/ip四层(我们只需要掌握tcp/ip五层协议即可) 每层运行常见物理设备: TCP/IP协议: Transmission Con ...
- MyBaits全局配置文件的各项标签1
■dtd约束 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ...
- Flutter之 LimitedBox、Offstage、OverflowBox、SizedBox详解
1. LimitedBox A box that limits its size only when it's unconstrained. 1.1 简介 LimitedBox,通过字面意思,也可以猜 ...