Advertisement
karlangadas

The Most Potent Corner

Dec 10th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <set>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. vector<int>v;
  10. vector<int>pot;
  11. int ncorn;     
  12.  
  13.  
  14. int adyacentes(int a,int b){
  15.     if (a==b)
  16.         return 0;
  17.     //En a está el min
  18.     for (int k=1;k<ncorn;k=k*2){
  19.         if ((a^k)==b){
  20.             //cout<<a<<" y "<<b<<" son adyacentes"<<endl;
  21.             //cout<<a<<" xor: "<<k<<"= "<<b<<endl;
  22.             return 1;}
  23.     }
  24.     return 0;
  25. }  
  26. int potencial(int ind){
  27.     int suma=0;
  28.     for (int i=0;i<ncorn;i++){
  29.         if(adyacentes(ind,i))
  30.             suma=suma+v[i];
  31.     }
  32.     return suma;
  33. }
  34. int main() {
  35.     int ndim;
  36.     while(cin>>ndim){
  37.         //cout<<"ndim: "<<ndim<<endl;
  38.         int aux;
  39.         ncorn=pow(2,ndim);
  40.         //cout<<"ncorn: "<<ncorn<<endl;
  41.         for (int i=0;i<ncorn;i++){
  42.             cin>>aux;
  43.             v.push_back(aux);
  44.         }
  45.         //Calcular potenciales
  46.         for (int i=0;i<ncorn;i++){
  47.             pot.push_back(potencial(i));
  48.             //cout<<"potencial de "<<i<<": "<<pot[i]<<endl;
  49.         }
  50.         int suma,max=0;
  51.         for (int i=0;i<ncorn;i++){
  52.             for (int j=0;j<ncorn;j++){
  53.                 if (adyacentes(i,j)){
  54.                     suma=pot[i]+pot[j];
  55.                     if (suma>max)
  56.                         max=suma;
  57.                 }
  58.             }
  59.         }
  60.         cout<<max<<endl;
  61.         v.clear();
  62.         pot.clear();
  63.     }
  64.     return 0;
  65.    
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement