J. Grid Beauty

Codeforces
IDCF10215J
Time3000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You are given a grid $g$ consisting of $n$ rows each of which is divided into $m$ columns. You can swap any two integers in the same row infinitely, but you cannot swap two integers from two different rows. Your task is to maximize the beauty of the grid by rearranging integers in each row. The beauty of the grid is the number of pairs ($i, thin j$) ($1 < i <= n, thin 1 <= j <= m$) such that $g_{i , j}$ is equal to $g_{i -1 , j}$ (i.e. $g_{i , j} equiv g_{i -1 , j}$). The first line contains an integer $T$ ($1 <= T <= 5$) specifying the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 <= n, m <= 10^3$), giving the number of rows and columns in the grid, respectively. Then $n$ lines follow, each line contains $m$ integers, giving the grid. All values in the grid are between $1$ and $10^8$ (inclusive). For each test case, print a single line containing the beauty of the grid. As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use _scanf/printf_ instead of _cin/cout_ in C++, prefer to use _BufferedReader/PrintWriter_ instead of _Scanner/System.out_ in Java. ## Input The first line contains an integer $T$ ($1 <= T <= 5$) specifying the number of test cases.The first line of each test case contains two integers $n$ and $m$ ($1 <= n, m <= 10^3$), giving the number of rows and columns in the grid, respectively.Then $n$ lines follow, each line contains $m$ integers, giving the grid. All values in the grid are between $1$ and $10^8$ (inclusive). ## Output For each test case, print a single line containing the beauty of the grid. [samples] ## Note As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use _scanf/printf_ instead of _cin/cout_ in C++, prefer to use _BufferedReader/PrintWriter_ instead of _Scanner/System.out_ in Java.
**Definitions** Let $ T \in \mathbb{Z} $ be the number of test cases. For each test case $ k \in \{1, \dots, T\} $: - Let $ n_k, m_k \in \mathbb{Z} $ denote the number of rows and columns. - Let $ G_k = (g_{k,i,j})_{1 \le i \le n_k, 1 \le j \le m_k} $ be the grid, where $ g_{k,i,j} \in \mathbb{Z} $ and $ 1 \le g_{k,i,j} \le 10^8 $. **Constraints** 1. $ 1 \le T \le 5 $ 2. For each $ k \in \{1, \dots, T\} $: - $ 1 \le n_k, m_k \le 10^3 $ - $ 1 \le g_{k,i,j} \le 10^8 $ for all $ i,j $ **Objective** For each test case $ k $, rearrange the elements within each row $ i $ arbitrarily (i.e., permute $ (g_{k,i,1}, \dots, g_{k,i,m_k}) $). Maximize the beauty: $$ \sum_{i=2}^{n_k} \sum_{j=1}^{m_k} \mathbf{1}_{\{g_{k,i,j} = g_{k,i-1,j}\}} $$ Output the maximum possible beauty.
API Response (JSON)
{
  "problem": {
    "name": "J. Grid Beauty",
    "description": {
      "content": "You are given a grid $g$ consisting of $n$ rows each of which is divided into $m$ columns. You can swap any two integers in the same row infinitely, but you cannot swap two integers from two differen",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 3000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10215J"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a grid $g$ consisting of $n$ rows each of which is divided into $m$ columns.\n\nYou can swap any two integers in the same row infinitely, but you cannot swap two integers from two differen...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ T \\in \\mathbb{Z} $ be the number of test cases.  \nFor each test case $ k \\in \\{1, \\dots, T\\} $:  \n- Let $ n_k, m_k \\in \\mathbb{Z} $ denote the number of rows and columns.  \n- L...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments