Poj 1862 Stripies/priority queue, pojstripies
Original question link: http://poj.org/problem? Id = 1862
Simple question, greedy + priority queue mainly exercises stl
Writing several implementation methods into the form of classes still takes a little longer...
Hand-played heap:
1:
1 # include <cstdio> 2 # include <cstdlib> 3 # include <cmath> 4 # include <iostream> 5 class Solution {6 public: 7 static const int Max_N = 110; 8 int sz; 9 double heap [Max_N]; 10 inline void init () {11 sz = 0; 12} 13 inline void push (double x) {14 int I = sz ++; 15 while (I> 0) {16 int p = (I-1)> 1; 17 if (heap [p]> = x) break; 18 heap [I] = heap [p]; 19 I = p; 20} 21 heap [I] = x; 22} 23 inline double pop () {24 double ret = heap [0], x = heap [-- sz]; 25 int I = 0; 26 while (I <1) + 1 <sz) {27 int a = (I <1) + 1, B = (I <1) + 2; 28 if (B <sz & heap [a] <= heap [B]) a = B; 29 if (heap [a] <= x) break; 30 heap [I] = heap [a]; 31 I = a; 32} 33 heap [I] = x; 34 return ret; 35} 36}; 37 int main () {38 # ifdef LOCAL39 freopen ("in.txt", "r", stdin); 40 freopen ("out.txt", "w +", stdout); 41 # endif42 int n; 43 double a, B; 44 Solution ans; 45 ans. init (); 46 scanf ("% d", & n); 47 while (n --) {48 scanf ("% lf", & a); 49 ans. push (a); 50} 51 while (ans. sz> 1) {52 a = ans. pop (), B = ans. pop (); 53 ans. push (2 * sqrt (a * B); 54} 55 printf ("%. 3lf ", ans. pop (); 56 return 0; 57}View Code
2:
1 # include <cstdio> 2 # include <cstdlib> 3 # include <cmath> 4 # include <iostream> 5 struct Node {6 static const int Max_N = 110; 7 int sz; 8 double heap [Max_N]; 9 Node (): sz (0) {} 10 inline void push (double x) {11 int I = sz ++; 12 while (I> 0) {13 int p = (I-1)> 1; 14 if (heap [p]> = x) break; 15 heap [I] = heap [p]; 16 I = p; 17} 18 heap [I] = x; 19} 20 inline double pop () {21 double ret = heap [0], x = heap [-- sz]; 22 int I = 0; 23 while (I <1) + 1 <sz) {24 int a = (I <1) + 1, B = (I <1) + 2; 25 if (B <sz & heap [a] <= heap [B]) a = B; 26 if (heap [a] <= x) break; 27 heap [I] = heap [a]; 28 I = a; 29} 30 heap [I] = x; 31 return ret; 32} 33} ans; 34 int main () {35 # ifdef LOCAL36 freopen ("in.txt", "r", stdin); 37 freopen ("out.txt", "w +", stdout ); 38 # endif39 int n; 40 double a, B; 41 scanf ("% d", & n); 42 while (n --) {43 scanf ("% lf ", & a); 44 ans. push (a); 45} 46 while (ans. sz> 1) {47 a = ans. pop (), B = ans. pop (); 48 ans. push (2 * sqrt (a * B); 49} 50 printf ("%. 3lf ", ans. pop (); 51 return 0; 52}View Code
3:
1 # include <cstdio> 2 # include <cstdlib> 3 # include <cmath> 4 # include <iostream> 5 const int Max_N = 110; 6 double heap [Max_N]; 7 int sz = 0; 8 void push (double x) {9 int I = sz ++; 10 while (I> 0) {11 int p = (I-1)> 1; 12 if (heap [p]> = x) break; 13 heap [I] = heap [p]; 14 I = p; 15} 16 heap [I] = x; 17} 18 double pop () {19 double ret = heap [0], x = heap [-- sz]; 20 int I = 0; 21 while (I <1) + 1 <sz) {22 int a = (I <1) + 1, B = (I <1) + 2; 23 if (B <sz & heap [a] <= heap [B]) a = B; 24 if (heap [a] <= x) break; 25 heap [I] = heap [a]; 26 I = a; 27} 28 heap [I] = x; 29 return ret; 30} 31 int main () {32 # ifdef LOCAL33 freopen ("in.txt", "r", stdin); 34 freopen ("out.txt ", "w +", stdout); 35 # endif36 int n; 37 double a, B; 38 39 scanf ("% d", & n); 40 while (n --) {41 scanf ("% lf", & a); 42 push (a); 43} 44 while (sz> 1) {45 a = pop (), B = pop (); 46 push (2 * sqrt (a * B); 47} 48 printf ("%. 3lf ", pop (); 49 return 0; 50}View Code
4:
std::priority_queue<double> ans
1 # include <cstdio> 2 # include <cstdlib> 3 # include <cmath> 4 # include <queue> 5 # include <iostream> 6 int main () {7 # ifdef LOCAL 8 freopen ("in.txt", "r", stdin); 9 freopen ("out.txt", "w +", stdout); 10 # endif11 int n; 12 double a, B; 13 scanf ("% d", & n); 14 std: priority_queue <double> ans; 15 while (n --) {16 scanf ("% lf", & a); 17 ans. push (a); 18} 19 while (ans. size ()> 1) {20 a = ans. top (), ans. pop (); 21 B = ans. top (), ans. pop (); 22 ans. push (2 * sqrt (a * B); 23} 24 printf ("%. 3lf ", ans. top (); 25 return 0; 26}View Code