Skip to content

Instantly share code, notes, and snippets.

@riantkb
Last active April 20, 2019 18:52
Show Gist options
  • Save riantkb/179509c6a5cb597f909d7d3f04e8303b to your computer and use it in GitHub Desktop.
Save riantkb/179509c6a5cb597f909d7d3f04e8303b to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
using namespace std;
class EllysTicketPrices {
double eps = 1e-9;
public:
double getPrice(int N, vector<int> C, int target) {
double hi = 1e8;
double low = 0;
for (int loop = 0; loop < 10000; ++loop) {
double m = (hi+low)/2.0;
m = (long long)(m * 100 + 0.5 + eps) * 0.01;
double mm(m);
double sum = m;
for(int i=0;i<C.size();i++){
double p = m * (100 + C[i]);
p = (long long)(p + 0.5 + eps) * 0.01;
sum += p;
m = p;
}
double ave = (long long)(sum / N * 100 + 0.5 + eps) * 0.01;
if(ave + eps > target){
hi = mm;
}else{
low = mm;
}
}
return hi;
}
};
// CUT begin
ifstream data("EllysTicketPrices.sample");
string next_line() {
string s;
getline(data, s);
return s;
}
template <typename T> void from_stream(T &t) {
stringstream ss(next_line());
ss >> t;
}
void from_stream(string &s) {
s = next_line();
}
template <typename T> void from_stream(vector<T> &ts) {
int len;
from_stream(len);
ts.clear();
for (int i = 0; i < len; ++i) {
T t;
from_stream(t);
ts.push_back(t);
}
}
template <typename T>
string to_string(T t) {
stringstream s;
s << t;
return s.str();
}
string to_string(string t) {
return "\"" + t + "\"";
}
bool double_equal(const double &a, const double &b) { return b==b && a==a && fabs(b - a) <= 1e-9 * max(1.0, fabs(a) ); }
bool do_test(int N, vector<int> C, int target, double __expected) {
time_t startClock = clock();
EllysTicketPrices *instance = new EllysTicketPrices();
double __result = instance->getPrice(N, C, target);
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
delete instance;
if (double_equal(__expected, __result)) {
cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
return true;
}
else {
cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
cout << " Expected: " << to_string(__expected) << endl;
cout << " Received: " << to_string(__result) << endl;
return false;
}
}
int run_test(bool mainProcess, const set<int> &case_set, const string command) {
int cases = 0, passed = 0;
while (true) {
if (next_line().find("--") != 0)
break;
int N;
from_stream(N);
vector<int> C;
from_stream(C);
int target;
from_stream(target);
next_line();
double __answer;
from_stream(__answer);
cases++;
if (case_set.size() > 0 && case_set.find(cases - 1) == case_set.end())
continue;
cout << " Testcase #" << cases - 1 << " ... ";
if ( do_test(N, C, target, __answer)) {
passed++;
}
}
if (mainProcess) {
cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
int T = time(NULL) - 1555785769;
double PT = T / 60.0, TT = 75.0;
cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl;
cout << "Score : " << 1000 * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
}
return 0;
}
int main(int argc, char *argv[]) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
set<int> cases;
bool mainProcess = true;
for (int i = 1; i < argc; ++i) {
if ( string(argv[i]) == "-") {
mainProcess = false;
} else {
cases.insert(atoi(argv[i]));
}
}
if (mainProcess) {
cout << "EllysTicketPrices (1000 Points)" << endl << endl;
}
return run_test(mainProcess, cases, argv[0]);
}
// CUT end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment