-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashcode2018.cpp
220 lines (170 loc) · 4.63 KB
/
hashcode2018.cpp
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <chrono>
using namespace std;
struct ride {
int rideid;
int x,y,z,m,d,e,f,instant;
bool isvisited;
ride(int rideid,int x, int y, int z, int m, int d, int e, int f, int instant):rideid(rideid),x(x),y(y),z(z),m(m),d(d),e(e),f(f),instant(instant),isvisited(false){}
};
struct car {
int carid,instant,x,y;
vector <int> rides;
car(int carid,int x, int y, int instant):carid(carid),x(x),y(y),instant(instant){}
};
struct valuecomp
{
int carid, rideid, dur;
double value;
valuecomp(int carid, int rideid, double value, int dur):carid(carid),rideid(rideid),value(value),dur(dur){}
};
vector <ride> rides;
vector <car> cars;
vector <car> active_cars;
int takeMax(vector<valuecomp> & values)
{
int max = values[0].value;
int index = 0;
for (int i= 0; i < values.size(); i++)
{
if (values[i].value > max)
{
max = values[i].value;
index = i;
}
}
return index;
}
int rideNum, ridecheck, bonus, points;
void rideAssign()
{
vector <valuecomp> values;
int d0, d, e, dur, t0, f, carid, rideid, xpos, ypos;
double val;
int maxindex = 0;
int ridesize = rides.size();
int passivecheck;
valuecomp * newvalue;
// bool passive_car = false;
for (int i = 0; i < cars.size(); i++)
{
t0 = cars[i].instant;
passivecheck = t0;
for (int j = 0; j < ridesize; j++)
{
if (rides[j].isvisited == false)
{
d0 = abs(rides[j].x - cars[i].x) + abs(rides[j].y - cars[i].y); // distance to rider
d = rides[j].d; // distance of ride
e = rides[j].e;
f = rides[j].f;
//val = (double) d / (d0 + (t0 + d0 - e) + d); // We want to maximize this value
if ((d0+t0) >= e && (t0 + d0 + d) <= f)
{
dur = d0 + d; // time to pass
newvalue = new valuecomp(i,j,0,dur);
values.push_back(*newvalue);
break;
}
else if ((t0 + d0 < e) && (t0 + d0 + abs(t0 + d0 - e) + d) <= f) // erken gitmiþ
{
dur = d0 + abs(t0 + d0 - e) + d; // It is waiting this time to pass
newvalue = new valuecomp(i,j,0,dur);
values.push_back(*newvalue);
break;
}
}
}
if (passivecheck == 0)
{ break; }
}
if (values.size() != 0)
{
maxindex = 0;
carid = values[maxindex].carid;
rideid = values[maxindex].rideid;
dur = values[maxindex].dur;
xpos = rides[rideid].z;
ypos = rides[rideid].m;
rides[rideid].isvisited = true; // This ride is done
cars[carid].instant = cars[carid].instant + dur; // Time instant of car changed
cars[carid].x = xpos; // Car moved to x pos
cars[carid].y = ypos; // Car moved to y pos
// Points calculation
if (cars[carid].instant == rides[rideid].e)
{
points = points + bonus;
// cout << "BONUS" << endl;
}
d = abs(rides[rideid].z - rides[rideid].x) + abs(rides[rideid].m - rides[rideid].y);
points = points + d; // distance of ride
// cout << "rideid: " <<rideid << " assigned to car"<< carid << endl;
cars[carid].rides.push_back(rideid); // Adding ride to this car
ridecheck++;
}
}
int main ()
{
clock_t tStart = clock();
ifstream input;
input.open("input.txt");
string line;
int r,c,v, steps, x,y,z,m,d,e,f;
input >> r >> c >> v >> rideNum >> e >> f;
for (int i = 0; i < v; i++)
{
car newcar(i, 0, 0, 0); // carid, x,y, instant
cars.push_back(newcar);
}
// vector <ride> rides;
int rideid = 0;
ridecheck = 0;
int UNTIL_HERE = 6000;
while(rideid < UNTIL_HERE)
{
input >> x >> y >> z >> m >> e >> f;
d = abs(z - x) + abs(m - y);
ride newride(rideid, x,y,z,m,d,e,f,0); // 0 -> instant
rides.push_back(newride);
rideid++;
}
rides.pop_back();
rideid--;
for (int k = 0; k < rideNum; k++)
{
// cout<< k << ". Ride is assigning" << endl;
rideAssign();
}
ofstream output;
int ridesize;
output.open("output.txt");
for (int i = 0; i < cars.size(); i++)
{
ridesize = cars[i].rides.size();
output << ridesize << " ";
cout << ridesize << " ";
for(int j = 0; j < cars[i].rides.size(); j++)
{
output << cars[i].rides[j] << " ";
cout << cars[i].rides[j] << " ";
}
output << endl;
cout << endl;
}
/*
for (int i = 0; i < 5000 - cars.size(); i++)
{
output << 0 << " ";
// cout << 0 << " ";
output << endl;
// cout << endl;
}
*/
cout << endl << endl << "RIDES DONE: " << ridecheck << "/" << rideNum << " %" << (int)(((double)ridecheck/rideNum)*100) << endl;
cout << "Points: " << points << endl;
printf("Time taken: %.2fs\n", (double)(clock() - tStart)/CLOCKS_PER_SEC);
}