C. Equal Sums

Codeforces
IDCF988C
Time2000ms
Memory256MB
Difficulty
implementationsortings
English · Original
Chinese · Translation
Formal · Original
You are given $k$ sequences of integers. The length of the $i$\-th sequence equals to $n_i$. You have to choose exactly two sequences $i$ and $j$ ($i \ne j$) such that you can remove exactly one element in each of them in such a way that the sum of the changed sequence $i$ (its length will be equal to $n_i - 1$) equals to the sum of the changed sequence $j$ (its length will be equal to $n_j - 1$). Note that it's **required** to remove exactly one element in each of the two chosen sequences. Assume that the sum of the empty (of the length equals $0$) sequence is $0$. ## Input The first line contains an integer $k$ ($2 \le k \le 2 \cdot 10^5$) — the number of sequences. Then $k$ pairs of lines follow, each pair containing a sequence. The first line in the $i$\-th pair contains one integer $n_i$ ($1 \le n_i < 2 \cdot 10^5$) — the length of the $i$\-th sequence. The second line of the $i$\-th pair contains a sequence of $n_i$ integers $a_{i, 1}, a_{i, 2}, \dots, a_{i, n_i}$. The elements of sequences are integer numbers from $-10^4$ to $10^4$. The sum of lengths of all given sequences don't exceed $2 \cdot 10^5$, i.e. $n_1 + n_2 + \dots + n_k \le 2 \cdot 10^5$. ## Output If it is impossible to choose two sequences such that they satisfy given conditions, print "_NO_" (without quotes). Otherwise in the first line print "_YES_" (without quotes), in the second line — two integers $i$, $x$ ($1 \le i \le k, 1 \le x \le n_i$), in the third line — two integers $j$, $y$ ($1 \le j \le k, 1 \le y \le n_j$). It means that the sum of the elements of the $i$\-th sequence without the element with index $x$ equals to the sum of the elements of the $j$\-th sequence without the element with index $y$. Two chosen sequences must be distinct, i.e. $i \ne j$. You can print them in any order. If there are multiple possible answers, print any of them. [samples] ## Note In the first example there are two sequences $[2, 3, 1, 3, 2]$ and $[1, 1, 2, 2, 2, 1]$. You can remove the second element from the first sequence to get $[2, 1, 3, 2]$ and you can remove the sixth element from the second sequence to get $[1, 1, 2, 2, 2]$. The sums of the both resulting sequences equal to $8$, i.e. the sums are equal.
你被给予 $k$ 个整数序列。第 $i$ 个序列的长度为 $n_i$。 你需要恰好选择两个序列 $i$ 和 $j$($i \ne j$),使得你可以从每个序列中恰好移除一个元素,从而使得修改后的序列 $i$(长度变为 $n_i - 1$)的和等于修改后的序列 $j$(长度变为 $n_j - 1$)的和。 注意,必须从两个选中的序列中各恰好移除一个元素。 假设空序列(长度为 $0$)的和为 $0$。 第一行包含一个整数 $k$($2 \le k \le 2 \cdot 10^5$)——序列的数量。 接下来有 $k$ 对行,每对包含一个序列。 第 $i$ 对中的第一行包含一个整数 $n_i$($1 \le n_i < 2 \cdot 10^5$)——第 $i$ 个序列的长度。第二行包含 $n_i$ 个整数 $a_{i,1}, a_{i,2}, \dots, a_{i,n_i}$,构成该序列。 序列中的元素为范围在 $-10^4$ 到 $10^4$ 之间的整数。 所有给定序列的长度之和不超过 $2 \cdot 10^5$,即 $n_1 + n_2 + \dots + n_k \le 2 \cdot 10^5$。 如果不可能选择两个满足条件的序列,请输出 "_NO_"(不含引号)。否则,第一行输出 "_YES_"(不含引号),第二行输出两个整数 $i$, $x$($1 \le i \le k$, $1 \le x \le n_i$),第三行输出两个整数 $j$, $y$($1 \le j \le k$, $1 \le y \le n_j$)。这表示从第 $i$ 个序列中移除下标为 $x$ 的元素后的元素和,等于从第 $j$ 个序列中移除下标为 $y$ 的元素后的元素和。 两个选中的序列必须不同,即 $i \ne j$。你可以按任意顺序输出它们。 如果有多个可能的答案,输出任意一个即可。 在第一个例子中,有两个序列 $[2, 3, 1, 3, 2]$ 和 $[1, 1, 2, 2, 2, 1]$。你可以从第一个序列中移除第二个元素得到 $[2, 1, 3, 2]$,从第二个序列中移除第六个元素得到 $[1, 1, 2, 2, 2]$。两个结果序列的和均为 $8$,即和相等。 ## Input 第一行包含一个整数 $k$($2 \le k \le 2 \cdot 10^5$)——序列的数量。接下来有 $k$ 对行,每对包含一个序列。第 $i$ 对中的第一行包含一个整数 $n_i$($1 \le n_i < 2 \cdot 10^5$)——第 $i$ 个序列的长度。第二行包含 $n_i$ 个整数 $a_{i,1}, a_{i,2}, \dots, a_{i,n_i}$,构成该序列。序列中的元素为范围在 $-10^4$ 到 $10^4$ 之间的整数。所有给定序列的长度之和不超过 $2 \cdot 10^5$,即 $n_1 + n_2 + \dots + n_k \le 2 \cdot 10^5$。 ## Output 如果不可能选择两个满足条件的序列,请输出 "_NO_"(不含引号)。否则,第一行输出 "_YES_"(不含引号),第二行输出两个整数 $i$, $x$($1 \le i \le k$, $1 \le x \le n_i$),第三行输出两个整数 $j$, $y$($1 \le j \le k$, $1 \le y \le n_j$)。这表示从第 $i$ 个序列中移除下标为 $x$ 的元素后的元素和,等于从第 $j$ 个序列中移除下标为 $y$ 的元素后的元素和。两个选中的序列必须不同,即 $i \ne j$。你可以按任意顺序输出它们。如果有多个可能的答案,输出任意一个即可。 [samples] ## Note 在第一个例子中,有两个序列 $[2, 3, 1, 3, 2]$ 和 $[1, 1, 2, 2, 2, 1]$。你可以从第一个序列中移除第二个元素得到 $[2, 1, 3, 2]$,从第二个序列中移除第六个元素得到 $[1, 1, 2, 2, 2]$。两个结果序列的和均为 $8$,即和相等。
Let $ S_i = \sum_{m=1}^{n_i} a_{i,m} $ denote the sum of the $i$-th sequence. For each sequence $i$, define the set of possible sums after removing exactly one element: $$ T_i = \left\{ S_i - a_{i,m} \mid 1 \leq m \leq n_i \right\} $$ We seek distinct indices $i \ne j$ and indices $x \in [1, n_i]$, $y \in [1, n_j]$ such that: $$ S_i - a_{i,x} = S_j - a_{j,y} $$ Equivalently, we seek $i \ne j$ such that $T_i \cap T_j \ne \emptyset$. **Objective:** Find such a pair $(i, x)$ and $(j, y)$, or determine that no such pair exists. --- **Formal Statement:** Given: - $k \in \mathbb{N}$, $2 \leq k \leq 2 \cdot 10^5$ - For each $i \in \{1, \dots, k\}$: - $n_i \in \mathbb{N}$, $1 \leq n_i < 2 \cdot 10^5$ - Sequence $a_i = (a_{i,1}, a_{i,2}, \dots, a_{i,n_i}) \in \mathbb{Z}^{n_i}$, with $|a_{i,m}| \leq 10^4$ - $\sum_{i=1}^k n_i \leq 2 \cdot 10^5$ Define: - $S_i = \sum_{m=1}^{n_i} a_{i,m}$ - $T_i = \{ S_i - a_{i,m} \mid m = 1, \dots, n_i \}$ **Find:** Indices $i, j \in \{1, \dots, k\}$, $i \ne j$, and indices $x \in \{1, \dots, n_i\}$, $y \in \{1, \dots, n_j\}$ such that: $$ S_i - a_{i,x} = S_j - a_{j,y} $$ If no such pair exists, output "NO". Otherwise, output "YES", followed by $(i, x)$ and $(j, y)$.
Samples
Input #1
2
5
2 3 1 3 2
6
1 1 2 2 2 1
Output #1
YES
2 6
1 2
Input #2
3
1
5
5
1 1 1 1 1
2
2 3
Output #2
NO
Input #3
4
6
2 2 2 2 2 2
5
2 2 2 2 2
3
2 2 2
5
2 2 2 2 2
Output #3
YES
2 2
4 1
API Response (JSON)
{
  "problem": {
    "name": "C. Equal Sums",
    "description": {
      "content": "You are given $k$ sequences of integers. The length of the $i$\\-th sequence equals to $n_i$. You have to choose exactly two sequences $i$ and $j$ ($i \\ne j$) such that you can remove exactly one elem",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF988C"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given $k$ sequences of integers. The length of the $i$\\-th sequence equals to $n_i$.\n\nYou have to choose exactly two sequences $i$ and $j$ ($i \\ne j$) such that you can remove exactly one elem...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给予 $k$ 个整数序列。第 $i$ 个序列的长度为 $n_i$。\n\n你需要恰好选择两个序列 $i$ 和 $j$($i \\ne j$),使得你可以从每个序列中恰好移除一个元素,从而使得修改后的序列 $i$(长度变为 $n_i - 1$)的和等于修改后的序列 $j$(长度变为 $n_j - 1$)的和。\n\n注意,必须从两个选中的序列中各恰好移除一个元素。\n\n假设空序列(长度为 $0$)的和为 $...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ S_i = \\sum_{m=1}^{n_i} a_{i,m} $ denote the sum of the $i$-th sequence.\n\nFor each sequence $i$, define the set of possible sums after removing exactly one element:\n$$\nT_i = \\left\\{ S_i - a_{i,m}...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments