#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
long long const MAXN=1e5+10;
vector<long long>nei[MAXN]={};
long long mod=1e9;
long long ans=0;
vector<long long>dx={0,0,-1,1},dy={-1,1,0,0};
long long dis[MAXN]={};
void bfs(long long x)
{
priority_queue<pair<long long,long long>,vector<pair<long long,long long>>,greater<pair<long long,long long>>>pq;
pq.push({0,x});
dis[x]=0;
while (pq.size())
{
auto y=pq.top();
pq.pop();
if (y.first>dis[y.second])
continue;
for (auto j:nei[y.second])
{
if (dis[j]>y.first+1)
{
dis[j]=y.first+1;
pq.push({dis[j],j});
}
}
}
}
int DistanceSum(int N, int X[], int Y[])
{
map<long long,vector<long long>>d;
map<pair<long long,long long>,long long>ex;
for (long long i=0;i<N;i++)
{
ex[{X[i],Y[i]}]=i;
d[X[i]].push_back(Y[i]);
}
bool subtask_3=1;
for (auto& i:d)
{
sort(begin(i.second),end(i.second));
if (i.second.size()!=i.second.back()-i.second[0]+1)
{
subtask_3=0;
break;
}
}
if (subtask_3)
{
sort(Y,Y+N);
sort(X,X+N);
long long ans=0;
long long szx=0,sux=0,szy=0,suy=0;
for (long long i=0;i<N;i++)
{
long long g=1ll*Y[i]*szy-suy;
g%=mod;
ans=(ans+g)%mod;
g=1ll*X[i]*szx-sux;
g%=mod;
ans=(ans+g)%mod;
szy++;
suy+=Y[i];
szx++;
sux+=X[i];
}
return ans;
}
for (auto i:ex)
{
long long x=i.first.first,y=i.first.second;
for (long long j=0;j<4;j++)
if (ex.find({x+dx[j],y+dy[j]})!=ex.end())
nei[i.second].push_back(ex[{x+dx[j],y+dy[j]}]);
}
for (long long i=0;i<N;i++)
dis[i]=1e10+10;
for (long long i=0;i<N;i++)
{
bfs(i);
for (long long j=i+1;j<N;j++)
{
dis[j]%=mod;
ans=(ans+dis[j])%mod;
}
for (long long j=0;j<N;j++)
dis[j]=1e10+10;
}
return ans;
}
Compilation message
city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:49:22: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} [-Wsign-compare]
49 | if (i.second.size()!=i.second.back()-i.second[0]+1)
| ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3164 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3164 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
3420 KB |
Output is correct |
2 |
Correct |
60 ms |
3420 KB |
Output is correct |
3 |
Correct |
146 ms |
3600 KB |
Output is correct |
4 |
Correct |
135 ms |
3604 KB |
Output is correct |
5 |
Correct |
271 ms |
3676 KB |
Output is correct |
6 |
Correct |
234 ms |
3684 KB |
Output is correct |
7 |
Correct |
222 ms |
3672 KB |
Output is correct |
8 |
Correct |
241 ms |
3672 KB |
Output is correct |
9 |
Correct |
296 ms |
3672 KB |
Output is correct |
10 |
Correct |
304 ms |
3872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4956 KB |
Output is correct |
2 |
Correct |
13 ms |
5224 KB |
Output is correct |
3 |
Correct |
40 ms |
7584 KB |
Output is correct |
4 |
Correct |
31 ms |
7508 KB |
Output is correct |
5 |
Correct |
67 ms |
11860 KB |
Output is correct |
6 |
Correct |
67 ms |
11892 KB |
Output is correct |
7 |
Correct |
76 ms |
11820 KB |
Output is correct |
8 |
Correct |
73 ms |
11740 KB |
Output is correct |
9 |
Correct |
66 ms |
11824 KB |
Output is correct |
10 |
Correct |
97 ms |
14820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1046 ms |
5976 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |