제출 #659292

#제출 시각아이디문제언어결과실행 시간메모리
659292marvinthangSalesman (IOI09_salesman)C++17
60 / 100
763 ms51260 KiB
/************************************* * author: marvinthang * * created: 17.11.2022 15:31:48 * *************************************/ #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define div ___div #define left ___left #define right ___right #define TIME (1.0 * clock() / CLOCKS_PER_SEC) #define MASK(i) (1LL << (i)) #define FULL(i) (MASK(i) - 1) #define BIT(x, i) ((x) >> (i) & 1) #define __builtin_popcount __builtin_popcountll #define ALL(v) (v).begin(), (v).end() #define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i) #define FORE(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i) #define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i) #define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i) #define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u) #define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u) #define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; } template <class T> print_op(vector <T>) { out << '{'; for (size_t i = 0; i + 1 < u.size(); ++i) out << u[i] << ", "; if (!u.empty()) out << u.back(); return out << '}'; } template <class U, class V> scan_op(pair <U, V>) { return in >> u.fi >> u.se; } template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.fi << ", " << u.se << ')'; } template <class A, class B> bool minimize(A &a, B b) { if (a > b) { a = b; return true; } return false; } template <class A, class B> bool maximize(A &a, B b) { if (a < b) { a = b; return true; } return false; } // end of template const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0); const int dir[] = {1, 0, -1, 0, 1, 1, -1, -1, 1}; // {2, 1, -2, -1, -2, 1, 2, -1, 2}; const int MAX = 5e5 + 5; const int INF = 2e9 + 7; struct TradeFair { int day, position, profitability; TradeFair(int d = 0, int l = 0, int p = 0): day(d), position(l), profitability(p) {} }; int numTradeFair, upStreamCost, downStreamCost, homePos; TradeFair tf[MAX]; vector <int> inDay[MAX]; void init(void) { cin >> numTradeFair >> upStreamCost >> downStreamCost >> homePos; FOR(i, 1, numTradeFair) { cin >> tf[i].day >> tf[i].position >> tf[i].profitability; inDay[tf[i].day].push_back(i); } } struct Node { int maxUp, maxDown; Node(): maxUp(-INF),maxDown(-INF) {} Node(int dp, int pos) { maxUp = dp - pos * upStreamCost; maxDown = dp + pos * downStreamCost; } Node operator + (const Node &other) const { Node res; res.maxUp = max(maxUp, other.maxUp); res.maxDown = max(maxDown, other.maxDown); return res; } } st[MAX << 2]; void update(int i, int l, int r, int p, int dp) { if (l == r) { st[i] = Node(dp, l); return; } int m = l + r >> 1; if (p <= m) update(i << 1, l, m, p, dp); else update(i << 1 | 1, m + 1, r, p, dp); st[i] = st[i << 1] + st[i << 1 | 1]; } Node get(int i, int l, int r, int u, int v) { if (l > v || r < u) return Node(); if (u <= l && r <= v) return st[i]; int m = l + r >> 1; return get(i << 1, l, m, u, v) + get(i << 1 | 1, m + 1, r, u, v); } int dp[MAX]; void process(void) { int maxPosition = homePos, maxDay = 0; FOR(i, 1, numTradeFair) { maximize(maxPosition, tf[i].position); maximize(maxDay, tf[i].day); } tf[++numTradeFair] = TradeFair(++maxDay, homePos, 0); inDay[maxDay].push_back(numTradeFair); update(1, 1, maxPosition, homePos, 0); FOR(d, 1, maxDay) if (!inDay[d].empty()) { for (int i: inDay[d]) { dp[i] = get(1, 1, maxPosition, 1, tf[i].position).maxDown - tf[i].position * downStreamCost; maximize(dp[i], get(1, 1, maxPosition, tf[i].position, maxPosition).maxUp + tf[i].position * upStreamCost); dp[i] += tf[i].profitability; } sort(inDay[d].begin(), inDay[d].end(), [&] (int i, int j) { return tf[i].position < tf[j].position; }); vector <int> f(inDay[d].size(), -INF), g(inDay[d].size(), -INF); int best = -INF; REP(p, inDay[d].size()) { int i = inDay[d][p]; best = max(best, tf[i].position * (downStreamCost + upStreamCost)); f[p] = best + dp[i] - tf[i].position * upStreamCost; best += tf[i].profitability; } best = -INF; FORD(p, (int) inDay[d].size() - 1, 0) { int i = inDay[d][p]; best = max(best, tf[i].position * (-upStreamCost - downStreamCost)); g[p] = best + dp[i] + tf[i].position * downStreamCost; best += tf[i].profitability; } best = -INF; REP(p, inDay[d].size()) { int i = inDay[d][p]; maximize(best, f[p]); maximize(dp[i], best - tf[i].position * downStreamCost); } best = -INF; FORD(p, (int) inDay[d].size() - 1, 0) { int i = inDay[d][p]; maximize(best, g[p]); maximize(dp[i], best + tf[i].position * upStreamCost); update(1, 1, maxPosition, tf[i].position, dp[i]); } } cout << dp[numTradeFair] << '\n'; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); // cout.tie(nullptr); file("salesman"); init(); process(); cerr << "Time elapsed: " << TIME << " s.\n"; return (0^0); }

컴파일 시 표준 에러 (stderr) 메시지

salesman.cpp: In function 'void update(int, int, int, int, int)':
salesman.cpp:80:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   80 |  int m = l + r >> 1;
      |          ~~^~~
salesman.cpp: In function 'Node get(int, int, int, int, int)':
salesman.cpp:89:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   89 |  int m = l + r >> 1;
      |          ~~^~~
salesman.cpp: In function 'int main()':
salesman.cpp:27:67: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 | #define          file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:151:2: note: in expansion of macro 'file'
  151 |  file("salesman");
      |  ^~~~
salesman.cpp:27:100: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 | #define          file(name)  if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
salesman.cpp:151:2: note: in expansion of macro 'file'
  151 |  file("salesman");
      |  ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...