Initial commit
This commit is contained in:
152
2019/republic/12_d2/cercetari.cpp
Normal file
152
2019/republic/12_d2/cercetari.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
|
||||
set<char> readCode() {
|
||||
char str[20] = {};
|
||||
input_stream >> str;
|
||||
|
||||
set<char> code = set<char>(begin(str), end(str));
|
||||
return code;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t getIntersection(set<char> set1, set<char> set2) {
|
||||
set<char> result;
|
||||
set_intersection(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(result, result.begin()));
|
||||
return result.size() - 1;
|
||||
}
|
||||
|
||||
|
||||
void task1() {
|
||||
uint16_t n;
|
||||
input_stream >> n;
|
||||
|
||||
uint16_t m;
|
||||
input_stream >> m;
|
||||
|
||||
uint16_t G;
|
||||
input_stream >> G;
|
||||
|
||||
set<char> monea_code = readCode();
|
||||
int16_t best_match = -1;
|
||||
vector<string> matches;
|
||||
matches.reserve(m * n);
|
||||
|
||||
for (uint16_t y = 0; y < n; y++) {
|
||||
for (uint16_t x = 0; x < m; x++) {
|
||||
char cat[20] = {};
|
||||
input_stream >> cat;
|
||||
set<char> cat_code = set<char>(begin(cat), end(cat));
|
||||
|
||||
uint8_t match = getIntersection(monea_code, cat_code);
|
||||
|
||||
if (match > best_match && match > G) {
|
||||
best_match = match;
|
||||
matches.clear();
|
||||
matches.push_back(to_string(y + 1) + " " + to_string(x + 1) + " " + string(cat));
|
||||
} else if (match == best_match) {
|
||||
matches.push_back(to_string(y + 1) + " " + to_string(x + 1) + " " + string(cat));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (best_match > 0) {
|
||||
output_stream << best_match << endl;
|
||||
for (auto i = matches.begin(); i != matches.end(); i++) {
|
||||
output_stream << *i << endl;
|
||||
}
|
||||
} else {
|
||||
output_stream << "MONEA NU ARE RUDE" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void task2() {
|
||||
uint16_t n;
|
||||
input_stream >> n;
|
||||
|
||||
uint16_t m;
|
||||
input_stream >> m;
|
||||
|
||||
uint16_t G;
|
||||
input_stream >> G;
|
||||
|
||||
set<char> monea_code = readCode();
|
||||
bool town[n][m] = {};
|
||||
|
||||
for (uint16_t y = 0; y < n; y++) {
|
||||
for (uint16_t x = 0; x < m; x++) {
|
||||
set<char> cat_code = readCode();
|
||||
|
||||
if (getIntersection(monea_code, cat_code) > G) {
|
||||
town[y][x] = true;
|
||||
} else {
|
||||
town[y][x] = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t sum[n][m] = {};
|
||||
uint16_t max_of_s, max_i, max_j = 0;
|
||||
|
||||
for (uint16_t i = 0; i < n; i++) {
|
||||
sum[i][m - 1] = town[i][m - 1];
|
||||
}
|
||||
|
||||
for (uint16_t j = 0; j < m; j++) {
|
||||
sum[n - 1][j] = town[n - 1][j];
|
||||
}
|
||||
|
||||
for (int16_t i = n-2; i >= 0; i--) {
|
||||
for (int16_t j = m-2; j >= 0; j--) {
|
||||
if (town[i][j]) {
|
||||
sum[i][j] = min(sum[i][j+1], min(sum[i+1][j], sum[i+1][j+1])) + 1;
|
||||
if (sum[i][j] >= max_of_s) {
|
||||
max_of_s = sum[i][j];
|
||||
max_i = i;
|
||||
max_j = j;
|
||||
}
|
||||
} else {
|
||||
sum[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (max_of_s > 0) {
|
||||
output_stream << (max_i + 1) << " " << (max_j + 1) << " " << max_of_s << endl;
|
||||
} else {
|
||||
output_stream << "MONEA NU ARE RUDE" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
output_stream.open("output.txt");
|
||||
|
||||
char task;
|
||||
input_stream >> task;
|
||||
|
||||
|
||||
if (task == '1') {
|
||||
task1();
|
||||
} else {
|
||||
task2();
|
||||
}
|
||||
|
||||
input_stream.close();
|
||||
output_stream.close();
|
||||
return 0;
|
||||
}
|
||||
BIN
2019/republic/12_d2/condition.pdf
Normal file
BIN
2019/republic/12_d2/condition.pdf
Normal file
Binary file not shown.
143
2019/republic/12_d2/pante.cpp
Normal file
143
2019/republic/12_d2/pante.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
bool checkAscending(string numStr) {
|
||||
char highestDigit = numStr[0];
|
||||
|
||||
for (uint8_t i = 1; i < numStr.length(); i++) {
|
||||
if (numStr[i] < highestDigit) {
|
||||
return false;
|
||||
}
|
||||
highestDigit = numStr[i];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint64_t n_choose_m(uint8_t n, uint8_t m) {
|
||||
if (m > n) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (m * 2 > n) {
|
||||
m = n - m;
|
||||
}
|
||||
|
||||
if (m == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64_t res = n;
|
||||
|
||||
for (int i = 2; i <= m; i++) {
|
||||
res *= n - i + 1;
|
||||
res /= i;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint64_t pos_combinations(uint8_t pos) {
|
||||
/**
|
||||
pos starts at 0 from right end of number
|
||||
10^pos <> 10^pos-1
|
||||
*/
|
||||
if (pos == 0) {
|
||||
return 0;
|
||||
} else {
|
||||
return n_choose_m(8 + pos, pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint64_t following_combinations(uint8_t first_num, uint8_t pos) {
|
||||
/**
|
||||
10^pos+1 <> first_num * 10^pos
|
||||
available = 10 - (first_num - 1) + allowed_repeats (pos)
|
||||
*/
|
||||
return n_choose_m(10 - first_num + pos, pos + 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint64_t preceeding_combinations(uint8_t first_num, uint8_t pos) {
|
||||
/**
|
||||
first_num * 10^pos <> 10^pos
|
||||
*/
|
||||
return pos_combinations(pos + 1) - following_combinations(first_num, pos);
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint64_t find_ascending(string start, string end) {
|
||||
uint64_t result = 0;
|
||||
const uint8_t end_len = end.length();
|
||||
|
||||
if (end_len == 1) {
|
||||
result += stoi(end) - stoi(start) + 1;
|
||||
} else if (start == "1") {
|
||||
char highest_digit = '1';
|
||||
bool sequence_integrity = true;
|
||||
|
||||
// Run this loop until a digit lower than the others is encountered
|
||||
uint8_t i;
|
||||
for (i = 0; i < end_len - 1; i++) {
|
||||
uint8_t pos = end_len - i - 1;
|
||||
result += pos_combinations(pos);
|
||||
|
||||
if (end[i] > highest_digit) {
|
||||
result += preceeding_combinations(end[i] - '0', pos) - preceeding_combinations(highest_digit - '0', pos);
|
||||
highest_digit = end[i];
|
||||
} else if (end[i] < highest_digit) {
|
||||
sequence_integrity = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
i++;
|
||||
for (i; i < end_len - 1; i++) {
|
||||
uint8_t pos = end_len - i - 1;
|
||||
result += pos_combinations(pos);
|
||||
}
|
||||
|
||||
if (sequence_integrity && end[end_len - 1] >= highest_digit) {
|
||||
result += end[end_len - 1] - highest_digit + 1;
|
||||
}
|
||||
} else {
|
||||
result += find_ascending("1", end) - find_ascending("1", start);
|
||||
|
||||
if (checkAscending(start)) {
|
||||
result += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
output_stream.open("output.txt");
|
||||
|
||||
string A;
|
||||
string B;
|
||||
|
||||
input_stream >> A;
|
||||
input_stream >> B;
|
||||
|
||||
cout << find_ascending(A, B) << endl;
|
||||
|
||||
input_stream.close();
|
||||
output_stream.close();
|
||||
return 0;
|
||||
}
|
||||
58
2019/republic/12_d2/vasul.cpp
Normal file
58
2019/republic/12_d2/vasul.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <deque>
|
||||
#include <numeric>
|
||||
|
||||
using namespace std;
|
||||
|
||||
ifstream input_stream;
|
||||
ofstream output_stream;
|
||||
|
||||
|
||||
uint64_t dequeSum(deque<uint64_t> &d) {
|
||||
return accumulate(d.begin(), d.end(), 0);
|
||||
}
|
||||
|
||||
int main() {
|
||||
input_stream.open("input.txt");
|
||||
output_stream.open("output.txt");
|
||||
|
||||
uint16_t P, T, N;
|
||||
input_stream >> P;
|
||||
input_stream >> T;
|
||||
input_stream >> N;
|
||||
|
||||
deque<uint64_t> bacteria;
|
||||
bacteria.push_front(P);
|
||||
|
||||
uint16_t i;
|
||||
uint64_t mature = 0;
|
||||
|
||||
// Populate list until death of first generation
|
||||
for (i=1; i < T && i <= N; i++) {
|
||||
// Reproduction
|
||||
bacteria.push_front(mature);
|
||||
|
||||
// Maturity
|
||||
mature += bacteria[1];
|
||||
}
|
||||
|
||||
// Run loop considering death until experiment is complete
|
||||
for(i; i <= N; i++) {
|
||||
// Reproduction
|
||||
bacteria.push_front(mature);
|
||||
|
||||
// Death
|
||||
mature -= bacteria.back();
|
||||
bacteria.pop_back();
|
||||
|
||||
// Maturity
|
||||
mature += bacteria[1];
|
||||
}
|
||||
|
||||
output_stream << dequeSum(bacteria) << endl;
|
||||
|
||||
input_stream.close();
|
||||
output_stream.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user