J - Grid Beauty

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,\,ji,j) (1 < i \le n,\, 1 \le j \le m1<i≤n,1≤j≤m) such that g_{i,j}gi,j​ is equal to g_{i-1,j}gi−1,j​ (i.e. g_{i,j} \equiv g_{i - 1,j}gi,j​≡gi−1,j​).

Input

The first line contains an integer TT (1 \le T \le 51≤T≤5) specifying the number of test cases.

The first line of each test case contains two integers nn and mm (1 \le n, m \le 10^31≤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 11 and 10^8108 (inclusive).

Output

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

Sample 1

InputcopyOutputcopy
2
2 3
1 2 3
4 1 2
3 3
5 7 9
3 2 9
5 3 2
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.

Sponsor

思路:直接想到贪心+map记忆化,两行两行的比较就完了

/*Where there is light, in my heart.*/
/*SUMMER_TRAINING DAY 16*/
#include<bits/stdc++.h>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
//
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define INF 0x3f3f3f
#define ll long long
//#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
#define unmap(a,b) unordered_map<a,b>
#define unset(a) unordered_set<a>
#define F first
#define S second·
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define _rep(i, a, b) for (int i = (a); i >= (b); --i)
#define mode 1e4+7
#define pi acos(-1)
typedef double db;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> vi;
const int N=1e4+5;
//
signed main(){
	IOS;
	int t;
	cin>>t;
	int n,m;
	while(t--){
		cin>>n>>m;
		int num;
		int sum=0;
		int res=0;
		map<int,int> mp1,mp2;
		for(int i=0;i<m;i++){
			cin>>num;
			mp1[num]++;
		}
		for(int i=1;i<n;i++){
			for(int j=0;j<m;j++){
				cin>>num;
				if(!res) mp2[num]++;
				else mp1[num]++;
				if(!res){
					if(mp1.count(num)&&mp1[num]!=0) sum++,mp1[num]--;
				}
				else{
					if(mp2.count(num)&&mp2[num]!=0) sum++,mp2[num]--;
				}
			}
			if(!res){
				res=1;
				mp1.clear();
			}
			else{
				res=0;
				mp2.clear();
			}
		}
		cout<<sum<<endl;
	}
}
//made by shun 20220719

这里引用一下文豪二分的版本,很妙

/*莫道桑榆晚,为霞尚满天*/

#include<bits/stdc++.h>

using namespace std;
const int N =1e3+5;
#define mem memset(a,b,sizeof(a))
typedef long long ll;
typedef pair<int,int> PII;
ll bsearch_1(ll a[],ll x,ll l, ll r)
{
    while (l < r)
    {
        ll mid = (l + r )>> 1;
        if (a[mid]>=x) r = mid;
        else l = mid + 1;
    }
    return l;
}
ll a[N][N];
int main()
{
    ll u;
    scanf("%lld",&u);
    while(u--)
    {
        ll n,m;
        scanf("%lld %lld",&n,&m);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++)
            a[i][j]=0;
        }
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<m;j++)
            {
                scanf("%lld",&a[i][j]);
            }
            sort(a[i],a[i]+m);
        }
        ll k=0;
        for(ll i=1;i<n;i++)
        {
            for(ll j=0;j<m;j++)
            {
                ll t=a[i][j];
                ll l=bsearch_1(a[i-1],t,0,m-1);
                if(a[i-1][l]==t)
                {
                    a[i-1][l]=-1;
                    k++;
                }
            }
        }
        printf("%lld\n",k);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值