私はこのパズルを解決しようとしています:Shipping Coding Puzzle。これは私がこれまでに出てきたコードです:g ++でC++プログラムのマイナーページフォールトを避ける
#include <fcntl.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sstream>
#include <set>
using namespace std;
struct Container
{
Container(int c, int w) : cost(c), weight(w){}
int cost;
int weight;
bool operator<(const Container& c) const
{
return double(cost)/weight < double(c.cost)/c.weight;
}
};
int main(int argc, char** argv)
{
int fd = open(argv[1], O_RDONLY);
char* pContent = (char*)mmap(NULL, 128 * sizeof(int), PROT_READ, MAP_PRIVATE, fd, 0);
pContent += 8;
stringstream ss(pContent);
int unload = 0;
ss >> unload;
set<Container> containers;
int cost = 0, weight = 0;
while(ss >> cost)
{
ss >> weight;
containers.insert(Container(cost, weight));
}
const Container& best = *containers.begin();
cost = best.cost * ceil(double(unload)/best.weight);
printf("%d\n", cost);
return 0;
}
私はこのロジックにいくつかのバグがあるかもしれないことは知っています。しかし、私の質問は論理に関連していません。このコードを提出すると、正常に実行されますが、マイナーページフォルト番号が409
であるため、得点は少なくなります。リーダーボードを見ると、誰かがマイナーページフォールトのC++コードを提出した69
です。これらのマイナーページフォルトを制御できる方法はありますか?いくつかのg ++フラグを使用している可能性がありますか?現在、私のmakeファイルは非常にシンプルです:g++ -o MyExe MyExe.cc
。
この文脈で「マイナーページフォールト」とは何ですか? –
私はそれが「需要ページング」でなければならないと信じます。そして、同じ結果を達成するために、他の人がすべて一緒に異なるアプローチを使用しているかもしれません。 –
@Als私は彼がソフトページフォールトを意味すると確信しています(メモリがマークされていない場合) – Lockhead