제출 #593681

#제출 시각아이디문제언어결과실행 시간메모리
593681Deepesson도시들 (IOI15_towns)C++17
61 / 100
20 ms1108 KiB
#include <bits/stdc++.h> #define MAX 115 int getDistance(int i, int j); int hubDistance(int N, int sub); typedef std::pair<int,int> pii; std::map<pii,int> memoria; int count=0; int smartgetDistance(int a, int b){ if(a==b)return 0; if(a>b)std::swap(a,b); pii x = {a,b}; if(memoria.find(x)!=memoria.end())return memoria[x]; int d = getDistance(a,b); memoria[x]=d; ++count; return d; } int dist[MAX][2]; pii MaisDistante(int N,int x,int p){ int best=-1,alvo=0; for(int i=0;i!=N;++i){ if(i==x)continue; int d = smartgetDistance(i,x); if(d>best){ best=d; alvo=i; } dist[i][p]=d; } return {best,alvo}; } int dist_centro[MAX]; ///Queries usadas: 3N-3 bool mesma_subarvore(int a,int b){ if(a==b)return 1; return smartgetDistance(a,b)<(dist_centro[a]+dist_centro[b]); } int hubDistance(int N, int sub) { count=0; memoria.clear(); pii alpha = MaisDistante(N,0,1); pii beta = MaisDistante(N,alpha.second,0); MaisDistante(N,beta.second,1); int d = beta.first; int ans=1e9; int exp_a=0,exp_b=0; std::map<pii,bool> hubs; for(int i=0;i!=N;++i){ if(i==alpha.second||i==beta.second)continue; int a = dist[i][0]; int b = dist[i][1]; int soma = a+b; int subt = soma-d; int base = subt/2; int custo = std::max(a,b)-base; exp_a = a-base; exp_b = b-base; if(custo<ans){///Fora da sub de A ou B ans=custo; hubs.clear(); hubs[{exp_a,exp_b}]=true; }else if(custo==ans)hubs[{exp_a,exp_b}]=true; dist_centro[i]=base; } if(sub<=2)return ans; int c=0; assert(hubs.size()<=2); for(auto&z:hubs){ exp_a = z.first.first; exp_b = z.first.second; std::vector<int> fora; int grupos[2]={1,1}; for(int i=0;i!=N;++i){ if(i==alpha.second||i==beta.second)continue; int a = dist[i][0]; int b = dist[i][1]; int soma = a+b; int subt = soma-d; int base = subt/2; int dist_a = a-base; int dist_b = b-base; if(dist_a == exp_a && dist_b == exp_b){///Fora da sub de A ou B fora.push_back(i); }else {///Na subarvore de A ou B if(a>b)grupos[0]++;else grupos[1]++; } } int lim=N/2; if(grupos[0]>lim||grupos[1]>lim)goto fail; ++c; if(c>1)assert(0); ///Executa APENAS 1 vez ///Para a sub 4 if(sub==4){ int sum = N-(grupos[0]+grupos[1]); if(sum>lim)goto fail; return ans; } ///Achei o erro: ///Pode ter mais de um hub ///E ele quer saber se qq um deles eh balanceado assert(fora.size()==(N-(grupos[0]+grupos[1]))); { std::vector<int> vec; for(int i=0;i!=fora.size();++i){ if(!vec.size())vec.push_back(fora[i]); { if(mesma_subarvore(vec.back(),fora[i])){ vec.push_back(fora[i]); }else vec.pop_back(); } } if(vec.size()){ int count=0; for(auto&x:fora)if(mesma_subarvore(x,vec.back()))++count; if(count>lim)goto fail; return ans; }else return ans; } return ans; fail:{} } return -ans; }

컴파일 시 표준 에러 (stderr) 메시지

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from towns.cpp:1:
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:104:27: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  104 |         assert(fora.size()==(N-(grupos[0]+grupos[1])));
      |                ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
towns.cpp:107:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  107 |             for(int i=0;i!=fora.size();++i){
      |                         ~^~~~~~~~~~~~~
towns.cpp:116:21: warning: declaration of 'count' shadows a global declaration [-Wshadow]
  116 |                 int count=0;
      |                     ^~~~~
towns.cpp:7:5: note: shadowed declaration is here
    7 | int count=0;
      |     ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...