-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
163 lines (136 loc) · 4.79 KB
/
Copy pathProgram.cs
File metadata and controls
163 lines (136 loc) · 4.79 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
156
157
158
159
160
161
162
163
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
namespace Hashcode
{
class Program
{
static void Main(string[] args)
{
string filename = "d_tough_choices.txt";
//find the books,lib,days
string[] file = System.IO.File.ReadAllLines(filename);
// first line
string[] first_line = file[0].Split(' ');
int nb = int.Parse(first_line[0]);
int nl = int.Parse(first_line[1]);
int nd = int.Parse(first_line[2]);
//finds the score of the books
string[] books_scr_string = file[1].Split(' ');
int[] books_scr = new int[nb];
for (int i = 0; i < nb; i++)
{
books_scr[i] = int.Parse(books_scr_string[i]);
}
int[,] libs = new int[nl, 3];
List<int>[] lib_books = new List<int>[nl];
//int[,] lib_books = new int[nl, nb];
int c = 2;
for (int i = 0; i < nl; i++)
{
string[] libinfo = file[c].Split(' ');
libs[i, 0] = Convert.ToInt32(libinfo[0]);
libs[i, 1] = Convert.ToInt32(libinfo[1]);
libs[i, 2] = Convert.ToInt32(libinfo[2]);
c++;
lib_books[i] = new List<int>();
string[] libboosk = file[c].Split(' ');
for (int k = 0; k < libboosk.Length; k++)
{
int tb = Convert.ToInt32(libboosk[k]);
lib_books[i].Add(tb);
}
c++;
}
float[] lib_value = new float[nl];
for (int i = 0; i < nl; i++)
{
int sum_book_scr = 0;
foreach (int item in lib_books[i])
{
sum_book_scr += books_scr[item];
}
lib_value[i] = (sum_book_scr / (float)libs[i, 0]) * libs[i, 2];
}
List<int> counted = new List<int>();
int rdays = nd;
int pl = 0;
int lb = 0;
List<int>[] listbook = new List<int>[nl];
List<List<int>> selectedBooks = new List<List<int>>();
while (rdays > 0 && lb < nl)
{
int r = GetMinLocation(libs, lib_value, nl, counted);
List<int> foundbooks = Getbooks(lib_books[r], libs[r, 2], rdays);
if(foundbooks.Count() >0)
{
counted.Add(r);
rdays = rdays - libs[r, 1];
selectedBooks.Add(foundbooks);
pl++;
}
Console.WriteLine("Processed {0}", lb);
lb++;
}
StreamWriter sw = new StreamWriter(filename+".out");
sw.WriteLine(pl);
for (int i = 0; i < counted.Count(); i++)
{
sw.WriteLine(string.Format("{0} {1}", counted[i], selectedBooks[i].Count()));
foreach (int item in selectedBooks[i])
{
sw.Write(string.Format("{0} ", item));
}
sw.WriteLine();
}
sw.Close();
//Console.ReadLine();
}
static List<int> Getbooks(List<int> books, int perday, int rdays)
{
List<int> sbooks = new List<int>();
int c = 0;
for (int i = 0; i < rdays; i++)
{
for (int j = 0; j < perday; j++)
{
if (c < books.Count())
sbooks.Add(books[c]);
c++;
}
}
return sbooks;
}
static int GetMinLocation(int[,] libs, float[] lib_value, int nl, List<int> counted)
{
int min = int.MaxValue;
float max = float.MinValue;
int l = -1;
for (int i = 0; i < nl; i++)
{
if (counted.Contains(i))
continue;
if (libs[i, 2] == min)
{
if (max < lib_value[i])
{
max = lib_value[i];
l = i;
}
}
else if (libs[i, 2] < min)
{
min = libs[i, 2];
max = lib_value[i];
l = i;
}
}
return l;
}
}
}