- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhashcode.cpp
More file actions
Latest commit
157 lines (155 loc) · 3.24 KB
/
Copy pathhashcode.cpp
File metadata and controls
157 lines (155 loc) · 3.24 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
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
#include<stdio.h>
#include<fstream>
#include<iostream>
#include<ctype.h>
#include<vector>
#include<math.h>
#defineifile"input/f_libraries_of_the_world.txt"
#defineofile"output/outf.txt"
#defineiter1
std::size_t maxsize = 100000;
staticlongint tdays,tdays_c,tbooks,tlibs,libcount=0;
structbooks
{
longint id;
longint score;
};
structbooks *book= newstructbooks[100000];
voidswap(int* a, int* b) // utility swap function
{
int t = *a;
*a = *b;
*b = t;
}
classlibrary
{
public:
int size, days,limit,less;
longdouble finscore;
int *bookid= newint[100000];
voidlscore() //score function
{
finscore=0;
if (tdays<=days)
return;
if (size>((tdays-days)*limit))
less=(tdays-days)*limit;
else
less=size;
int i;
for(i=0;i<less;i++)
finscore+=book[bookid[i]].score;
finscore= finscore/days;
}
voidsort() //sort function
{
int i, j, temp;
for(i=0;i<size-1;i++) //implements bubble sort
{
for(j=0;j<size-i-1;++j)
{
if(book[bookid[j]].score<book[bookid[j+1]].score)
{
swap(&bookid[j],&bookid[j+1]); //swapping
}
}
}
}
voiddupkill(){
int i;
for (i=0;i<less;i++){
book[bookid[i]].score = 0;
}
}
};
library *lib = new library[maxsize];
int *libord= newint[maxsize];
voidlsort(int a) //sort library function
{
int i, j, temp=a;
for(i=a+1;i<tlibs-1;i++)
{
if(lib[libord[i]].finscore>=lib[libord[temp]].finscore)
temp = i;
}
if(temp!=a)
swap(&libord[a],&libord[temp]);
}
voidlcount(){ //no of libs to output
longlongint i=0,dc=0; //dc is daycount
while(dc<=tdays_c){
dc+=lib[libord[i]].days;
i++;
}
libcount= (i>tlibs)?tlibs:i; // prevents errors for file A
}
voidinput() //input function
{
int i,j;
std::ifstream fin1;
fin1.open(ifile);
fin1 >> tbooks;
fin1 >> tlibs;
fin1 >> tdays;
for(i = 0; i < tbooks; i++)
{
book[i].id = i;
fin1 >> book[i].score;
}
for(i = 0; i < tlibs; i++)
{
fin1 >> lib[i].size;
fin1 >> lib[i].days;
fin1 >> lib[i].limit;
for(j = 0; j < lib[i].size; j++)
{
fin1 >> lib[i].bookid[j];
}
}
fin1.close();
}
voidoutput() // output function
{
int i,j;
std::ofstream fout1;
fout1.open(ofile);
fout1<<libcount<<"\n";
for(i=0;i<libcount;i++)
{
fout1<<libord[i]<<""<<(lib[libord[i]].less);
fout1<<"\n";
for(j=0;j<lib[libord[i]].less;j++)
{
fout1<<lib[libord[i]].bookid[j]<<"";
}
fout1<<"\n";
}
fout1.close();
}
intmain(){
input();
int i,j;
tdays_c =tdays;
for(i=0;i<tlibs;i++){
lib[i].sort();
lib[i].lscore();
}
for(i=0;i<tlibs;i++) //initializes the array
libord[i]=i;
for(i=0;i<tlibs;i++){
lsort(i);
if(lib[libord[i]].finscore==0)
break;
lib[libord[i]].dupkill();
tdays = tdays - lib[libord[i]].days;
for(j=i+1;j<tlibs;j++){
lib[libord[j]].sort();
lib[libord[j]].lscore();
}
printf("Selected %d libraries out of %ld libraries\n",i+1,tlibs);
}
lcount(); // Counts the number of libraries to write to the file
printf("output done\n");
output();
return0;
}