File size: 12,759 Bytes
14c9c2b | 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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | #include <bits/stdc++.h>
using namespace std;
struct Type {
string name;
long long q;
long long v;
long long m;
long long l;
};
struct PItem {
int type;
int mult;
long long val;
int wM;
int wL;
};
static const long long MASS_CAP = 20000000LL; // mg
static const long long VOL_CAP = 25000000LL; // uL
// Simple JSON-like parser tailored for the given input format
vector<Type> parseInput(istream& in) {
string s((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
vector<Type> items;
size_t i = 0, n = s.size();
auto skip_ws = [&]() {
while (i < n && isspace((unsigned char)s[i])) i++;
};
auto skip_until = [&](char c){
while (i < n && s[i] != c) i++;
if (i < n && s[i] == c) i++;
};
auto parse_string = [&]() -> string {
skip_ws();
while (i < n && s[i] != '"') i++;
if (i < n && s[i] == '"') i++;
string res;
while (i < n && s[i] != '"') {
if (s[i] == '\\' && i + 1 < n) {
res.push_back(s[i+1]);
i += 2;
} else {
res.push_back(s[i++]);
}
}
if (i < n && s[i] == '"') i++;
return res;
};
auto parse_ll = [&]() -> long long {
skip_ws();
// move to first digit or '-'
while (i < n && !(s[i] == '-' || (s[i] >= '0' && s[i] <= '9'))) i++;
long long sign = 1;
if (i < n && s[i] == '-') { sign = -1; i++; }
long long val = 0;
while (i < n && s[i] >= '0' && s[i] <= '9') {
val = val * 10 + (s[i] - '0');
i++;
}
return sign * val;
};
skip_ws();
// expect {
while (i < n && s[i] != '{') i++;
if (i < n && s[i] == '{') i++;
while (true) {
skip_ws();
if (i >= n) break;
if (s[i] == '}') { i++; break; }
string key = parse_string();
skip_ws();
// skip to '['
while (i < n && s[i] != '[') i++;
if (i < n && s[i] == '[') i++;
long long q = parse_ll();
long long v = parse_ll();
long long m = parse_ll();
long long l = parse_ll();
// skip to ']'
while (i < n && s[i] != ']') i++;
if (i < n && s[i] == ']') i++;
items.push_back({key, q, v, m, l});
// skip to next '"' or '}'
while (i < n && s[i] != '"' && s[i] != '}') i++;
if (i < n && s[i] == '}') { i++; break; }
}
return items;
}
struct Solution {
vector<long long> cnt;
long long value;
long long mass;
long long vol;
};
static inline long long clampLL(long long x, long long lo, long long hi) {
if (x < lo) return lo;
if (x > hi) return hi;
return x;
}
Solution evalSolution(const vector<Type>& types, const vector<long long>& cnt) {
long long mass=0, vol=0, val=0;
int n = (int)types.size();
for (int i=0;i<n;i++) {
long long c = cnt[i];
if (c < 0) c = 0;
if (c > types[i].q) c = types[i].q;
mass += types[i].m * c;
vol += types[i].l * c;
val += types[i].v * c;
}
// Enforce feasibility
if (mass > MASS_CAP || vol > VOL_CAP) {
// infeasible, return zero
return Solution{vector<long long>(n,0),0,0,0};
}
return Solution{cnt, val, mass, vol};
}
Solution greedyFill(const vector<Type>& types, const vector<long long>& base, double lambda) {
int n = (int)types.size();
vector<long long> cnt = base;
long long mass=0, vol=0, val=0;
for (int i=0;i<n;i++) {
long long c = clampLL(cnt[i], 0, types[i].q);
cnt[i] = c;
mass += types[i].m * c;
vol += types[i].l * c;
val += types[i].v * c;
}
long long remM = MASS_CAP - mass;
long long remL = VOL_CAP - vol;
if (remM < 0 || remL < 0) {
// base infeasible; return base as is (will be evaluated as infeasible outside)
return evalSolution(types, cnt);
}
while (true) {
int best = -1;
double bestScore = -1.0;
for (int i=0;i<n;i++) {
if (cnt[i] >= types[i].q) continue;
if (types[i].m > remM || types[i].l > remL) continue;
double normM = (double)types[i].m / (double)MASS_CAP;
double normL = (double)types[i].l / (double)VOL_CAP;
double denom = lambda * normM + (1.0 - lambda) * normL;
if (denom <= 0) continue;
double score = (double)types[i].v / denom;
if (score > bestScore) { bestScore = score; best = i; }
}
if (best == -1) break;
long long canM = remM / types[best].m;
long long canL = remL / types[best].l;
long long canQ = types[best].q - cnt[best];
long long add = min(canQ, min(canM, canL));
if (add <= 0) break;
cnt[best] += add;
remM -= add * types[best].m;
remL -= add * types[best].l;
val += add * types[best].v;
}
return evalSolution(types, cnt);
}
Solution greedyMaxRatio(const vector<Type>& types) {
int n = (int)types.size();
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
vector<double> ratio(n, 0.0);
for (int i=0;i<n;i++) {
double a = (double)types[i].m / (double)MASS_CAP;
double b = (double)types[i].l / (double)VOL_CAP;
double d = max(a, b);
if (d <= 0) ratio[i] = 0;
else ratio[i] = (double)types[i].v / d;
}
sort(ord.begin(), ord.end(), [&](int a, int b){
if (ratio[a] != ratio[b]) return ratio[a] > ratio[b];
return types[a].v > types[b].v;
});
vector<long long> cnt(n, 0);
long long remM = MASS_CAP, remL = VOL_CAP;
for (int id : ord) {
if (types[id].m > remM || types[id].l > remL) continue;
long long k = min((long long)types[id].q, min(remM / types[id].m, remL / types[id].l));
if (k > 0) {
cnt[id] = k;
remM -= k * types[id].m;
remL -= k * types[id].l;
}
}
return evalSolution(types, cnt);
}
Solution solveDPScaled(const vector<Type>& types, long long scaleM, long long scaleL) {
int n = (int)types.size();
long long Msc = MASS_CAP / scaleM; // floor
long long Lsc = VOL_CAP / scaleL; // floor
int MS = (int)Msc;
int LS = (int)Lsc;
if (MS <= 0 || LS <= 0) {
vector<long long> zero(n,0);
return evalSolution(types, zero);
}
// Build pseudo items via binary splitting
vector<PItem> items;
items.reserve(200);
for (int i=0;i<n;i++) {
long long qcap = min(types[i].q, min(MASS_CAP / max(1LL, types[i].m), VOL_CAP / max(1LL, types[i].l)));
long long j = 1;
long long Q = qcap;
while (Q > 0) {
long long chunk = min(j, Q);
long long tm = types[i].m * chunk;
long long tl = types[i].l * chunk;
int wM = (int)((tm + scaleM - 1) / scaleM);
int wL = (int)((tl + scaleL - 1) / scaleL);
if (wM <= MS && wL <= LS) {
items.push_back({i, (int)chunk, types[i].v * chunk, wM, wL});
}
Q -= chunk;
j <<= 1;
}
}
int W = (LS + 1);
int SZ = (MS + 1) * W;
// DP arrays
vector<long long> dp(SZ, 0);
vector<int> prevItem(SZ, -1);
vector<int> prevIdx(SZ, -1);
for (int idxItem = 0; idxItem < (int)items.size(); idxItem++) {
const PItem &p = items[idxItem];
for (int m = MS; m >= p.wM; --m) {
int base_m = m * W;
int prev_m = (m - p.wM) * W;
for (int v = LS; v >= p.wL; --v) {
int idx = base_m + v;
int pidx = prev_m + (v - p.wL);
long long cand = dp[pidx] + p.val;
if (cand > dp[idx]) {
dp[idx] = cand;
prevItem[idx] = idxItem;
prevIdx[idx] = pidx;
}
}
}
}
// Find best state
long long bestVal = -1;
int bestState = 0;
for (int i=0;i<SZ;i++) {
if (dp[i] > bestVal) { bestVal = dp[i]; bestState = i; }
}
vector<long long> cnt(n, 0);
int cur = bestState;
while (cur >= 0 && prevItem[cur] != -1) {
int it = prevItem[cur];
cnt[items[it].type] += items[it].mult;
cur = prevIdx[cur];
}
// Ensure counts do not exceed caps
for (int i=0;i<n;i++) {
cnt[i] = clampLL(cnt[i], 0, types[i].q);
}
Solution sol = evalSolution(types, cnt);
// Use leftover capacity to greedily fill
vector<double> lambdas = {0.0, 0.33, 0.5, 0.67, 1.0};
Solution best = sol;
for (double lam : lambdas) {
Solution s2 = greedyFill(types, sol.cnt, lam);
if (s2.value > best.value) best = s2;
}
return best;
}
Solution greedyMulti(const vector<Type>& types) {
vector<double> lambdas = {0.0, 0.25, 0.5, 0.75, 1.0};
Solution best{{},0,0,0};
int n = (int)types.size();
for (double lam : lambdas) {
vector<int> ord(n);
iota(ord.begin(), ord.end(), 0);
vector<double> ratio(n, 0.0);
for (int i=0;i<n;i++) {
double normM = (double)types[i].m / (double)MASS_CAP;
double normL = (double)types[i].l / (double)VOL_CAP;
double denom = lam * normM + (1.0 - lam) * normL;
if (denom <= 0) ratio[i] = 0;
else ratio[i] = (double)types[i].v / denom;
}
sort(ord.begin(), ord.end(), [&](int a, int b){
if (ratio[a] != ratio[b]) return ratio[a] > ratio[b];
return types[a].v > types[b].v;
});
vector<long long> cnt(n, 0);
long long remM = MASS_CAP, remL = VOL_CAP;
for (int id : ord) {
if (types[id].m > remM || types[id].l > remL) continue;
long long add = min((long long)types[id].q, min(remM / types[id].m, remL / types[id].l));
if (add > 0) {
cnt[id] = add;
remM -= add * types[id].m;
remL -= add * types[id].l;
}
}
Solution s = evalSolution(types, cnt);
if (s.value > best.value) best = s;
}
Solution smax = greedyMaxRatio(types);
if (smax.value > best.value) best = smax;
return best;
}
Solution pairwiseImprove(const vector<Type>& types, Solution sol) {
int n = (int)types.size();
vector<long long> cnt = sol.cnt;
long long mass = sol.mass, vol = sol.vol, val = sol.value;
bool improved = true;
int iter = 0;
while (improved && iter < 200) {
improved = false;
iter++;
for (int i=0;i<n && !improved;i++) {
if (cnt[i] >= types[i].q) continue;
for (int j=0;j<n && !improved;j++) {
if (cnt[j] <= 0) continue;
if (i == j) continue;
long long nmass = mass + types[i].m - types[j].m;
long long nvol = vol + types[i].l - types[j].l;
if (nmass <= MASS_CAP && nvol <= VOL_CAP && types[i].v > types[j].v) {
mass = nmass; vol = nvol; val += (types[i].v - types[j].v);
cnt[i]++; cnt[j]--;
improved = true;
}
}
}
}
return evalSolution(types, cnt);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<Type> types = parseInput(cin);
int n = (int)types.size();
// Cap q by feasibility
for (int i=0;i<n;i++) {
long long capM = (types[i].m > 0) ? MASS_CAP / types[i].m : 0;
long long capL = (types[i].l > 0) ? VOL_CAP / types[i].l : 0;
long long qcap = min(types[i].q, min(capM, capL));
types[i].q = max(0LL, qcap);
}
// Generate candidates
vector<pair<long long,long long>> scales = {
{100000, 125000}, // 200 x 200
{100000, 100000}, // 200 x 250
{50000, 125000}, // 400 x 200
{200000, 125000}, // 100 x 200
};
Solution best{{},0,0,0};
// Greedy baselines
Solution g = greedyMulti(types);
if (g.value > best.value) best = g;
// DP candidates
for (auto sc : scales) {
Solution s = solveDPScaled(types, sc.first, sc.second);
if (s.value > best.value) best = s;
}
// Final light local improvement
Solution improved = pairwiseImprove(types, best);
if (improved.value > best.value) best = improved;
// Output JSON with same keys in same order
cout << "{\n";
for (int i=0;i<n;i++) {
cout << " \"" << types[i].name << "\": " << clampLL(best.cnt[i], 0, types[i].q);
if (i + 1 < n) cout << ",\n";
else cout << "\n";
}
cout << "}\n";
return 0;
} |