-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol_brute.cpp
More file actions
44 lines (35 loc) · 1.16 KB
/
sol_brute.cpp
File metadata and controls
44 lines (35 loc) · 1.16 KB
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
#include "info.h"
#include "output.h"
#include "sol_brute.h"
double scoreForVideoInCache(int videoId, int cacheId, Output &output) {
int initScore = output.getScore();
if (!output.addVideoToCache(videoId, cacheId)) {
return 0.0;
}
int newScore = output.getScore();
output.removeVideoFromCache(videoId, cacheId);
int gain = newScore - initScore;
return (double) gain / videoSizes[videoId];
}
bool insertBestVideoCachePair(Output &output) { // videoId, cacheId
int bestVideo = -1, bestCache = -1;
double bestScore = 1e-9;
for (int videoId = 0; videoId < nVideos; videoId++) {
for (int cacheId = 0; cacheId < nCaches; cacheId++) {
double score = scoreForVideoInCache(videoId, cacheId, output);
if (score > bestScore) {
bestScore = score;
bestVideo = videoId;
bestCache = cacheId;
}
}
}
if (bestVideo != -1) {
output.addVideoToCache(bestVideo, bestCache);
return true;
}
return false;
}
void solveBrute(Output &output) {
while (insertBestVideoCachePair(output)) {}
}