karlangadas

bridge inconcluso

Dec 12th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <functional>
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. int min (int a, int b){
  7.     if (a<b)
  8.         return a;
  9.     return b;
  10. }
  11. int max (int a, int b){
  12.     if (a<b)
  13.         return b;
  14.     return a;
  15. }
  16.  
  17. int main() {
  18.    
  19.     int ncases;
  20.     cin>>ncases;
  21.     while(ncases--){
  22.         list<int> l1;
  23.         list<int> l2;
  24.         vector<int>v;
  25.         int sum=0;
  26.         int nelem,aux,n1,n2,n3;
  27.         cin>>nelem;
  28.         for (int i=0;i<nelem;i++){
  29.             cin>>aux;
  30.             l1.push_back(aux);
  31.         }
  32.         l1.sort();
  33.         if (l1.size()==0)
  34.             return 0;
  35.         if (l1.size()==1){
  36.             cout<<l1.front()<<endl;
  37.             cout<<l1.front()<<endl;
  38.             return 0;
  39.         }
  40.         int modo=0;
  41.        
  42.         while (!l1.empty()){
  43.             if (l1.size()==2){
  44.                 n1=l1.front();
  45.                
  46.                 l1.pop_front();
  47.                
  48.                 n2=l1.front();
  49.                 l1.pop_front();
  50.                 //cout<<"holi "<<n1<<' '<<n2<<endl;
  51.                 sum=sum+n2;
  52.                 v.push_back(n1);
  53.                 v.push_back(n2);
  54.                 break;
  55.             }
  56.             if (modo==0){
  57.                
  58.                 n1=l1.front();
  59.                
  60.                 l1.pop_front();
  61.                
  62.                
  63.                 n2=l1.front();
  64.                 l1.pop_front();
  65.                 //cout<<"holi "<<n1<<' '<<n2<<endl;
  66.                 sum=sum+n2;
  67.                 v.push_back(n1);
  68.                 v.push_back(n2);
  69.                 l2.push_back(n1);
  70.                 l2.push_back(n2);
  71.                 l2.sort();
  72.                 n3=l2.front();
  73.                 //cout<<"je "<<n3<<endl;
  74.                
  75.                 sum=sum+n3;
  76.                 l2.pop_front();
  77.                 v.push_back(n3);
  78.                 l1.push_front(n3);
  79.                 l1.sort();
  80.                 modo=1;
  81.                 l2.sort();
  82.                 //cout<<"menor de l2: "<<l2.front()<<endl;
  83.             }
  84.             else{
  85.                 n1=l1.back();l1.pop_back();
  86.                 n2=l1.back();l1.pop_back();
  87.                 //cout<<"holi "<<n2<<' '<<n1<<endl;
  88.                 //cout<<"je menor de l2: "<<l2.front()<<endl;
  89.                 sum=sum+n1;
  90.                 v.push_back(n2);
  91.                 v.push_back(n1);
  92.                 l2.push_back(n1);
  93.                 l2.push_back(n2);
  94.                 l2.sort();
  95.                 n3=l2.front();
  96.                 sum=sum+n3;
  97.                 //cout<<"je "<<n3<<endl;
  98.                
  99.                 l2.pop_front();
  100.                 v.push_back(n3);
  101.                 l1.push_front(n3);
  102.                 l1.sort();
  103.                 modo=0;
  104.             }
  105.         }
  106.         cout<<sum<<endl;
  107.         int i=0;
  108.         while ((i+2)<v.size()){
  109.             cout<<v[i]<<' '<<v[i+1]<<endl;
  110.             cout<<v[i+2]<<endl;
  111.             i+=3;
  112.         }
  113.         cout<<v[i]<<' '<<v[i+1]<<endl;
  114.         if (ncases>1)
  115.         cout<<endl;
  116.        
  117.     }
  118.    
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment