J. Grid Beauty
time limit per test

3.0 s

memory limit per test

256 MB

input

standard input

output

standard output

You are given a grid gg consisting of nn rows each of which is divided into mm 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,j ) (1<i≤n,1≤j≤m ) such that gi,jgi,j is equal to gi−1,j (i.e. gi,j≡gi−1 ).

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 nn and mm (1≤n,m≤103 ), giving the number of rows and columns in the grid, respectively.

Then nn lines follow, each line contains mm integers, giving the grid. All values in the grid are between 1 and 108 (inclusive).

Output

For each test case, print a single line containing the beauty of the grid.

Example
Input

 
2
2 3
1 2 3
4 1 2
3 3
5 7 9
3 2 9
5 3 2
Output

 
2
3
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.

解题思路:这道题就是给说如果a[i][j] = a[i-1][j]则是美丽的,问你说最多有几个;

这道题我是卡着时间过的,用map超时了,用unorder_map变快了,可能有其他人有很多更好的做法,下面是我的做法;

 #include<iostream>
#include<stdio.h>
#include<unordered_map> //unordered_map的头文件;
#include<algorithm>
using namespace std; int n ;
int row , col;
int a[][];
unordered_map<int,int>flag; //用来记录当前行出现了数字出现多少个;
unordered_map<int,int>flag1; //用来记录下一行的数字出现多少个;
int ans = ; int main()
{
scanf("%d",&n);
while(n--)
{
ans = ;
scanf("%d%d",&row,&col);
for(int i = ;i < row ;i++)
{
for(int j = ; j < col ;j++)
{
scanf("%d",&a[i][j]);
} }
for(int i = ; i < row- ;i++ )
{
for(int j = ; j < col ;j++)
{ flag[a[i][j]]++; //统计每个数字出现的个数;
flag1[a[i+][j]]++; //统计下一行每个数字出现的个数;
}
for(int j = ; j < col ; j++)
{ if(flag1[a[i+][j]]>) //如果这一行的数字在下一行出现过;
{
ans += min(flag[a[i+][j]],flag1[a[i+][j]]);
//不断取这一行和下一行的较小者;
flag1[a[i+][j]] = ;
//并将统计过的数字个数重新置为0;
}
}
for(int j = ; j < col ;j++)
{
flag[a[i][j]] = ;
flag1[a[i+][j]] = ;
//初始化,方便后面的行的比较,比如比较了第一和第二行,现在比较第二和第三行,所以每个数字出现的个数要重新置为0;
}
}
printf("%d\n",ans);
}
return ;
}

2019 JUST Programming Contest J. Grid Beauty的更多相关文章

  1. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest J. Set

    Let's consider some math problems. JSZKC has a set A=A={1,2,...,N}. He defines a subset of A as 'Meo ...

  2. Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】

    J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...

  3. 2017-2018 ACM-ICPC Latin American Regional Programming Contest J - Jumping frog 题解(gcd)

    题目链接 题目大意 一只青蛙在长度为N的字符串上跳跃,"R"可以跳上去,"P"不可以跳上去. 字符串是环形的,N-1和0相连. 青蛙的跳跃距离K的取值范围是[1 ...

  4. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Convert QWERTY to Dvorak

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5502  The 12th Zhejiang Provincial ...

  5. AtCoder diverta 2019 Programming Contest 2

    AtCoder diverta 2019 Programming Contest 2 看起来我也不知道是一个啥比赛. 然后就写写题解QWQ. A - Ball Distribution 有\(n\)个 ...

  6. 【AtCoder】diverta 2019 Programming Contest 2

    diverta 2019 Programming Contest 2 A - Ball Distribution 特判一下一个人的,否则是\(N - (K - 1) - 1\) #include &l ...

  7. 【AtCoder】diverta 2019 Programming Contest

    diverta 2019 Programming Contest 因为评测机的缘故--它unrated了.. A - Consecutive Integers #include <bits/st ...

  8. [AtCoder] Yahoo Programming Contest 2019

    [AtCoder] Yahoo Programming Contest 2019   很遗憾错过了一场 AtCoder .听说这场是涨分场呢,于是特意来补一下题. A - Anti-Adjacency ...

  9. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Beauty of Array

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5496 The 12th Zhejiang Provincial ...

随机推荐

  1. axis2 webService开发指南(2)

    1  Axis2的简单WebService示例 1.1 新建一个web工程,创建一个类Greeting,用于当作webservice服务 代码如下: package amyservices; impo ...

  2. 前端开发之CSS篇四

    一.相对定位 二.绝对定位 三.固定定位 四.z-index 前言 定位有三种:1.相对定位 2.绝对定位 3.固定定位 这三种定位,每种都暗藏玄机,所以要每个单独剖析. 1️⃣   相对定位 1.三 ...

  3. C# ValueTypes

    [C# ValueTypes] 1.哪些类型是ValueType? The value types consist of two main categories: Structs Enumeratio ...

  4. Oracle中针对中文进行排序

    在oracle 9i之前,对中文的排序,是默认按2进制编码来进行排序的. 9i时增加了几种新的选择: 按中文拼音进行排序:SCHINESE_PINYIN_M 按中文部首进行排序:SCHINESE_RA ...

  5. sql解决主键冲突

    在数据插入的时候,假设主键对应的值已经存在,则插入失败!这就是主键冲突.当主键存在冲突(duplicate key)的时候,可以选择性的进行处理,即忽略.更新或者替换. 1.忽略 insert ign ...

  6. Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c

    队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...

  7. Windows 安装 Maven 及 Eclipse 安装Maven插件

    环境说明: window 8.1 64bit Eclipse Version: Luna Release (4.4.0) Maven 3.0.5 Windows Maven 安装: 1.确保安装了JD ...

  8. -other linker flags - 详解

    • 值:-objC,-all_load,-force_load

 • -objC: 在iOS 中,使用-all_load时,如果静态库中有类别时会出问题,使用其他两个值则不会有问题.

 • -al ...

  9. ubuntu14.04 64 位 vmware tools 问题2

    当提示说open-vm-tools版本太低时可以这样解决 0.使用最新版本12.5的vmware player. 1.sudo apt-get autoremove open-vm-dkms open ...

  10. 回顾2017系列篇(五):人工智能给UI/UX设计带来的影响

    如今,我们正处于设计新纪年的转折点上,用机器人和人工智能方面的专家说法表达即“The end is near(终点近了)”.但这并不意味着世界末日,但未来机器人将毫无疑问地接管一部分目前被人类占领的工 ...