-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashcode.cpp
157 lines (155 loc) · 3.24 KB
/
hashcode.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
#include<stdio.h>
#include<fstream>
#include<iostream>
#include<ctype.h>
#include<vector>
#include<math.h>
#define ifile "input/f_libraries_of_the_world.txt"
#define ofile "output/outf.txt"
#define iter 1
std::size_t maxsize = 100000;
static long int tdays,tdays_c,tbooks,tlibs,libcount=0;
struct books
{
long int id;
long int score;
};
struct books *book= new struct books[100000];
void swap(int* a, int* b) // utility swap function
{
int t = *a;
*a = *b;
*b = t;
}
class library
{
public:
int size, days,limit,less;
long double finscore;
int *bookid= new int[100000];
void lscore() //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;
}
void sort() //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
}
}
}
}
void dupkill(){
int i;
for (i=0;i<less;i++){
book[bookid[i]].score = 0;
}
}
};
library *lib = new library[maxsize];
int *libord= new int[maxsize];
void lsort(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]);
}
void lcount(){ //no of libs to output
long long int 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
}
void input() //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();
}
void output() // 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();
}
int main(){
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();
return 0;
}