三年目の初学者(1問目)
問題
topcoder SRM 600 div2 easy 250
https://community.topcoder.com/stat?c=problem_statement&pm=12824&rd=15712
使用言語
C++
所感
・全探索
・変数名大事。
コード
#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 TheShuttles { public: int getLeastCost(vector<int> cnt, int baseCost, int seatCost) { int ans = 10e7, Cost_tmp; for (int seat = 1; seat <= 100; seat++) { Cost_tmp = 0; for (int i = 0; i < cnt.size(); i++) { Cost_tmp += cnt[i] % seat == 0 ? cnt[i] / seat * (baseCost + seatCost * seat) : (cnt[i] / seat + 1) * (baseCost + seatCost * seat); } ans = min(ans, Cost_tmp); } return ans; } }; // CUT begin ifstream data("TheShuttles.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 do_test(vector<int> cnt, int baseCost, int seatCost, int __expected) { time_t startClock = clock(); TheShuttles *instance = new TheShuttles(); int __result = instance->getLeastCost(cnt, baseCost, seatCost); double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC; delete instance; if (__result == __expected) { 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; vector<int> cnt; from_stream(cnt); int baseCost; from_stream(baseCost); int seatCost; from_stream(seatCost); next_line(); int __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(cnt, baseCost, seatCost, __answer)) { passed++; } } if (mainProcess) { cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl; int T = time(NULL) - 1517574098; double PT = T / 60.0, TT = 75.0; cout << "Time : " << T / 60 << " minutes " << T % 60 << " secs" << endl; cout << "Score : " << 250 * (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 << "TheShuttles (250 Points)" << endl << endl; } return run_test(mainProcess, cases, argv[0]); } // CUT end