QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#768441 | #7878. Matrix Distances | liliwa | RE | 1ms | 3640kb | C++23 | 1.9kb | 2024-11-21 10:42:05 | 2024-11-21 10:42:06 |
Judging History
answer
#include<bits/stdc++.h>
#define S second
#define F first
using namespace std;
using ll=long long;
// using ll=long double;
using pii=pair<ll,ll>;
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
const int N = 1010;
int c[N][N];
ll f[N],g[N];
bool cmp1(pii a, pii b)
{
return a.F<b.F;
}
bool cmp2(pii a, pii b)
{
return a.S<b.S;
}
void solve(){
int n,m;
cin>>n>>m;
unordered_map<int,vector<pii>> mp;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>c[i][j];
mp[c[i][j]].push_back({i,j});
}
}
ll ans = 0;
for(auto& [x,v]:mp)
{
sort(v.begin(), v.end(), cmp1);
ll sum = v[0].F;
ll num = 1;
f[0] = 0;
for(int i=1;i<v.size();i++)
{
f[i] = v[i].F*num - sum;
num++;
sum += v[i].F;
}
sum = v.back().F;
num = 1;
g[v.size()-1] = 0;
for(int i=(int)v.size()-2;i>=0;i--)
{
g[i] = sum - v[i].F*num ;
num++;
sum += v[i].F;
}
for(int i=0;i<v.size();i++)
{
ans += g[i] + f[i];
}
sort(v.begin(), v.end(), cmp2);
sum = v[0].S;
num = 1;
f[0] = 0;
for(int i=1;i<v.size();i++)
{
f[i] = v[i].S*num - sum;
num++;
sum += v[i].S;
}
sum = v.back().S;
num = 1;
g[v.size()-1] = 0;
for(int i=(int)v.size()-2;i>=0;i--)
{
g[i] = sum - v[i].S*num ;
num++;
sum += v[i].S;
}
for(int i=0;i<v.size();i++)
{
ans += g[i] + f[i];
}
}
cout<<ans;
}
int main(){
IOS;
int t=1;
// cin>>t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3568kb
input:
2 2 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
4 4 1 3 2 4 2 1 2 3 1 3 3 2 3 2 1 4
output:
152
result:
ok 1 number(s): "152"
Test #3:
score: -100
Runtime Error
input:
1000 1000 227980299 227980299 227980299 227980299 227980299 776958596 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 776958596 227980299 227980299 329001637 227980299 227980299 227980299 329001637 227980299 227980299 227980299 227980299 227980299 ...