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 3
Output:
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通过您的时间复杂度是最佳的。 我们需要优化的几件事是:

  1. 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文章)

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

    循环优化:重构代码,以确保没有多余的循环

  3. 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 int
int main()
{
// fast io
ios_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 N
cin >> n;
int** arr = (int**)(malloc(sizeof(int*) * n));
//to calculate trace
lli sum = 0;
//to calculate duplicate rows
int 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 matrix
if (i == j)
sum += arr[i][j];
//to check whether duplicate exists in this column
if (flag == false && mymap.find(arr[i][j]) != mymap.end()) {
row++;
flag = true;
}
if (flag == false)
mymap[arr[i][j]]++;
}
}
// to calculate duplicate columns
int 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 column
if (flag == false && mymap.find(arr[j][i]) != mymap.end()) {
col++;
flag = true;
// once duplicate found simply break
break;
}
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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/380531.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

装修(43天,安装新家具啦)

装修(43天,安装新家具啦) 昨天保洁公司的来做了保洁.说好是9点钟让我去开门的,可他们却10点才到,原来听错地点了.三个人,从早上10开始做,到下午六点我去的时候,还没有做完,大概要做到七点.才150元钱,也真难为他们,还有一部分是公司的.效果觉得还是一般,有些地方还是不干净.可能…

dbms和sql_DBMS | 并发控制和各种并发控制方法

dbms和sql并发控制 (Concurrency Control) The concurrency control is the coordination of the simultaneous execution of a transaction in a multiuser database system. The concurrency control can ensure the serializability of the transaction in a multiuser data…

修改窗口图标 AfxRegisterWndClass()

LPCTSTR AFXAPI AfxRegisterWndClass(UINT nClassStyle,HCURSOR hCursor 0,HBRUSH hbrBackground 0,HICON hIcon 0 ); 可以简单修改一个窗口的注册类&#xff0c;当然也可以用SetWindowsLong()来实现。转载于:https://www.cnblogs.com/magic-cube/archive/2011/05/05/2038…

linux停止ssh服务的命令,开启、关闭、查看SSH服务

一、临时启用SSH服务1、通过SSH服务器的启动脚本文件启动SSH服务通过OpenSSH服务器的脚本文件“/etc/rc.d/init.d/sshd”启动SSH服务&#xff0c;命令执行如下。/etc/rc.d/init.d/sshd start命令执行后&#xff0c; SSH服务开始运行。2、使用Linux下的service命令启动SSH服务使…

XCHE命令

功能&#xff1a; 交换数据&#xff0c;交换两个操作数内容 形式&#xff1a; xche reg,reg xche reg,mem xche mem,regreg&#xff1a;寄存器&#xff1b;mem&#xff1a;内存单元 比如&#xff1a; xche eax&#xff0c;ebx;交换eax和ebx的内容 xche eax,[ebx]

软件测试经典网站(转)

软件测试经典网站网址简介http://bdonline.sqe.com/一个关于网站测试方面的网页,对这方面感兴趣的人可以参考http://citeseer.nj.nec.com/一个丰富的电子书库,内容很多,而且提供著作的相关文档参考和下载,是作者非常推荐的一个资料参考网站http://groups.yahoo.com/group/LoadR…

ai人工智能在手机的应用_强化学习在人工智能中的应用

ai人工智能在手机的应用The reinforcement learning is being used in many Intelligent Systems and the developers are seeing a great scope in it for current and future developments in the field of computers and robotics. This is because, the Reinforcement Lear…

LEA指令

功能&#xff1a;取偏移地址 格式&#xff1a; LEA reg&#xff0c;memreg&#xff1a;寄存器&#xff1b;mem&#xff1a;内存单元 例如&#xff1a; LEA AX,[1000H]将1000源操作数[1000H]的偏移地址1000H送到AX中。 理解的时候可将[]去掉&#xff0c;等同于&#xff1a; m…

参考站点

