#include <iostream> #include <vector> #include <algorithm> #include <limits> using namespace std; typedef long long l; typedef vector<l> v_l; class Client{ public: Client *next; Client *prev; l queue; l b; l time; Client(l dt){ next = NULL; prev = NULL; queue = 1; b = 0; time = dt; } void step1(l dt){ b += dt * a(); time -= dt * queue; } void step2(l dt){ if (time <= 0){ b -= time; if (next != NULL) next->time += time; remove(); time = 0; } } l min_time(){ /// minimum backing time required to make this client wait l d = time / queue; if (time % queue != 0) d += 1; return d; } void remove(){ /// remove from double queue if (prev != NULL) prev->next = next; if (next != NULL){ next->queue += queue; next->b += b; next->prev = prev; // FIXME or not: loost reference } } l a(){ return queue * (queue - 1) / 2; } Client * add_right(l dt){ Client * c = new Client(dt); c->prev = this; next = c; return c; } }; l get_max_arg(Client * prefirst, Client * colector){ l t = numeric_limits<l>::max(); for(Client * c=prefirst->next;c != colector; c=c->next){ t = min(t, c->min_time()); } if (t == numeric_limits<l>::max()) t = -1; return t; } l get_a(Client * prefirst, Client * colector){ l a=0; for(Client * c=prefirst->next;c != NULL; c=c->next){ a += c->a(); } return a; } l get_b(Client * prefirst, Client * colector){ l b=0; for(Client * c=prefirst->next;c != NULL; c=c->next){ b += c->b; } return b; } void do_step(Client * prefirst, Client * colector, l dt){ for(Client * c=prefirst->next;c != colector; c=c->next){ c->step1(dt); } for(Client * c=prefirst->next;c != colector; c=c->next){ c->step2(dt); } } int main(){ v_l max_arg, a, b; l t, n, m; v_l d; Client * prefirst; Client * colector; prefirst = new Client(-1); // IN: cin>>n>>m; cin>>t; colector = prefirst->add_right(t); for(int i=1;i<n;i++){ cin>>t; colector = colector->add_right(t); } colector = colector->add_right(-1); // One after last for(Client * c=colector->prev;c->prev != prefirst; c=c->prev){ c->time -= c->prev->time; } for(int i=0;i<m;i++){ cin>>t; d.push_back(t); } // ALG: max_arg.push_back(0); t=0; while (t != -1){ t = get_max_arg(prefirst, colector); // DEBUG: // cout << "Found max t: " << t << endl; // cout<<"clients: ["<<endl;; // for(Client * c=prefirst;c != NULL; c=c->next){ // cout<<" C("; // cout<<"a: " << c->a() << ", "; // cout<<"b: " << c->b << ", "; // cout<<"time: " << c->time << ", "; // cout<<"queue: " << c->queue << ")" << endl; // } // cout<<"]"<<endl; // END DEBUG if (t == -1)max_arg.push_back(numeric_limits<l>::max()/2); else max_arg.push_back(t); a.push_back(get_a(prefirst, colector)); b.push_back(get_b(prefirst, colector)); do_step(prefirst, colector, t); } for(int i=1;i<max_arg.size();i++)max_arg[i] += max_arg[i-1]; // DEBUG: // cout<<"max_arg: [ "; // for(int i=0;i<max_arg.size();i++)cout<<max_arg[i]<<" "; // cout<<"]"<<endl; // for(int i=0;i<max_arg.size();i++){ // cout<<"i: "<<i<<", a[i]: "<<a[i]<<", b[i]: "<<b[i]; // cout<<", max_arg[i]: "<<max_arg[i]<<endl; // } // OUT: for(int i=0;i<m;i++){ l j = lower_bound(max_arg.begin(), max_arg.end(), d[i]) - max_arg.begin() - 1; cout<<a[j] * (d[i] - max_arg[j]) + b[j]<<endl; } return 0; }
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | #include <iostream> #include <vector> #include <algorithm> #include <limits> using namespace std; typedef long long l; typedef vector<l> v_l; class Client{ public: Client *next; Client *prev; l queue; l b; l time; Client(l dt){ next = NULL; prev = NULL; queue = 1; b = 0; time = dt; } void step1(l dt){ b += dt * a(); time -= dt * queue; } void step2(l dt){ if (time <= 0){ b -= time; if (next != NULL) next->time += time; remove(); time = 0; } } l min_time(){ /// minimum backing time required to make this client wait l d = time / queue; if (time % queue != 0) d += 1; return d; } void remove(){ /// remove from double queue if (prev != NULL) prev->next = next; if (next != NULL){ next->queue += queue; next->b += b; next->prev = prev; // FIXME or not: loost reference } } l a(){ return queue * (queue - 1) / 2; } Client * add_right(l dt){ Client * c = new Client(dt); c->prev = this; next = c; return c; } }; l get_max_arg(Client * prefirst, Client * colector){ l t = numeric_limits<l>::max(); for(Client * c=prefirst->next;c != colector; c=c->next){ t = min(t, c->min_time()); } if (t == numeric_limits<l>::max()) t = -1; return t; } l get_a(Client * prefirst, Client * colector){ l a=0; for(Client * c=prefirst->next;c != NULL; c=c->next){ a += c->a(); } return a; } l get_b(Client * prefirst, Client * colector){ l b=0; for(Client * c=prefirst->next;c != NULL; c=c->next){ b += c->b; } return b; } void do_step(Client * prefirst, Client * colector, l dt){ for(Client * c=prefirst->next;c != colector; c=c->next){ c->step1(dt); } for(Client * c=prefirst->next;c != colector; c=c->next){ c->step2(dt); } } int main(){ v_l max_arg, a, b; l t, n, m; v_l d; Client * prefirst; Client * colector; prefirst = new Client(-1); // IN: cin>>n>>m; cin>>t; colector = prefirst->add_right(t); for(int i=1;i<n;i++){ cin>>t; colector = colector->add_right(t); } colector = colector->add_right(-1); // One after last for(Client * c=colector->prev;c->prev != prefirst; c=c->prev){ c->time -= c->prev->time; } for(int i=0;i<m;i++){ cin>>t; d.push_back(t); } // ALG: max_arg.push_back(0); t=0; while (t != -1){ t = get_max_arg(prefirst, colector); // DEBUG: // cout << "Found max t: " << t << endl; // cout<<"clients: ["<<endl;; // for(Client * c=prefirst;c != NULL; c=c->next){ // cout<<" C("; // cout<<"a: " << c->a() << ", "; // cout<<"b: " << c->b << ", "; // cout<<"time: " << c->time << ", "; // cout<<"queue: " << c->queue << ")" << endl; // } // cout<<"]"<<endl; // END DEBUG if (t == -1)max_arg.push_back(numeric_limits<l>::max()/2); else max_arg.push_back(t); a.push_back(get_a(prefirst, colector)); b.push_back(get_b(prefirst, colector)); do_step(prefirst, colector, t); } for(int i=1;i<max_arg.size();i++)max_arg[i] += max_arg[i-1]; // DEBUG: // cout<<"max_arg: [ "; // for(int i=0;i<max_arg.size();i++)cout<<max_arg[i]<<" "; // cout<<"]"<<endl; // for(int i=0;i<max_arg.size();i++){ // cout<<"i: "<<i<<", a[i]: "<<a[i]<<", b[i]: "<<b[i]; // cout<<", max_arg[i]: "<<max_arg[i]<<endl; // } // OUT: for(int i=0;i<m;i++){ l j = lower_bound(max_arg.begin(), max_arg.end(), d[i]) - max_arg.begin() - 1; cout<<a[j] * (d[i] - max_arg[j]) + b[j]<<endl; } return 0; } |