This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
vector<int> Grafo, Dulces;
vector<bool> Visitados;
int n;
mt19937 Aleatorio(2);
int Costo(vector<int> Asignado){
int Retorno = 0;
for(int i = 0; i < n; i++){
int Dulce_1 = Dulces[Asignado[i]];
int Dulce_2 = Dulces[Asignado[Grafo[i]]];
int Disconformidad = abs(Dulce_1 - Dulce_2);
Retorno = max(Retorno, Disconformidad);
}
return Retorno;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
Grafo.assign(n, 0);
Dulces.assign(n, 0);
Visitados.assign(n, 0);
for(int i = 0; i < n; i++) cin>>Grafo[i];
for(int i = 0; i < n; i++){
cin>>Dulces[i];
Grafo[i]--;
}
double Temperatura = 1000000;
double Enfriador = 0.999999;
vector<int> Asignar(n);
for(int i = 0; i < n; i++) Asignar[i] = i;
uniform_int_distribution<int> Entero(0, n - 1);
uniform_real_distribution<double> Decimal(0, 1);
int Costo_anterior = Costo(Asignar);
while(Temperatura > 0.000001){
int _1 = Entero(Aleatorio);
int _2 = Entero(Aleatorio);
vector<int> Copia = Asignar;
swap(Copia[_1], Copia[_2]);
int Nuevo = Costo(Copia);
if(Nuevo < Costo_anterior){
Costo_anterior = Nuevo;
Asignar = Copia;
} else {
double Exponente = (double)abs(Nuevo - Costo_anterior);
Exponente *= -1;
Exponente /= Temperatura;
if(exp(Exponente) > Decimal(Aleatorio)){
Costo_anterior = Nuevo;
Asignar = Copia;
}
}
Temperatura *= Enfriador;
/*cout<<Costo_anterior<<"\n";
for(auto E: Asignar) cout<<Dulces[E]<<" ";
cout<<"\n"<<Temperatura<<"\n";*/
}
cout<<Costo(Asignar)<<"\n";
for(int i = 0; i < n; i++){
cout<<Dulces[Asignar[i]]<<" ";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |