UVa 10037 Bridge

 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  #include <iostream>
 4  #include <algorithm>
 5  using namespace std;
 6  int main()
 7  {
 8      int cas;
 9      cin>>cas;
10      while(cas--)
11      {
12          int v[1005]={0};
13          int n;
14          cin >> n;
15 
16          for(int i=0;i<n;i++)
17          {
18              cin>>v[i];
19          }
20          if(n==1)
21          {
22              cout << v[0] <<endl << v[0] <<endl;
23              if(cas) cout << endl;
24              continue;
25          }
26 
27          sort(v,v+n);
28          int nn=n;
29          int ans=0;
30          while(n>3)
31          {
32              if(v[0]+v[n-2]<2*v[1])
33              {
34                  ans+=2*v[0]+v[n-2]+v[n-1];
35              }
36              else
37              {
38                  ans+=2*v[1]+v[0]+v[n-1];
39              }
40              n-=2;
41          }
42          if(n==2)
43          {
44              ans+=v[1];
45          }
46          else if(n==3)
47          {
48              ans+=v[1]+v[2]+v[0];
49          }
50 
51          cout << ans <<endl;
52          n=nn;
53          while(n>3)
54          {
55              if(v[0]+v[n-2]<2*v[1])
56              {
57                  cout << v[0] << ' ' << v[n-1] << endl;
58                  cout << v[0] << endl;
59                  cout << v[0] << ' ' << v[n-2] << endl;
60                  cout << v[0] << endl;
61              }
62              else
63              {
64                  cout << v[0] << ' ' << v[1] << endl;
65                  cout << v[0] << endl;
66                  cout << v[n-2] << ' ' << v[n-1] << endl;
67                  cout << v[1] << endl;
68 
69              }
70              n-=2;
71          }
72          if(n==2)
73          {
74              cout << v[0] << ' ' << v[1] << endl;
75          }
76          else if(n==3)
77          {
78              cout << v[0] << ' ' << v[2] << endl;
79              cout << v[0] << endl;
80              cout << v[0] << ' ' << v[1] << endl;
81          }
82          if(cas) cout<< endl;
83      }
84 
85 
86      return 0;

87  }

留言

這個網誌中的熱門文章

Things a Little Bird Told Me: Confessions of the Creative Mind

UVa 12970 Alcoholic Pilots

UVa 483 Word Scramble