26个杰出的jQuery幻灯片插件http://woshao.com/article/6807a76a43d611e081e1000c2959fd2a/周公的专栏http://blog.csdn.net/zhoufoxcn/ W3SCHOOL在线教程http://www.w3school.com.cn/jQuery API 中文版http://www.css88.com/jqueryapi在ASP.NET中使用Highcharts js图表http://…

linux多进程通过中断实现,Linux驱动中断上下文中会发生什么结果实验测试

一、前言每一个Linux驱动工程师都知道这样一个准则&#xff1a;在中断上下文中不能睡眠。但是为什么interrupt context中不能调用导致睡眠的kernel API呢&#xff1f;如果驱动这么做会导致什么样的后果呢&#xff1f;这就是本文探讨的主题。为了理解这个主题&#xff0c;我们设…

'unsigned char'-C编程中的声明,赋值和用法

char is a data type in C programming language which can store value from -128 to 127. It generally used to store character values. char是C编程语言中的数据类型&#xff0c;可以存储从-128到127的值 。 它通常用于存储字符值。 unsigned is a qualifier which is us…

cmp指令

功能&#xff1a;比较 格式&#xff1a; CMP destination,sourceCMP 指令比较整数。字符代码也是整数&#xff0c;因此可以用 CMP 指令。 如果比较的是两个无符号数&#xff0c;则零标志位和进位标志位表示的两个操作数之间的关系 如果比较的是两个有符号数&#xff0c;则符…

游戏后的迷茫

9月份花了很多时间耐下心的玩了一个游戏——三国志11&#xff0c;这个是我继三国5以后耐下心玩得最多的一个游戏了&#xff0c;也是2年来耐下心玩得最多的游戏。现在是不是真的太浮躁了。连玩游戏都耐不下心。每天的泡论坛&#xff0c;看电影&#xff0c;下载&#xff0c;刻录的…

linux下获取时间的函数

相关函数 time&#xff0c;ctime&#xff0c;gmtime&#xff0c;localtime //--------------------------------------------------------------------------------------------------------------------------------------------------------// asctime&#xff08;将时间和日…

linux 内核配置 dns,linux bind dns简单配置

操作系统版本&#xff1a;[roottest ~]# cat /etc/issueRed Hat Enterprise Linux AS release 4 (Nahant Update 4)Kernel r on an m内核&#xff1a;[roottest ~]# uname -aLinux test 2.6.9-42.EL #1 Wed Jul 12 23:16:43 EDT 2006 i686 i686 i386 GNU/Linux[roottest ~]#需要…

鸡兔同笼问题

已知鸡兔的总数量为n&#xff0c;总腿数为m。输入n和m&#xff0c;依次输出鸡的数目和兔的数目。如果无解&#xff0c;则输出No answer。 样例输入&#xff1a;①14 32 ② 10 16 样例输出&#xff1a;①12 2 ②No answer 【分析】 设鸡有a只&#xff0c;兔有b只&#xff0c…

两个人 三声叹 一钵泪

寂寞的人,流下的泪珠是单数的 转载于:https://www.cnblogs.com/aque1984/archive/2006/10/02/520282.html

散列碰撞_散列中的碰撞和碰撞解决技术

散列碰撞Prerequisite: Hashing data structure 先决条件&#xff1a; 哈希数据结构 碰撞 (Collisions) Hash functions are there to map different keys to unique locations (index in the hash table), and any hash function which is able to do so is known as the per…

call和ret(f)指令

call指令 CPU执行call指令时&#xff0c;进行两步操作 将当前的ip&#xff08;eip&#xff09;或者cs和ip&#xff08;ecs和eip&#xff09;压入栈中跳转到标号处 call 标号 将当前的IP压栈后&#xff0c;转到标号处执行指令 相当于&#xff1a; push ip jmp near ptr 标号…

天翼云linux远程密码不对,天翼云主机远程连接

天翼云主机如何进行远程连接&#xff1f;操作系统不同自然连接方式也是不一样&#xff0c;今天我们分别介绍一下天翼云Windows主机远程连接和Linux云主机远程连接的方法。一、Windows云主机远程连接(1)在电脑连接网络的情况下&#xff0c;点击桌面左下角的windows图标&#xff…