日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Vestigium-Google CodeJam 2020资格回合问题1解决方案

發布時間:2023/12/1 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Vestigium-Google CodeJam 2020资格回合问题1解决方案 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem statement:

問題陳述:

Vestigium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.

Vestigium在拉丁語中表示“痕跡”。 在此問題中,我們使用拉丁方和矩陣跡線。

The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).

方陣的軌跡是主對角線(從左上角到右下角)上的值之和。

An N-by-N square matrix is a Latin square if each cell contains one of N different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N values are the integers between 1 and N.

如果每個單元格包含N個不同值之一,并且在行或列中沒有重復值,則N × N方陣是拉丁方陣。 在這個問題中,我們將只處理“自然拉丁方”,其中N值為1到N之間的整數。

Given a matrix that contains only integers between 1 and N, we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.

給定一個僅包含1到N之間的整數的矩陣,我們想計算其軌跡并檢查它是否為自然的拉丁方。 為了提供一些其他信息,而不是簡單地告訴我們矩陣是否是自然的拉丁正方形,請計算包含重復值的行數和列數。

Input:

輸入:

The first line of the input gives the number of test cases, T. T test cases follow. Each starts with a line containing a single integer N: the size of the matrix to explore. Then, N lines follow. The i-th of these lines contains N integers Mi,1 ,Mi,2 ...,Mi,N. Mi,j is the integer in the i-th row and j-th column of the matrix.

輸入的第一行給出測試用例的數量T。 隨后是T測試用例。 每個都以包含單個整數N的行開始: N :要探索的矩陣的大小。 然后,跟隨N行。 這些行中的第i個包含N個整數M i,1 ,M i,2 ...,M i,N 。 M i,j是矩陣的第i行和第j列中的整數。

Output:

輸出:

For each test case, output one line containing Case #x: k r c, where x is the test case number (starting from 1), k is the trace of the matrix, r is the number of rows of the matrix that contain repeated elements, and c is the number of columns of the matrix that contain repeated elements.

對于每個測試用例,輸出包含Case# x的一行:krc ,其中x是測試用例編號(從1開始), k是矩陣的跡線, r是包含重復元素的矩陣的行數, c是包含重復元素的矩陣的列數。

Constraints:

限制條件:

1 ≤ T ≤ 100. 2 ≤ N ≤ 100. 1 ≤ Mi,j ≤ N,for all i,j.

Example:

例:

Input: 3 #no of test cases 4 # value of N 1 2 3 4 # ith row 2 1 4 3 3 4 1 2 4 3 2 1 4 2 2 2 2 2 3 2 3 2 2 2 3 2 2 2 2 3 2 1 3 1 3 2 1 2 3Output: Case #1: 4 0 0 Case #2: 9 4 4 Case #3: 8 0 2

Explanation:

說明:

In Example #1, the input is a natural Latin square, which means no row or column has repeated elements. All four values in the main diagonal are 1, and so the trace (their sum) is 4.

在示例1中,輸入是自然的拉丁方格,這意味著行或列中沒有重復的元素。 主對角線上的所有四個值均為1,因此跡線(它們的總和)為4。

In Example #2, all rows and columns have repeated elements. Notice that each row or column with repeated elements is counted only once regardless of the number of elements that are repeated or how often they are repeated within the row or column. In addition, notice that some integers in the range 1 through N may be absent from the input.

在示例2中,所有行和列都有重復的元素。 注意,具有重復元素的每一行或每一列僅計數一次,而不管重復元素的數量或在行或列中重復元素的頻率。 另外,請注意,輸入中可能缺少1到N范圍內的某些整數。

In Example #3, the leftmost and rightmost columns have repeated elements.

在示例#3中,最左邊和最右邊的列具有重復的元素。

Source: Qualification Round 2020 - Code Jam 2020 - Vestigium

資料來源: 2020年資格巡回賽-2020年《果醬大戰》-Vestigium

Solution

Before discussing the solution, I want to mention a few points here on Google Codejam. Codejam is similar to competitive programming. Here time complexity is not only the measure. Say your code has a time complexity of O(n) but the loop runs twice and the second one is redundant. This may lead to TLE through your time complexity is optimal. So few things we need to optimize is:

