-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodejam_2013_1B_A_Osmos.cpp
119 lines (85 loc) · 2.34 KB
/
codejam_2013_1B_A_Osmos.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
//============================================================================
// Name : codejam.cpp
// Author : Michal Simon
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <fstream>
#include <time.h>
#include <math.h>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int addMote(int A, int x) {
return pow(2.0, x) * (A - 1) + 1;
}
int howMany(double A, double mote) {
return floor( log( (mote - 1) / (A - 1) ) / log(2.0) ) + 1;
}
int eat(int A, vector<int>::iterator begin, vector<int>::iterator end) {
int b = *begin;
// if it is the end ...
if(begin == end) return 0;
// is A is greater than the smallest one, eat it and proceed further
if (*begin < A) return eat(A + *begin, begin + 1, end);
// if we would remove the rest we would need to do ...
int remove = end - begin;
// if A equals 1 there's nothing else we can do
if (A == 1) return remove;
// A equals the next element
if (*begin == A) {
// add one mote
A += A - 1;
int add = eat(A + *begin, begin + 1, end) + 1;
// return whatever is smaller
return add < remove ? add : remove;
}
// how many motes do we need to add
int add = howMany(A, *begin);
// add ..
A = addMote(A, add);
// and then we still need to eat the rest ...
add += eat(A + *begin, begin + 1, end);
// return whatever is smaller
return add < remove ? add : remove;
}
int solve (ifstream& fin) {
// first read the case
int A = 0, size = 0;
fin >> A;
fin >> size;
vector<int> motes;
for (int i = 0; i < size; i++) {
int tmp;
fin >> tmp;
motes.push_back(tmp);
}
// then sort the motes
sort(motes.begin(), motes.end());
// and eat them up ...
return eat(A, motes.begin(), motes.end());
}
void doit () {
int count = 0;
ifstream fin ("/home/simonm/Pobrane/A-large-practice(2).in");
// ofstream fout("/home/simonm/prog2.out");
fin >> count;
for (int i = 1; i <= count; i++) {
int result = solve(fin);
cout << "Case #" << i << ": " << result << endl;
// fout << "Case #" << i << ": " << result << endl;
}
fin.close();
// fout.flush();
// fout.close();
}
int main() {
time_t start = time(0);
doit();
time_t stop = time(0);
// cout << endl << stop - start << endl;
return 0;
}