Submission #912837

# Submission time Handle Problem Language Result Execution time Memory
912837 2024-01-20T02:41:29 Z Muhammad_Aneeq Ideal city (IOI12_city) C++17
44 / 100
1000 ms 13904 KB
#include <cmath>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
int const MAXN=1e5+10;
vector<int>nei[MAXN]={};
int mod=1e9;
int ans=0;
vector<int>dx={0,0,-1,1},dy={-1,1,0,0};
int dis[MAXN]={};
void bfs(int x)
{
	priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>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<int,vector<int>>d;
	map<pair<int,int>,int>ex;
	for (int 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);
		int ans=0;
		long long szx=0,sux=0,szy=0,suy=0;
		for (int 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)
	{
		int x=i.first.first,y=i.first.second;
		for (int 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 (int i=0;i<N;i++)
		dis[i]=1e9+10;
	for (int i=0;i<N;i++)
	{
		bfs(i);
		for (int j=i+1;j<N;j++)
			ans=(ans+dis[j])%mod;
		for (int j=0;j<N;j++)
			dis[j]=1e9+10;
	}
	return ans;
}

Compilation message

city.cpp: In function 'int DistanceSum(int, int*, int*)':
city.cpp:48:22: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'} [-Wsign-compare]
   48 |   if (i.second.size()!=i.second.back()-i.second[0]+1)
      |       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3016 KB Output is correct
2 Correct 1 ms 3012 KB Output is correct
3 Incorrect 2 ms 2908 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 59 ms 2908 KB Output is correct
2 Correct 55 ms 3124 KB Output is correct
3 Correct 133 ms 3164 KB Output is correct
4 Correct 125 ms 3188 KB Output is correct
5 Correct 255 ms 3412 KB Output is correct
6 Correct 220 ms 3164 KB Output is correct
7 Correct 205 ms 3164 KB Output is correct
8 Correct 223 ms 3252 KB Output is correct
9 Correct 270 ms 3244 KB Output is correct
10 Correct 265 ms 3248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 4696 KB Output is correct
2 Correct 12 ms 4696 KB Output is correct
3 Correct 29 ms 7304 KB Output is correct
4 Correct 30 ms 7380 KB Output is correct
5 Correct 59 ms 11628 KB Output is correct
6 Correct 63 ms 11448 KB Output is correct
7 Correct 64 ms 11796 KB Output is correct
8 Correct 75 ms 11328 KB Output is correct
9 Correct 73 ms 11480 KB Output is correct
10 Correct 74 ms 13904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1066 ms 5468 KB Time limit exceeded
2 Halted 0 ms 0 KB -