A. The New President

Codeforces
IDCF10015A
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
Finally, it is time to vote for a new president and you are really excited about that. You know that the final results may take weeks to be announced, while you can't really wait to see the results. Somehow you managed to get the preferences list for every voter (we don't care how you managed to get this piece of information!). Each voter sorted out all candidates starting by his most preferred candidate and ending with his least preferred one. When voting, a voter votes for the candidate who comes first in his preferences list. For example, if there are 5 candidates (numbered 1 to 5), and the preferences list for one voter is [3, 2, 5, 1, 4] and the current competing candidates in the voting process are candidates 2 and 4, the voter will vote for candidate number 2. Here are the rules for the election process: Given the preferences lists, you need to write a program to figure out which candidate will win and in which round. Your program will be tested on one or more test cases. The first line of the input will be a single integer *T*, the number of test cases (1  ≤  *T*  ≤  100). Followed by the test cases, the first line of a test case contains two integers *C* and *V* separated by a single space. *C* and *V* (1  ≤  *C*, *V*  ≤  100) represent the number of candidates and voters respectively, followed by *V* lines each line contains *C* integers separated by a single space, representing the preferences list for a single voter (the first is his most preferable candidate while the last is his least preferable one). Each integer from 1 to *C* will appear exactly once in each line. For each test case, print on a single line two integers separated by a single space. The first integer is the ID of the winner candidate (a number from 1 to *C*), the second integer is either 1 or 2 indicating whether this candidate will win in the first round or the second one respectively. ## Input Your program will be tested on one or more test cases. The first line of the input will be a single integer *T*, the number of test cases (1  ≤  *T*  ≤  100). Followed by the test cases, the first line of a test case contains two integers *C* and *V* separated by a single space. *C* and *V* (1  ≤  *C*, *V*  ≤  100) represent the number of candidates and voters respectively, followed by *V* lines each line contains *C* integers separated by a single space, representing the preferences list for a single voter (the first is his most preferable candidate while the last is his least preferable one). Each integer from 1 to *C* will appear exactly once in each line. ## Output For each test case, print on a single line two integers separated by a single space. The first integer is the ID of the winner candidate (a number from 1 to *C*), the second integer is either 1 or 2 indicating whether this candidate will win in the first round or the second one respectively. [samples]
**Definitions** Let $ T \in \mathbb{Z} $ be the number of test cases. For each test case: - Let $ C \in \mathbb{Z} $ be the number of candidates, $ V \in \mathbb{Z} $ the number of voters. - Let $ P = (P_1, P_2, \dots, P_V) $ be the profile of preference lists, where each $ P_j = (p_{j,1}, p_{j,2}, \dots, p_{j,C}) $ is a permutation of $ \{1, 2, \dots, C\} $, representing voter $ j $’s ranked preferences. **Constraints** 1. $ 1 \le T \le 100 $ 2. For each test case: - $ 1 \le C, V \le 100 $ - Each $ P_j $ is a permutation of $ \{1, 2, \dots, C\} $ **Objective** Simulate a two-round voting system: - **Round 1**: Each voter votes for their most preferred candidate (i.e., $ p_{j,1} $). Let $ f(c) $ be the number of first-choice votes for candidate $ c $. - If $ \exists c^* $ such that $ f(c^*) > \frac{V}{2} $, then $ c^* $ wins in **round 1**. - **Round 2**: If no candidate has a majority in Round 1, eliminate all candidates except the top two by first-choice vote count. Each voter then votes for the candidate among the two remaining who appears earliest in their preference list. The candidate with the most votes in this round wins in **round 2**. Output: $ (w, r) $, where $ w \in \{1, \dots, C\} $ is the winning candidate and $ r \in \{1, 2\} $ is the round of victory.
API Response (JSON)
{
  "problem": {
    "name": "A. The New President",
    "description": {
      "content": "Finally, it is time to vote for a new president and you are really excited about that. You know that the final results may take weeks to be announced, while you can't really wait to see the results. ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10015A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Finally, it is time to vote for a new president and you are really excited about that. You know that the final results may take weeks to be announced, while you can't really wait to see the results.\n\n...",
      "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:  \n- Let $ C \\in \\mathbb{Z} $ be the number of candidates, $ V \\in \\mathbb{Z} $ the number of voters.  \n- L...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments