old_mile.cpp and adl.cpp
This commit is contained in:
59
cpp/adl.cpp
Normal file
59
cpp/adl.cpp
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
VIM: let b:cf5build="clang -std=c++20 -O2 -pthread -lstdc++ -I. {SRC} -o {OUT}"
|
||||||
|
VIM: let b:cf5run="{OUT}"
|
||||||
|
VIM-: let b:cppflags=g:Iboost.g:Itbb
|
||||||
|
VIM-: let b:ldflags=g:Lboost.g:Ltbb
|
||||||
|
VIM-: let b:ldlibpath=g:Bboost.g:Btbb
|
||||||
|
*/
|
||||||
|
#include <iostream>
|
||||||
|
#include <exception>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace ttt {
|
||||||
|
struct s {
|
||||||
|
};
|
||||||
|
void print( const s& t ){
|
||||||
|
std::cout << "ttt::print" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace C
|
||||||
|
{
|
||||||
|
template <typename T>
|
||||||
|
void print( T t ){
|
||||||
|
std::cout << "C::print" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void call_print( const T& t ){
|
||||||
|
print( t );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int main ( void )
|
||||||
|
{try{
|
||||||
|
auto begin = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
|
|
||||||
|
//......
|
||||||
|
C::call_print(ttt::s{});
|
||||||
|
|
||||||
|
auto end = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> seconds = end - begin;
|
||||||
|
std::cout << "Time: " << seconds.count() << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
catch ( const std::exception& e )
|
||||||
|
{
|
||||||
|
std::cerr << std::endl
|
||||||
|
<< "std::exception(\"" << e.what() << "\")." << std::endl;
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
catch ( ... )
|
||||||
|
{
|
||||||
|
std::cerr << std::endl
|
||||||
|
<< "unknown exception." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}}
|
||||||
|
|
||||||
185
puzzles/training/old_mile.cpp
Normal file
185
puzzles/training/old_mile.cpp
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
O,1,11.3,4781,S, 1,5
|
||||||
|
O,6,53.8,1589,S, 3,7
|
||||||
|
O,4,40.7,9544,B, 5,9
|
||||||
|
O,7,14.6,858,B, 6,10
|
||||||
|
O,3,32.5,6971,S, 7,11
|
||||||
|
O,5,19.5,8747,S, 7,11
|
||||||
|
O,0,95.6,1206,S, 10,14
|
||||||
|
O,2,57.5,2178,B, 11,15
|
||||||
|
|
||||||
|
|
||||||
|
Q,1
|
||||||
|
Q,6
|
||||||
|
Q,10
|
||||||
|
Q,12
|
||||||
|
Q,14
|
||||||
|
Q,14
|
||||||
|
Q,15
|
||||||
|
Q,15
|
||||||
|
|
||||||
|
|
||||||
|
R,3384
|
||||||
|
R,3384
|
||||||
|
R,3384
|
||||||
|
R,3384
|
||||||
|
R,3384
|
||||||
|
R,11133
|
||||||
|
R,0
|
||||||
|
R,16576
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <random>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// Please disable before submitting.
|
||||||
|
constexpr bool kDebug = false;
|
||||||
|
|
||||||
|
struct order {
|
||||||
|
uint64_t order_token;
|
||||||
|
uint64_t shares;
|
||||||
|
double price;
|
||||||
|
bool side; // false = sell, true = buy
|
||||||
|
uint64_t created_at;
|
||||||
|
uint64_t cancelled_or_executed_at;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct query {
|
||||||
|
uint64_t query_time;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<uint64_t> your_solution(const vector<order> &orders,
|
||||||
|
const vector<query> &queries) {
|
||||||
|
|
||||||
|
std::vector<uint64_t> resp(queries.size(), 0);
|
||||||
|
|
||||||
|
std::vector<std::pair<query, size_t>> sorted_queries;
|
||||||
|
sorted_queries.reserve(queries.size());
|
||||||
|
for (size_t i = 0; i < queries.size(); ++i ){
|
||||||
|
sorted_queries.emplace_back(queries[i],i);
|
||||||
|
}
|
||||||
|
std::sort(sorted_queries.begin(), sorted_queries.end(),
|
||||||
|
[](const auto& op1, const auto& op2){
|
||||||
|
return op1.first.query_time < op2.first.query_time;
|
||||||
|
});
|
||||||
|
|
||||||
|
std::vector<order> sorted_orders(orders.begin(), orders.end());
|
||||||
|
std::sort(sorted_orders.begin(), sorted_orders.end(),
|
||||||
|
[](const auto& op1, const auto& op2){
|
||||||
|
return op1.created_at < op2.created_at;
|
||||||
|
});
|
||||||
|
|
||||||
|
std::vector<std::vector<order>::iterator> current_orders;
|
||||||
|
auto current_orders_cmp = [](const auto& op1, const auto& op2){
|
||||||
|
return op1->cancelled_or_executed_at < op2->cancelled_or_executed_at;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
auto ot = sorted_orders.begin();
|
||||||
|
for (const auto& q : sorted_queries){
|
||||||
|
while ( !current_orders.empty()
|
||||||
|
&& current_orders.front()->cancelled_or_executed_at < q.first.query_time){
|
||||||
|
// remove from heap
|
||||||
|
std::pop_heap(current_orders.begin(), current_orders.end(),
|
||||||
|
current_orders_cmp);
|
||||||
|
current_orders.resize(current_orders.size()-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// std::cout << "2222222" << std::endl;
|
||||||
|
|
||||||
|
while (ot != sorted_orders.end() && ot->created_at <= q.first.query_time){
|
||||||
|
if ( ot->cancelled_or_executed_at >= q.first.query_time){
|
||||||
|
current_orders.push_back(ot);
|
||||||
|
std::push_heap(current_orders.begin(), current_orders.end(),
|
||||||
|
current_orders_cmp);
|
||||||
|
}
|
||||||
|
++ot;
|
||||||
|
}
|
||||||
|
|
||||||
|
// std::cout << "33333" << std::endl;
|
||||||
|
|
||||||
|
uint64_t shares_live = 0;
|
||||||
|
for (const auto cot : current_orders){
|
||||||
|
shares_live += cot->shares;
|
||||||
|
}
|
||||||
|
resp[q.second] = shares_live;
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Please do not change any code below. //
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
std::pair<vector<order>, vector<query>> gen_input(int seed) {
|
||||||
|
mt19937 gen(seed);
|
||||||
|
|
||||||
|
const uint64_t order_len = 1 << (gen() % 20);
|
||||||
|
const uint64_t time_len = 1 << (gen() % 20);
|
||||||
|
|
||||||
|
vector<order> orders;
|
||||||
|
for (uint64_t i = 0; i < order_len; ++i) {
|
||||||
|
const double duration = std::max(1.0, time_len / std::log2(time_len));
|
||||||
|
const uint64_t start = gen() % time_len;
|
||||||
|
const uint64_t end = std::min<int>(start + duration, time_len);
|
||||||
|
order o{
|
||||||
|
.order_token = i,
|
||||||
|
.shares = static_cast<uint64_t>(gen() % 10000),
|
||||||
|
.price = static_cast<double>(gen() % 1000) / 10,
|
||||||
|
.side = gen() % 2 ? false : true,
|
||||||
|
.created_at = start,
|
||||||
|
.cancelled_or_executed_at = end,
|
||||||
|
};
|
||||||
|
orders.emplace_back(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<query> queries;
|
||||||
|
for (uint64_t i = 0; i < order_len; ++i) {
|
||||||
|
query q{
|
||||||
|
.query_time = static_cast<uint64_t>(gen() % time_len),
|
||||||
|
};
|
||||||
|
queries.emplace_back(q);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {std::move(orders), std::move(queries)};
|
||||||
|
}
|
||||||
|
|
||||||
|
int hash_result(const std::vector<uint64_t> &answers) {
|
||||||
|
std::hash<uint64_t> hasher;
|
||||||
|
uint64_t result = 0;
|
||||||
|
for (auto &a : answers) {
|
||||||
|
result = hasher(a) ^ (result << 1);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test harness generates an input to and hashes the output from your
|
||||||
|
// solution. Please do not change any code below this line.
|
||||||
|
int solution(int seed) {
|
||||||
|
auto input = gen_input(seed);
|
||||||
|
bool debug = kDebug && input.first.size() < 100 && input.second.size() < 100;
|
||||||
|
if (debug) {
|
||||||
|
for (auto &o : input.first) {
|
||||||
|
std::cerr << "O," << o.order_token << "," << o.price << "," << o.shares
|
||||||
|
<< "," << (o.side ? 'B' : 'S') << "," << o.created_at << ","
|
||||||
|
<< o.cancelled_or_executed_at << "\n";
|
||||||
|
}
|
||||||
|
for (auto &q : input.second) {
|
||||||
|
std::cerr << 'Q' << "," << q.query_time << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto answers = your_solution(input.first, input.second);
|
||||||
|
|
||||||
|
if (debug) {
|
||||||
|
for (auto &a : answers) {
|
||||||
|
std::cerr << "R," << a << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hash_result(answers);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user