-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathks.cpp
More file actions
105 lines (80 loc) · 1.94 KB
/
Copy pathks.cpp
File metadata and controls
105 lines (80 loc) · 1.94 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
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <ctime>
#include <iomanip>
#include <algorithm>
std::vector<int> w;
std::vector<int> v;
std::vector<std::vector<int>> row;
clock_t timeTaken;
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Not enough args. Usage: ./ks.exe [infile]\n";
exit(0);
}
std::ifstream fs;
fs.open(argv[1], std::ifstream::in);
char line[2048];
int data;
std::string test;
size_t points;
int numOfPizzas;
fs.getline(line, 1024, ' ');
points = atoi(line);
fs.getline(line, 1024);
numOfPizzas = atoi(line);
timeTaken = clock();
int c;
std::string str;
while (fs.getline(line, 1024, ' '))
{
w.push_back(atoi(line));
}
timeTaken -= clock();
float totalTime = (float)timeTaken / CLOCKS_PER_SEC;
std::cout
<< "# of pizzas: " << w.size() << '\n'
<< "# max points: " << points << '\n'
<< "Read/insert of " << w.size() << " took " << std::setprecision(9) << timeTaken << " s\n";
v.resize(w.size(), 0);
printf("v.size(): %lu\n", v.size());
for (size_t i = 0; i < v.size(); ++i)
v[i] = i;
row.resize(w.size() + 1);
for (size_t i = 0; i < (w.size() + 1); ++i)
row[i].resize((points + 1), 0);
printf("row.size(): %lu\n", row.size());
printf("row[0].size(): %lu\n", row[0].size());
for (size_t i = 1; i <= w.size(); ++i)
{
for (size_t j = 0; j <= points; ++j)
{
printf("w[%lu][%lu]\n", i,j);
if (w[i] > j)
row[i][j] = row[i - 1][j];
else
row[i][j] = std::max(row[i - 1][j], (v[i] + row[i - 1][j - w[i]]));
}
}
/*
for (size_t i = 0; i < (w.size() + 1); ++i)
{
for (size_t j = 0; j < (points + 1); ++j)
{
if ((j == points))
{
std::cout << row[i][j] << '\n';
break;
}
std::cout << row[i][j] << ' ';
}
}*/
std::cout << row[w.size()][points];
fs.close();
return 0;
}