#include <cstdio> #include <list> using namespace std; long long calculate_next_oven(list<long long*> clients) { long long* last_client; long long* client; long long res = 1000001; list<long long*>::iterator it; it = clients.begin(); last_client = *it; ++it; while(it != clients.end()) { client = *it; long long delta = client[0] - last_client[0]; long long new_res= (delta-1)/last_client[1] + 1; if (new_res < res) res = new_res; last_client = client; ++it; } return res; } int main() { long long n, m; scanf("%lld", &n); scanf("%lld", &m); if(n*m > 400000000) { list<long long*> clients; long long* ovens = new long long[m]; long long* first_client = new long long[2]; first_client[0] = 0; first_client[1] = 1; clients.push_back(first_client); long long* last_client = first_client; long long zero_res = 0; for(long long i = 1; i <= n; i++) { long long* client = new long long[2]; scanf("%lld", client); client[1]=1; if(client[0] == last_client[0]) { zero_res += last_client[1]; last_client[1] += 1; } else { clients.push_back(client); last_client = client; } } long long max_oven = 0; for(int i = 0; i < m; i++) { scanf("%lld", ovens + i); if (ovens[i] > max_oven) max_oven = ovens[i]; } long long* res = new long long[max_oven + 1]; res[0] = 0; long long* standard_res = new long long[max_oven + 1]; standard_res[0] = zero_res; long long next_oven = calculate_next_oven(clients); list<long long*>::iterator it; for(long long i = 1; i <= max_oven; i++) { if(i == next_oven) { standard_res[i] = 0; res[i] = 0; it = clients.begin(); long long* last_client = *it; long long* client; while(it != clients.end()) { client = *it; if((client[0] - last_client[0]) <= last_client[1]*i && last_client != client) { long long tmp_standard_res = (client[1]*(last_client[1]*2 -1 + client[1]))/2; standard_res[i] += tmp_standard_res; res[i] += tmp_standard_res; long long bonus_res = (last_client[1]*(i-1) - (client[0] - last_client[0]))*client[1]; res[i] += bonus_res; last_client[1] += client[1]; it = clients.erase(it); continue; } long long tmp_standard_res= (client[1]*(client[1]-1))/2; standard_res[i] += tmp_standard_res; res[i] += tmp_standard_res; last_client = client; ++it; } next_oven = calculate_next_oven(clients); } else { res[i] = standard_res[i-1]; standard_res[i] = res[i]; } } for(long long i = 1; i <= max_oven; i++) { res[i] += res[i-1]; } for(long long i = 0; i < m; i++) { printf("%lld\n", res[ovens[i]]); } return 0; } else { long long* clients = new long long[n]; long long* ovens = new long long[m]; long long* res = new long long[m]; long long* next_free = new long long[m]; for(int i = 0; i < n; i++) scanf("%lld", clients + i); for(int i = 0; i < m; i++) { res[i] = 0; next_free[i] = 0; scanf("%lld", ovens + i); } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { next_free[j] += ovens[j]; if(next_free[j] > clients[i]) { long long tmp = next_free[j] - clients[i]; res[j] += tmp; } else next_free[j] = clients[i]; } } for(int i = 0; i < m;i++) printf("%lld\n", res[i]); 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 161 162 163 164 165 166 167 168 169 170 171 172 | #include <cstdio> #include <list> using namespace std; long long calculate_next_oven(list<long long*> clients) { long long* last_client; long long* client; long long res = 1000001; list<long long*>::iterator it; it = clients.begin(); last_client = *it; ++it; while(it != clients.end()) { client = *it; long long delta = client[0] - last_client[0]; long long new_res= (delta-1)/last_client[1] + 1; if (new_res < res) res = new_res; last_client = client; ++it; } return res; } int main() { long long n, m; scanf("%lld", &n); scanf("%lld", &m); if(n*m > 400000000) { list<long long*> clients; long long* ovens = new long long[m]; long long* first_client = new long long[2]; first_client[0] = 0; first_client[1] = 1; clients.push_back(first_client); long long* last_client = first_client; long long zero_res = 0; for(long long i = 1; i <= n; i++) { long long* client = new long long[2]; scanf("%lld", client); client[1]=1; if(client[0] == last_client[0]) { zero_res += last_client[1]; last_client[1] += 1; } else { clients.push_back(client); last_client = client; } } long long max_oven = 0; for(int i = 0; i < m; i++) { scanf("%lld", ovens + i); if (ovens[i] > max_oven) max_oven = ovens[i]; } long long* res = new long long[max_oven + 1]; res[0] = 0; long long* standard_res = new long long[max_oven + 1]; standard_res[0] = zero_res; long long next_oven = calculate_next_oven(clients); list<long long*>::iterator it; for(long long i = 1; i <= max_oven; i++) { if(i == next_oven) { standard_res[i] = 0; res[i] = 0; it = clients.begin(); long long* last_client = *it; long long* client; while(it != clients.end()) { client = *it; if((client[0] - last_client[0]) <= last_client[1]*i && last_client != client) { long long tmp_standard_res = (client[1]*(last_client[1]*2 -1 + client[1]))/2; standard_res[i] += tmp_standard_res; res[i] += tmp_standard_res; long long bonus_res = (last_client[1]*(i-1) - (client[0] - last_client[0]))*client[1]; res[i] += bonus_res; last_client[1] += client[1]; it = clients.erase(it); continue; } long long tmp_standard_res= (client[1]*(client[1]-1))/2; standard_res[i] += tmp_standard_res; res[i] += tmp_standard_res; last_client = client; ++it; } next_oven = calculate_next_oven(clients); } else { res[i] = standard_res[i-1]; standard_res[i] = res[i]; } } for(long long i = 1; i <= max_oven; i++) { res[i] += res[i-1]; } for(long long i = 0; i < m; i++) { printf("%lld\n", res[ovens[i]]); } return 0; } else { long long* clients = new long long[n]; long long* ovens = new long long[m]; long long* res = new long long[m]; long long* next_free = new long long[m]; for(int i = 0; i < n; i++) scanf("%lld", clients + i); for(int i = 0; i < m; i++) { res[i] = 0; next_free[i] = 0; scanf("%lld", ovens + i); } for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { next_free[j] += ovens[j]; if(next_free[j] > clients[i]) { long long tmp = next_free[j] - clients[i]; res[j] += tmp; } else next_free[j] = clients[i]; } } for(int i = 0; i < m;i++) printf("%lld\n", res[i]); return 0; } } |