2019 JUST Programming Contest J. Grid Beauty
3.0 s
256 MB
standard input
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 ).
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).
For each test case, print a single line containing the beauty of the grid.
2
2 3
1 2 3
4 1 2
3 3
5 7 9
3 2 9
5 3 2
2
3
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的更多相关文章
- 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 ...
- 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 ...
- 2017-2018 ACM-ICPC Latin American Regional Programming Contest J - Jumping frog 题解(gcd)
题目链接 题目大意 一只青蛙在长度为N的字符串上跳跃,"R"可以跳上去,"P"不可以跳上去. 字符串是环形的,N-1和0相连. 青蛙的跳跃距离K的取值范围是[1 ...
- 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 ...
- AtCoder diverta 2019 Programming Contest 2
AtCoder diverta 2019 Programming Contest 2 看起来我也不知道是一个啥比赛. 然后就写写题解QWQ. A - Ball Distribution 有\(n\)个 ...
- 【AtCoder】diverta 2019 Programming Contest 2
diverta 2019 Programming Contest 2 A - Ball Distribution 特判一下一个人的,否则是\(N - (K - 1) - 1\) #include &l ...
- 【AtCoder】diverta 2019 Programming Contest
diverta 2019 Programming Contest 因为评测机的缘故--它unrated了.. A - Consecutive Integers #include <bits/st ...
- [AtCoder] Yahoo Programming Contest 2019
[AtCoder] Yahoo Programming Contest 2019 很遗憾错过了一场 AtCoder .听说这场是涨分场呢,于是特意来补一下题. A - Anti-Adjacency ...
- 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 ...
随机推荐
- 蓝桥杯练习系统算法训练习题加答案java版本
附上百度文库的链接:http://wenku.baidu.com/view/afb78d36b42acfc789eb172ded630b1c59ee9bf7
- Android 4.x 获取存储卡路径的方式
http://blog.sina.com.cn/s/blog_8daaa9ea0101mx7f.html 以前的Android(4.1之前的版本)中,SDcard跟路径通过“/sdcard”或 ...
- GetHashCode()
[GetHashCode] GetHashCode 方法的默认实现不保证针对不同的对象返回唯一值.而且,.NET Framework 不保证 GetHashCode 方法的默认实现以及它所返回的值在不 ...
- Robocopy和xxcopy全掌握
Windows提供的复制操作功能实在是太过简陋,可定制性又不强,在复制.移动.备份文件夹的时候,总要循环往复做多次操作.现在我们就向你介绍两款强力复制备份软件:Robocopy和XXCOPY,具体功能 ...
- Python基础:面向对象基础 (一) 类及其属性和魔法方法
定义类,添加和获取对象属性 # 定义类 格式如下 # class 类名: # 方法列表 # 新式类定义形式 # info 是一个实例方法,第一个参数一般是self,表示实例对象本身 class Her ...
- 01 lucene基础 北风网项目培训 Lucene实践课程 索引
在创建索引的过程中IndexWriter会创建多个对应的Segment,这个Segment就是对应一个实体的索引段.随着索引的创建,Segment会慢慢的变大.为了提高索引的效率,IndexWrite ...
- linux 安装网络监控插件indicator-sysmonitor
1.添加源 sudo add-apt-repository ppa:fossfreedom/indicator-sysmonitor 2.更新源 sudo apt-get update 3.安装 su ...
- eclipse基础环境配置和svn、maven的配置以及maven的安装
安装eclipse和基础配置 第一步:解压eclipse安装包,直接解压就可以,绿色版安装 第二步:启动eclipse,注意这里的eclipse需要依赖jdk,并且版本需要匹配,否则启动会出 现问题. ...
- Hibernate入门级实例
一.开发环境 Win8 + jdk1.7 + MyEclipse + Tomcat5.0 + MySQL 说明:其实Hibernate是非常独立的框架,根本不需要MyEclipse,Eclipse,T ...
- sql2008 安装提示重启失败
[转] https://www.cnblogs.com/chenshaogang/p/4313022.html