#include "cielib.h" #include <vector> #include <set> #include <cstdlib> //#include <iostream> using namespace std; int g_D, g_K, g_R; vector<int> g_t0, g_t1, g_d, g_u; set<int> g_s; // zbior pozycji na ktorych d[i]!=u[i] void init_global() { g_D = podajD(); g_K = podajK(); g_R = podajR(); g_d.resize(g_D, 0); g_u.resize(g_D, g_R); for(int i=0; i<g_D; ++i) g_s.insert(i); g_t0.resize(g_D); g_t1.resize(g_D); } /* void print(const vector<int>& v) { for(int i=0; i<(int)v.size(); ++i) cout << v[i] << " "; cout << endl; } void print_set() { cout << "SET: "; for(auto it=g_s.begin(); it!=g_s.end(); ++it) cout << *it << " "; cout << endl; } */ void set_middle(vector<int>& v) { for(int i=0; i<(int)v.size(); ++i) { v[i] = (g_d[i]+g_u[i])/2; } } int rand_from_set() { int r = rand() % g_s.size(); auto it = g_s.begin(); advance(it,r); return *it; } int rand_delta(int i) { int delta=1; if(rand()%10<5) delta*=-1; //cout << "*** " << delta << " xxx "; if(g_t1[i]+delta<g_d[i] || g_t1[i]+delta>g_u[i]) delta*=-1; //cout << delta << endl; //if(t0[i]+delta<d[i] || t0[i]+delta>u[i]) {cout << "ERROR" << endl; return 13;} return delta; } int main() { srand(1974); init_global(); int i, delta; while(g_s.size()>0) { set_middle(g_t1); //print(g_d); //print(g_t1); //print(g_u); //cout << call << endl; //cout << endl; czyCieplo(g_t1.data()); int licznik = 0, limit = 100*g_D; do { ++licznik; if(licznik==limit) { set_middle(g_t1); czyCieplo(g_t1.data()); licznik = 0; //cout << "RESET" << endl; } g_t0 = g_t1; i = rand_from_set(); delta = rand_delta(i); g_t1[i] += delta; } while(czyCieplo(g_t1.data())==0); //cout << "-----------------" << endl; if(delta==1) { g_d[i] = g_t1[i]; } else //delta=-1 { g_u[i] = g_t1[i]; } if(g_d[i]==g_u[i]) g_s.erase(i); //print_set(); } /* for(int i=0; i<D; ++i) if(d[i]!=u[i]) { cout << "ERROR" << endl; return 13; } */ /* for(int i=0; i<D; ++i) cout << d[i] << " "; cout << endl; */ znalazlem(g_d.data()); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #include "cielib.h" #include <vector> #include <set> #include <cstdlib> //#include <iostream> using namespace std; int g_D, g_K, g_R; vector<int> g_t0, g_t1, g_d, g_u; set<int> g_s; // zbior pozycji na ktorych d[i]!=u[i] void init_global() { g_D = podajD(); g_K = podajK(); g_R = podajR(); g_d.resize(g_D, 0); g_u.resize(g_D, g_R); for(int i=0; i<g_D; ++i) g_s.insert(i); g_t0.resize(g_D); g_t1.resize(g_D); } /* void print(const vector<int>& v) { for(int i=0; i<(int)v.size(); ++i) cout << v[i] << " "; cout << endl; } void print_set() { cout << "SET: "; for(auto it=g_s.begin(); it!=g_s.end(); ++it) cout << *it << " "; cout << endl; } */ void set_middle(vector<int>& v) { for(int i=0; i<(int)v.size(); ++i) { v[i] = (g_d[i]+g_u[i])/2; } } int rand_from_set() { int r = rand() % g_s.size(); auto it = g_s.begin(); advance(it,r); return *it; } int rand_delta(int i) { int delta=1; if(rand()%10<5) delta*=-1; //cout << "*** " << delta << " xxx "; if(g_t1[i]+delta<g_d[i] || g_t1[i]+delta>g_u[i]) delta*=-1; //cout << delta << endl; //if(t0[i]+delta<d[i] || t0[i]+delta>u[i]) {cout << "ERROR" << endl; return 13;} return delta; } int main() { srand(1974); init_global(); int i, delta; while(g_s.size()>0) { set_middle(g_t1); //print(g_d); //print(g_t1); //print(g_u); //cout << call << endl; //cout << endl; czyCieplo(g_t1.data()); int licznik = 0, limit = 100*g_D; do { ++licznik; if(licznik==limit) { set_middle(g_t1); czyCieplo(g_t1.data()); licznik = 0; //cout << "RESET" << endl; } g_t0 = g_t1; i = rand_from_set(); delta = rand_delta(i); g_t1[i] += delta; } while(czyCieplo(g_t1.data())==0); //cout << "-----------------" << endl; if(delta==1) { g_d[i] = g_t1[i]; } else //delta=-1 { g_u[i] = g_t1[i]; } if(g_d[i]==g_u[i]) g_s.erase(i); //print_set(); } /* for(int i=0; i<D; ++i) if(d[i]!=u[i]) { cout << "ERROR" << endl; return 13; } */ /* for(int i=0; i<D; ++i) cout << d[i] << " "; cout << endl; */ znalazlem(g_d.data()); } |