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
| Inputcopy | Outputcopy |
|---|---|
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;
}
这是一个关于算法优化的问题,主要涉及网格排列的优化策略。题目中给出一个由整数构成的网格,允许无限次在同一行内交换数字,目标是最大化网格的美感,即相同数值相邻的对数。解决方案可以采用贪心和记忆化搜索的方法,通过逐行比较并更新计数来达到最优状态。博客提供了两种不同的实现方式,一种使用了map进行记忆化,另一种采用了二分查找。
1121

被折叠的 条评论
为什么被折叠?