在討論解決方案之前,我想在Google Codejam上提幾點。 Codejam與競爭性編程類似。 在這里,時間復雜度不僅是度量。 假設您的代碼的時間復雜度為O(n),但是循環運行兩次,第二個循環是多余的。 這可能導致TLE通過您的時間復雜度是最佳的。 我們需要優化的幾件事是:

  • Fast i/o- use fast i/o to reduce the time (refer to fast i/o article of our website)

    快速I / O-使用快速I / O以減少時間(請參閱我們網站上的快速I / O文章)

  • Loop optimization: Refactor your code so that there are no redundant loops

    循環優化:重構代碼,以確保沒有多余的循環

  • Avoid functions as this may lead to more time taken (using the function is always recommended to write production-level code, but competitive programming is different)

    避免使用函數,因為這可能會花費更多時間(始終建議使用該函數編寫生產級代碼,但競爭性編程會有所不同)

  • So here I haven't used any function and have used fast i/o. {Please refer to my code for details.

    所以在這里我沒有使用任何功能并且使用了快速的I / O。 {請參閱我的代碼以獲取詳細信息。

    Now back to the solution.

    現在回到解決方案。

    1) Computing trace

    1)計算軌跡

    Diagonal means a[i][i]

    對角表示a [i] [i]

    Trace of a matrix means sum of the diagonal which is sum(a[i][i])

    矩陣的軌跡表示對角線之和 ,即sum(a [i] [i])

    I have computed the trace while taking input as part of loop optimization

    在將輸入作為循環優化的一部分時,我已經計算了跟蹤

    While taking the input while row==column, I have added the input to my cumulative sum and after input taking is done, I have my trace value ready too.

    在row == column的同時獲取輸入時,我已將輸入添加到累積總和中,完成輸入獲取后,我也準備好跟蹤值。

    2) Checking number of rows having duplicate entry

    2)檢查重復條目的行數

    I have done this too while taking input. While taking input for each row, I have kept a map to check whether the input is already present there or not. If present, mark that row as duplicate and we are done for that row, for further inputs we will not check anymore to reduce time. This can be done using a flag variable.

    我在接受輸入時也這樣做了。 在獲取每一行的輸入時,我保留了一張地圖以檢查輸入是否已經存在。 如果存在,則將該行標記為重復行,然后完成該行的操作,對于其他輸入,我們將不再進行檢查以減少時間。 這可以使用標志變量來完成。

    So, By the time I took my input, I have two things ready. One is trace and another is the count of duplicate rows.

    因此,當我接受輸入時,我已經做好了兩件事。 一個是跟蹤,另一個是重復行的計數。

    3) Checking number of rows having duplicate entry

    3)檢查重復條目的行數

    For this thing, I was unable to compute while taking the input. As I was taking input row-wise, the column was not ready for me until my last row input. I could have stored additionally each column but that would be not so efficient considering. To compute this using another loop similar I did for row duplicate checking. For each column, we will use a map and check if any duplicate value has arrived r not. If any time, we find a duplicate, just mark that column as duplicate as proceed to the next column.

    為此,我無法在輸入時進行計算。 當我逐行進行輸入時,直到我最后一行輸入時,該列才為我準備好。 我本可以另外存儲每列,但是考慮起來并不是那么有效。 為了使用另一個循環來計算它,我做了行重復檢查。 對于每一列,我們將使用一個映射并檢查是否有任何重復的值到達或沒有到達。 如果有時間,我們會找到重復的記錄,只需將該列標記為重復,就可以繼續進行下一列。

    C++ Implementation:

    C ++實現:

    #include <bits/stdc++.h> using namespace std;#define lli long long intint main() {// fast ioios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);int t;cin >> t;for (int test_case = 1; test_case <= t; test_case++) {int n;//input Ncin >> n;int** arr = (int**)(malloc(sizeof(int*) * n));//to calculate tracelli sum = 0;//to calculate duplicate rowsint row = 0;for (int i = 0; i < n; i++) {arr[i] = (int*)(malloc(sizeof(int) * n));map<int, int> mymap;bool flag = false;for (int j = 0; j < n; j++) {cin >> arr[i][j];//calculating trace of the matrixif (i == j)sum += arr[i][j];//to check whether duplicate exists in this columnif (flag == false && mymap.find(arr[i][j]) != mymap.end()) {row++;flag = true;}if (flag == false)mymap[arr[i][j]]++;}}// to calculate duplicate columnsint col = 0;for (int i = 0; i < n; i++) {map<int, int> mymap;bool flag = false;for (int j = 0; j < n; j++) {// to check whether duplicate exists in this columnif (flag == false && mymap.find(arr[j][i]) != mymap.end()) {col++;flag = true;// once duplicate found simply breakbreak;}if (flag == false)mymap[arr[j][i]]++;}}cout << "Case #" << test_case << ": " << sum << " " << row << " " << col << endl;}return 0; }

    Output:

    輸出:

    3 4 1 2 3 4 2 1 4 3 3 4 1 2 4 3 2 1 Case #1: 4 0 0 4 2 2 2 2 2 3 2 3 2 2 2 3 2 2 2 2 Case #2: 9 4 4 3 2 1 3 1 3 2 1 2 3 Case #3: 8 0 2

    翻譯自: https://www.includehelp.com/icp/vestigium-google-codejam-2020-qualification-round-problem1-solution.aspx

    總結

    以上是生活随笔為你收集整理的Vestigium-Google CodeJam 2020资格回合问题1解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。