Initial commit
This commit is contained in:
87
2019/republic/11_d1/bare.cpp
Normal file
87
2019/republic/11_d1/bare.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
|
||||
int getMinRest(int L, int lengths[], int K) {
|
||||
for (int i = 0; i < K; i++) {
|
||||
if (L % lengths[i] == 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
sort(lengths, lengths + K);
|
||||
for (int i = 1; i < K; i++) {
|
||||
if (lengths[i] - lengths[i - 1] == 1 && lengths[i] * lengths[i - 1] < L) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (K < 2) {
|
||||
return L % lengths[0];
|
||||
}
|
||||
|
||||
|
||||
set<int> processed_lengths;
|
||||
set<int> new_lengths;
|
||||
unordered_map<int, bool> detected_lengths;
|
||||
|
||||
for (int i = 0; i < K; i++) {
|
||||
processed_lengths.insert(lengths[i]);
|
||||
new_lengths.insert(lengths[i]);
|
||||
detected_lengths[lengths[i]] = true;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
// cout << "Processed: " << processed_lengths.size() << "; New: " << new_lengths.size() << endl;
|
||||
set<int> found_lengths;
|
||||
|
||||
for (auto i: processed_lengths) {
|
||||
for (auto j: new_lengths) {
|
||||
int new_len = i + j;
|
||||
if (new_len <= L && detected_lengths[new_len] == false) {
|
||||
found_lengths.insert(new_len);
|
||||
detected_lengths[new_len] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
processed_lengths.insert(new_lengths.begin(), new_lengths.end());
|
||||
if (found_lengths.size() == 0) {
|
||||
break;
|
||||
}
|
||||
new_lengths = found_lengths;
|
||||
}
|
||||
|
||||
auto m = processed_lengths.end();
|
||||
m--;
|
||||
return L - *m;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
|
||||
int L, K;
|
||||
input_stream >> L;
|
||||
input_stream >> K;
|
||||
|
||||
int lengths[K];
|
||||
for (int i = 0; i < K; i++) {
|
||||
input_stream >> lengths[i];
|
||||
}
|
||||
|
||||
cout << getMinRest(L, lengths, K) << endl;
|
||||
|
||||
input_stream.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user