답안 #912848

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
912848 2024-01-20T02:54:15 Z Muhammad_Aneeq 이상적인 도시 (IOI12_city) C++17
44 / 100
1000 ms 18120 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;
long long 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();
		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>,vector<int>>ex;
	for (int i=0;i<N;i++)
	{
		ex[{X[i],Y[i]}].push_back(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())
			{
				for (auto k:i.second)
					for (auto l:ex[{x+dx[j],y+dy[j]}])
						nei[k].push_back(l);
			}
		}
	}
	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:46: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]
   46 |   if (i.second.size()!=i.second.back()-i.second[0]+1)
      |       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 1 ms 2908 KB Output is correct
3 Incorrect 2 ms 2908 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 3164 KB Output is correct
2 Correct 54 ms 3168 KB Output is correct
3 Correct 137 ms 3164 KB Output is correct
4 Correct 140 ms 3160 KB Output is correct
5 Correct 256 ms 3336 KB Output is correct
6 Correct 220 ms 3164 KB Output is correct
7 Correct 211 ms 3420 KB Output is correct
8 Correct 218 ms 3320 KB Output is correct
9 Correct 274 ms 3340 KB Output is correct
10 Correct 266 ms 3336 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 5528 KB Output is correct
2 Correct 14 ms 5468 KB Output is correct
3 Correct 35 ms 9052 KB Output is correct
4 Correct 34 ms 9248 KB Output is correct
5 Correct 81 ms 15440 KB Output is correct
6 Correct 83 ms 15544 KB Output is correct
7 Correct 83 ms 15364 KB Output is correct
8 Correct 83 ms 15332 KB Output is correct
9 Correct 85 ms 15440 KB Output is correct
10 Correct 103 ms 18120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1010 ms 6224 KB Time limit exceeded
2 Halted 0 ms 0 KB -