D. Tournament Construction

Codeforces
IDCF850D
Time2000ms
Memory512MB
Difficulty
constructive algorithmsdpgraphsgreedymath
English · Original
Chinese · Translation
Formal · Original
Ivan is reading a book about tournaments. He knows that a tournament is an oriented graph with exactly one oriented edge between each pair of vertices. The score of a vertex is the number of edges going outside this vertex. Yesterday Ivan learned Landau's criterion: there is tournament with scores _d_1 ≤ _d_2 ≤ ... ≤ _d__n_ if and only if for all 1 ≤ _k_ < _n_ and . Now, Ivan wanna solve following problem: given a **set** of numbers _S_ = {_a_1, _a_2, ..., _a__m_}, is there a tournament with given set of scores? I.e. is there tournament with sequence of scores _d_1, _d_2, ..., _d__n_ such that if we remove duplicates in scores, we obtain the required set {_a_1, _a_2, ..., _a__m_}? Find a tournament with **minimum** possible number of vertices. ## Input The first line contains a single integer _m_ (1 ≤ _m_ ≤ 31). The next line contains _m_ distinct integers _a_1, _a_2, ..., _a__m_ (0 ≤ _a__i_ ≤ 30) — elements of the set _S_. It is guaranteed that all elements of the set are distinct. ## Output If there are no such tournaments, print string "_\=(_" (without quotes). Otherwise, print an integer _n_ — the number of vertices in the tournament. Then print _n_ lines with _n_ characters — matrix of the tournament. The _j_\-th element in the _i_\-th row should be 1 if the edge between the _i_\-th and the _j_\-th vertices is oriented towards the _j_\-th vertex, and 0 otherwise. The main diagonal should contain only zeros. [samples]
Ivan 正在阅读一本关于锦标赛的书。他了解到,锦标赛是一个有向图,其中每对顶点之间恰好有一条有向边。一个顶点的得分是从此顶点出发的边的数量。 昨天,Ivan 学习了 Landau 准则:存在一个锦标赛,其得分为 #cf_span[d1 ≤ d2 ≤ ... ≤ dn],当且仅当对所有 #cf_span[1 ≤ k < n] 有 。 现在,Ivan 想解决以下问题:给定一个 *集合* #cf_span[S = {a1, a2, ..., am}],是否存在一个锦标赛,其得分集合恰好为该集合?即,是否存在一个得分序列 #cf_span[d1, d2, ..., dn],使得在去除重复项后,得到的集合恰好为 #cf_span[{a1, a2, ..., am}]? 请找出具有 *最小* 顶点数的锦标赛。 第一行包含一个整数 #cf_span[m] (#cf_span[1 ≤ m ≤ 31])。 第二行包含 #cf_span[m] 个互不相同的整数 #cf_span[a1, a2, ..., am] (#cf_span[0 ≤ ai ≤ 30]) —— 集合 #cf_span[S] 的元素。保证集合中所有元素互不相同。 如果不存在这样的锦标赛,请输出字符串 "_=(_"(不含引号)。 否则,输出一个整数 #cf_span[n] —— 锦标赛的顶点数。 然后输出 #cf_span[n] 行,每行包含 #cf_span[n] 个字符 —— 锦标赛的邻接矩阵。第 #cf_span[i] 行的第 #cf_span[j] 个字符应为 #cf_span[1],表示从第 #cf_span[i] 个顶点到第 #cf_span[j] 个顶点的边方向朝向第 #cf_span[j] 个顶点;否则为 #cf_span[0]。主对角线上的元素全为 0。 ## Input 第一行包含一个整数 #cf_span[m] (#cf_span[1 ≤ m ≤ 31])。第二行包含 #cf_span[m] 个互不相同的整数 #cf_span[a1, a2, ..., am] (#cf_span[0 ≤ ai ≤ 30]) —— 集合 #cf_span[S] 的元素。保证集合中所有元素互不相同。 ## Output 如果不存在这样的锦标赛,请输出字符串 "_=(_"(不含引号)。否则,输出一个整数 #cf_span[n] —— 锦标赛的顶点数。然后输出 #cf_span[n] 行,每行包含 #cf_span[n] 个字符 —— 锦标赛的邻接矩阵。第 #cf_span[i] 行的第 #cf_span[j] 个字符应为 #cf_span[1],表示从第 #cf_span[i] 个顶点到第 #cf_span[j] 个顶点的边方向朝向第 #cf_span[j] 个顶点;否则为 #cf_span[0]。主对角线上的元素全为 0。 [samples]
Given a set $ S = \{a_1, a_2, \dots, a_m\} $ of distinct non-negative integers, find the minimum integer $ n $ and a tournament graph on $ n $ vertices with score sequence $ (d_1, d_2, \dots, d_n) $ such that the set of distinct scores is exactly $ S $, i.e., $ \{d_1, d_2, \dots, d_n\} = S $, and if no such tournament exists, output "_=(_". A tournament is a complete oriented graph: for every pair $ i \ne j $, exactly one of $ (i,j) $ or $ (j,i) $ is an edge. The score $ d_i $ of vertex $ i $ is its out-degree. Landau’s criterion: A non-decreasing sequence $ (d_1 \le d_2 \le \dots \le d_n) $ is a score sequence of a tournament if and only if: 1. $ \sum_{i=1}^k d_i \ge \binom{k}{2} $ for all $ 1 \le k < n $, 2. $ \sum_{i=1}^n d_i = \binom{n}{2} $. --- **Formal Problem Statement:** Let $ S = \{a_1, a_2, \dots, a_m\} \subset \mathbb{Z}_{\ge 0} $, with $ m \le 31 $, $ a_i \le 30 $, all distinct. Find the minimal $ n \in \mathbb{Z}^+ $ such that there exists a sequence $ (d_1, d_2, \dots, d_n) \in \mathbb{Z}_{\ge 0}^n $ satisfying: - $ \{d_1, d_2, \dots, d_n\} = S $ (as sets), - $ \sum_{i=1}^n d_i = \binom{n}{2} $, - $ \sum_{i=1}^k d_{(i)} \ge \binom{k}{2} $ for all $ 1 \le k < n $, where $ d_{(1)} \le d_{(2)} \le \dots \le d_{(n)} $ is the sorted sequence. If no such $ n $ exists, output "_=(_". Otherwise, output: - $ n $ - An $ n \times n $ adjacency matrix $ A = (a_{ij}) $, where: - $ a_{ii} = 0 $, - $ a_{ij} \in \{0,1\} $ for $ i \ne j $, - $ a_{ij} + a_{ji} = 1 $ for all $ i \ne j $, - $ \sum_{j=1}^n a_{ij} = d_i $ for all $ i $. --- **Note:** The problem reduces to: over all $ n \ge \max(S) $, check whether there exists a multiset of $ n $ scores, using only values from $ S $, that satisfies Landau’s conditions. Find the minimal such $ n $, and construct the corresponding tournament.
Samples
Input #1
2
1 2
Output #1
4
0011
1001
0100
0010
Input #2
2
0 3
Output #2
6
000111
100011
110001
011001
001101
000000
API Response (JSON)
{
  "problem": {
    "name": "D. Tournament Construction",
    "description": {
      "content": "Ivan is reading a book about tournaments. He knows that a tournament is an oriented graph with exactly one oriented edge between each pair of vertices. The score of a vertex is the number of edges goi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 524288
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF850D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Ivan is reading a book about tournaments. He knows that a tournament is an oriented graph with exactly one oriented edge between each pair of vertices. The score of a vertex is the number of edges goi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "Ivan 正在阅读一本关于锦标赛的书。他了解到,锦标赛是一个有向图,其中每对顶点之间恰好有一条有向边。一个顶点的得分是从此顶点出发的边的数量。\n\n昨天,Ivan 学习了 Landau 准则:存在一个锦标赛,其得分为 #cf_span[d1 ≤ d2 ≤ ... ≤ dn],当且仅当对所有 #cf_span[1 ≤ k < n] 有 。\n\n现在,Ivan 想解决以下问题:给定一个 *集合* #cf_...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Given a set $ S = \\{a_1, a_2, \\dots, a_m\\} $ of distinct non-negative integers, find the minimum integer $ n $ and a tournament graph on $ n $ vertices with score sequence $ (d_1, d_2, \\dots, d_n) $ s...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments