Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; void addmod(int &a, long long b) { a = (a + b); if (a >= 1000000007) a -= 1000000007; } void mulmod(int &a, long long b) { a = (a * b) % 1000000007; } template <class T> bool domin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <class T> bool domax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } int gi() { int a; scanf("%d", &a); return a; } long long gll() { long long a; scanf("%lld", &a); return a; } vector<pair<int, long long> > arr[1000007], dep[1000007]; int main() { int n = gi(), m = gi(), k = gi(); vector<long long> costarr(1000007, LLONG_MAX), costdep(1000007, LLONG_MAX); for (int i = 0; i < m; i++) { int dy = gi() - 1, di = gi(), ai = gi(); long long c = gi(); if (di == 0) { dep[dy - 1].push_back({ai - 1, c}); } else { arr[dy + 1].push_back({di - 1, c}); } } int tcnt = 0; long long rcost = 0; vector<bool> seen(n, false); vector<int> cost(n, 0); for (int i = 0; i < 1000007; i++) { for (auto j : arr[i]) { int ix = j.first; long long tco = j.second; if (seen[ix]) { if (tco < cost[ix]) { rcost += tco - cost[ix]; cost[ix] = tco; } } else { seen[ix] = true; tcnt++; rcost += tco; cost[ix] = tco; } } if (tcnt == n) { costarr[i] = rcost; } } fill(seen.begin(), seen.end(), false); fill(cost.begin(), cost.end(), 0); tcnt = 0, rcost = 0; for (int i = 1000007 - 1; i >= 0; i--) { for (auto j : dep[i]) { int ix = j.first; long long tco = j.second; if (seen[ix]) { if (tco < cost[ix]) { rcost += tco - cost[ix]; cost[ix] = tco; } } else { seen[ix] = true; tcnt++; rcost += tco; cost[ix] = tco; } } if (tcnt == n) { costdep[i] = rcost; } } long long ans = LLONG_MAX; for (int bg = 1; bg + k < 1000007; bg++) { if (costarr[bg] == LLONG_MAX || costdep[bg + k - 1] == LLONG_MAX) continue; domin(ans, costarr[bg] + costdep[bg + k - 1]); } if (ans == LLONG_MAX) ans = -1; cout << ans << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long INF = 1e15; const double eps = 1e-8; template <typename T> inline void read(T &x) { x = 0; int f = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') { f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= f; } const long long N = 100010, K = 1000010; long long Mx_d; struct FLIGHT { long long day, fr, to, co; inline void in() { read(day); read(fr); read(to); read(co); Mx_d = max(Mx_d, day); } inline void print() { printf("%lld %lld %lld %lld\n", day, fr, to, co); } bool operator<(const FLIGHT &a) const { return day < a.day; } } fl[N]; long long n, m, k; long long cost[N]; long long res[K]; signed main() { read(n); read(m); read(k); for (long long i = 1; i <= m; i++) { fl[i].in(); } sort(fl + 1, fl + m + 1); memset(cost, -1, sizeof cost); memset(res, -1, sizeof res); long long cur = m, cnt = 0, ret = 0; for (long long i = Mx_d; i >= 1; i--) { while ((fl[cur].to == 0 || fl[cur].day >= i) && cur > 1) { long long co = fl[cur].co, to = fl[cur].to; if (!to) { --cur; continue; } if (cost[to] == -1) { ++cnt; cost[to] = co; ret += co; } else if (cost[to] > co) { ret -= cost[to] - co; cost[to] = co; } --cur; } res[i] = (cnt == n ? ret : -1); } cur = 1; memset(cost, -1, sizeof cost); cnt = 0; ret = 0; long long ans = INF; for (long long i = 1; i <= m; i++) { while (fl[i].to != 0 && i <= m) { ++i; } if (i > m) break; long long day = fl[i].day, co = fl[i].co, to = fl[i].fr; if (cost[to] == -1) { ++cnt; cost[to] = co; ret += co; } else if (cost[to] > co) { ret -= cost[to] - co; cost[to] = co; } if (day + k + 1 <= 1000000 && cnt == n && res[day + k + 1] != -1) ans = min(ans, ret + res[day + k + 1]); } if (ans == INF) puts("-1"); else printf("%lld\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; struct node { int d, p, v; bool operator<(const node& rhs) const { return d < rhs.d; } } nd1[MAXN], nd2[MAXN]; int n, m, k; multiset<int> cm[MAXN], bk[MAXN]; int main() { cin >> n >> m >> k; int c1 = 0, c2 = 0; for (int i = 0; i < m; i++) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (t == 0) nd1[c1++] = (node){d, f, c}; else nd2[c2++] = (node){d, t, c}; } sort(nd1, nd1 + c1); sort(nd2, nd2 + c2); int pos = 0; int cmcnt = 0, bkcnt = 0; long long tot = 0, ans = pow(2, 60); while (nd2[pos].d <= nd1[0].d + k && pos < c2) pos++; for (int i = pos; i < c2; i++) { if (bk[nd2[i].p].size() == 0) bkcnt++; else tot -= *bk[nd2[i].p].begin(); bk[nd2[i].p].insert(nd2[i].v); tot += *bk[nd2[i].p].begin(); } if (bkcnt != n) { cout << -1 << endl; return 0; } int l = 0, r = pos, flag = 1; while (l < c1 && r < c2) { if (cm[nd1[l].p].size() == 0) cmcnt++; else tot -= *cm[nd1[l].p].begin(); cm[nd1[l].p].insert(nd1[l].v); tot += *cm[nd1[l].p].begin(); while (nd2[r].d <= nd1[l].d + k && r < c2) { if (bk[nd2[r].p].size() == 1) { flag = 0; break; } else tot -= *bk[nd2[r].p].begin(); auto pos = bk[nd2[r].p].lower_bound(nd2[r].v); bk[nd2[r].p].erase(pos); tot += *bk[nd2[r].p].begin(); r++; } if (!flag || r == c2) break; if (cmcnt == n) ans = min(ans, tot); l++; } if (ans == pow(2, 60)) cout << -1 << endl; else cout << ans << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long inf = 1e11 + 100; long long n, m, k; long long startt[1000005], endd[1000005]; long long mint[100005]; struct node { long long d, f, t, c; } p[100005]; bool cmp(node a, node b) { return a.d < b.d; } void init() { for (int i = 0; i <= n; ++i) mint[i] = inf; memset(startt, 0, sizeof(startt)); memset(endd, 0, sizeof(endd)); } int main() { while (~scanf("%lld %lld %lld", &n, &m, &k)) { init(); for (int i = 1; i <= m; ++i) scanf("%lld %lld %lld %lld", &p[i].d, &p[i].f, &p[i].t, &p[i].c); sort(p + 1, p + 1 + m, cmp); long long cost = inf * n; for (int i = 1; i <= m; ++i) { if (p[i].t == 0) { if (p[i].c < mint[p[i].f]) { cost -= mint[p[i].f]; mint[p[i].f] = p[i].c; cost += p[i].c; } } startt[p[i].d] = cost; } for (int i = 0; i <= n; ++i) mint[i] = inf; cost = inf * n; for (int i = m; i >= 1; --i) { if (p[i].f == 0) { if (p[i].c < mint[p[i].t]) { cost -= mint[p[i].t]; mint[p[i].t] = p[i].c; cost += p[i].c; } } endd[p[i].d] = cost; } int pos = 0; for (int i = 1; i <= 1000000; ++i) { if (startt[i] != 0) { pos = i; break; } } for (int i = 0; i < pos; ++i) startt[i] = inf * n; for (int i = pos; i <= 1000000; ++i) if (startt[i + 1] == 0) startt[i + 1] = startt[i]; for (int i = 1000000; i >= 1; --i) { if (endd[i] != 0) { pos = i; break; } } for (int i = 1000000; i > pos; --i) endd[i] = inf * n; for (int i = pos; i >= 1; --i) if (endd[i - 1] == 0) endd[i - 1] = endd[i]; long long minn = 1e18; for (int i = 1; i + k + 1 <= 1000000; ++i) if (minn > startt[i] + endd[i + k + 1] && startt[i] < inf && endd[i + k + 1] < inf) minn = min(minn, startt[i] + endd[i + k + 1]); if (minn >= inf) printf("-1\n"); else printf("%lld\n", minn); } }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > fron[1000010], bac[1000010]; long long minf[1000010], minb[1000010]; int mozf[1000010], mozb[1000010]; int minc[100010]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (f == 0) { bac[d].push_back({t, c}); } else { fron[d].push_back({f, c}); } } fill(minc, minc + 100010, 1000000000); for (int i = 1; i <= 1000000; i++) { mozf[i] = mozf[i - 1]; minf[i] = minf[i - 1]; for (auto x : fron[i]) { int node = x.first; int cost = x.second; if (cost < minc[node]) { if (minc[node] == 1000000000) { mozf[i]++; } else { minf[i] -= minc[node]; } minf[i] += cost; minc[node] = cost; } } } fill(minc, minc + 100010, 1000000000); for (int i = 1000000; i >= 1; i--) { mozb[i] = mozb[i + 1]; minb[i] = minb[i + 1]; for (auto x : bac[i]) { int node = x.first; int cost = x.second; if (cost < minc[node]) { if (minc[node] == 1000000000) { mozb[i]++; } else { minb[i] -= minc[node]; } minb[i] += cost; minc[node] = cost; } } } long long rez = 1000000000000000; for (int i = 1; i <= 1000000 - k - 1; i++) { if (mozf[i] == n && mozb[i + k + 1] == n) { rez = min(rez, minf[i] + minb[i + k + 1]); } } printf("%lld", (rez == 1000000000000000 ? -1 : rez)); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; long long n, prefix[1000005], suffix[1000005]; struct flight { int d, f, t, c; } a[100005]; int vis[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long q, m, k, h, ans = 0, sum = 0, p, x, y; cin >> n >> m >> k; for (int i = 0; i < m; ++i) { cin >> a[i].d >> a[i].f >> a[i].t >> a[i].c; } sort(a, a + m, [](flight& i, flight& j) { return i.d < j.d; }); fill_n(vis, 100005, -1); fill_n(prefix, 1000005, 1e16); fill_n(suffix, 1000005, 1e16); int ptr = 0, cnt = 0; for (int i = 1; i < 1000005 && ptr < m; ++i) { for (; a[ptr].d == i && ptr < m; ++ptr) { if (a[ptr].f == 0) continue; if (vis[a[ptr].f] == -1) { ++cnt; vis[a[ptr].f] = a[ptr].c; ans += a[ptr].c; } else if (vis[a[ptr].f] > a[ptr].c) { ans += a[ptr].c - vis[a[ptr].f]; vis[a[ptr].f] = a[ptr].c; } } if (cnt >= n) prefix[i] = ans; } fill_n(vis, 100005, -1); ptr = m - 1, cnt = 0, ans = 0; for (int i = 1000005; --i && ptr >= 0;) { for (; a[ptr].d == i && ptr >= 0; --ptr) { if (a[ptr].t == 0) continue; if (vis[a[ptr].t] == -1) { ++cnt; vis[a[ptr].t] = a[ptr].c; ans += a[ptr].c; } else if (vis[a[ptr].t] > a[ptr].c) { ans += a[ptr].c - vis[a[ptr].t]; vis[a[ptr].t] = a[ptr].c; } } if (cnt >= n) suffix[i] = ans; } ans = 1e17; for (int i = 1; i + k < 1000004; ++i) { ans = min(prefix[i] + suffix[i + k + 1], ans); } cout << (ans < 1e16 ? ans : -1); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; long long dp1[1000005], dp2[1000005], cost[1000005]; struct node { int r, m, c; bool operator<(const node &st) const { return r < st.r; } }; vector<node> from, to; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); while (m--) { int t, f, d, cc; scanf("%d%d%d%d", &t, &f, &d, &cc); if (d == 0) to.push_back({t, f, cc}); else from.push_back({t, d, cc}); } sort(from.begin(), from.end()); sort(to.begin(), to.end()); int cnt = 0, s = 0; long long sum = 0; for (int i = 1; i <= 1e6; i++) { while (cnt < to.size() && to[cnt].r == i) { if (cost[to[cnt].m] == 0) { cost[to[cnt].m] = to[cnt].c; s++; sum += to[cnt].c; } else if (to[cnt].c < cost[to[cnt].m]) { sum += (to[cnt].c - cost[to[cnt].m]); cost[to[cnt].m] = to[cnt].c; } cnt++; } if (s == n) dp1[i] = sum; } memset(cost, 0, sizeof(cost)); sum = 0; cnt = from.size() - 1, s = 0; for (int i = 1e6; i >= 1; i--) { while (cnt >= 0 && from[cnt].r == i) { if (cost[from[cnt].m] == 0) { cost[from[cnt].m] = from[cnt].c; s++; sum += from[cnt].c; } else if (from[cnt].c < cost[from[cnt].m]) { sum += (from[cnt].c - cost[from[cnt].m]); cost[from[cnt].m] = from[cnt].c; } cnt--; } if (s == n) dp2[i] = sum; } long long ans = -1; for (int i = 1; i <= 1e6; i++) { if (dp1[i] && i + k + 1 <= 1e6 && dp2[i + k + 1]) { if (ans == -1) ans = dp1[i] + dp2[i + k + 1]; else ans = min(ans, dp1[i] + dp2[i + k + 1]); } } printf("%lld\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 5; int n, m, k; struct data { int day, from, to, cost; } a[MAXN]; bool cmp(const data &a, const data &b) { return a.day < b.day; } long long min_so_far[MAXN]; long long min_gather[MAXN]; long long min_leave[MAXN]; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%d", &a[i].day, &a[i].from, &a[i].to, &a[i].cost); } sort(a + 1, a + 1 + m, cmp); fill(min_so_far, min_so_far + MAXN, 1e12); fill(min_gather, min_gather + MAXN, 1e12); long long cur = 1000000000000ll * n; for (int i = 1; i <= m; i++) { if (a[i].to == 0) { if (a[i].cost < min_so_far[a[i].from]) { cur += (a[i].cost - min_so_far[a[i].from]); min_so_far[a[i].from] = a[i].cost; min_gather[a[i].day] = cur; } } } for (int i = 1; i < MAXN; i++) { min_gather[i] = min(min_gather[i], min_gather[i - 1]); } fill(min_so_far, min_so_far + MAXN, 1e12); fill(min_leave, min_leave + MAXN, 1e12); cur = 1000000000000ll * n; for (int i = m; i >= 1; i--) { if (a[i].from == 0) { if (a[i].cost < min_so_far[a[i].to]) { cur += (a[i].cost - min_so_far[a[i].to]); min_so_far[a[i].to] = a[i].cost; min_leave[a[i].day] = cur; } } } for (int i = MAXN - 2; i >= 1; i--) { min_leave[i] = min(min_leave[i], min_leave[i + 1]); } long long res = 1e12; for (int i = 1; i + k + 1 <= 1000000; i++) { res = min(res, min_gather[i] + min_leave[i + k + 1]); } if (res == 1e12) { printf("-1\n"); } else { printf("%lld\n", res); } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; long long n, m, k, day, dep[1000010], arr[1000010], cost[1000010], cel, from, coss; long long mini[1000010], wylot[1000010], dolot[1000010], sum, inf = 1e7; vector<pair<long long, int> > tab; int ptr; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; inf *= inf; for (int a = 1; a <= m; a++) { cin >> day >> dep[a] >> arr[a] >> cost[a]; tab.push_back(make_pair(day, a)); } sort(tab.begin(), tab.end()); ptr = 0; for (int a = 1; a <= n; a++) mini[a] = inf; sum = n * inf; dolot[0] = n * inf; for (int a = 1; a <= 1e6; a++) { while (ptr < tab.size() && tab[ptr].first <= a) { cel = arr[tab[ptr].second]; from = dep[tab[ptr].second]; coss = cost[tab[ptr].second]; if (cel != 0) { ptr++; continue; } if (coss < mini[from]) { sum -= mini[from]; mini[from] = coss; sum += coss; } ptr++; } dolot[a] = sum; } reverse(tab.begin(), tab.end()); ptr = 0; for (int a = 1; a <= n; a++) mini[a] = inf; sum = n * inf; wylot[(int)1e6 + 1] = n * inf; for (int a = 1e6; a >= 0; a--) { while (ptr < tab.size() && tab[ptr].first >= a) { cel = arr[tab[ptr].second]; from = dep[tab[ptr].second]; coss = cost[tab[ptr].second]; if (cel == 0) { ptr++; continue; } if (coss < mini[cel]) { sum -= mini[cel]; mini[cel] = coss; sum += coss; } ptr++; } wylot[a] = sum; } long long ans = inf; for (int a = 1; a <= 1e6 - k; a++) { if (dolot[a - 1] < n * inf && wylot[a + k] < n * inf) ans = min(ans, dolot[a - 1] + wylot[a + k]); } if (ans == inf) cout << -1; else cout << ans << '\n'; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct node { int d, f, t; long long c; } a[100005]; int cmp(node a, node b) { return a.d < b.d; } long long INF = 0x3f3f3f3f3f3f; long long cost[100005]; int vis[100005]; int cnt1[1000006]; int cnt2[1000006]; long long sum2[1000006]; long long sum1[1000006]; int main() { int n, m, k; memset(cnt1, 0, sizeof(cnt1)); memset(cnt2, 0, sizeof(cnt2)); memset(vis, 0, sizeof(vis)); memset(cost, INF, sizeof(cost)); memset(sum1, 0, sizeof(sum1)); memset(sum2, 0, sizeof(sum2)); scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%I64d", &a[i].d, &a[i].f, &a[i].t, &a[i].c); } sort(a + 1, a + 1 + m, cmp); int pre = 0; for (int i = 1; i <= m; i++) { if (a[i].f == 0) continue; int f = a[i].f; int d = a[i].d; sum1[d] = sum1[pre]; cnt1[d] = cnt1[pre]; if (cost[f] > a[i].c) { if (vis[f] == 1) sum1[d] = sum1[d] - cost[f] + a[i].c; else sum1[d] += a[i].c; cost[f] = a[i].c; if (vis[f] == 0) { cnt1[d]++; vis[f] = 1; } } pre = d; } for (int i = 1; i <= 1000000; i++) { if (cnt1[i] == 0) cnt1[i] = cnt1[i - 1]; if (sum1[i] == 0) sum1[i] = sum1[i - 1]; } memset(vis, 0, sizeof(vis)); pre = 1000000; memset(cost, INF, sizeof(cost)); for (int i = m; i >= 1; i--) { if (a[i].t == 0) continue; int d = a[i].d; int t = a[i].t; sum2[d] = sum2[pre]; cnt2[d] = cnt2[pre]; if (cost[t] > a[i].c) { if (vis[t] == 1) sum2[d] = sum2[d] - cost[t] + a[i].c; else sum2[d] += a[i].c; cost[t] = a[i].c; if (vis[t] == 0) { cnt2[d]++; vis[t] = 1; } } pre = d; } for (int i = 1000000; i >= 0; i--) { if (cnt2[i] == 0) cnt2[i] = cnt2[i + 1]; if (sum2[i] == 0) sum2[i] = sum2[i + 1]; } long long ans = INF; for (int i = 1; i <= 1000000; i++) { if (i + k + 1 > 1000000) break; if (cnt1[i] == n && cnt2[i + k + 1] == n) ans = min(ans, (long long)sum1[i] + sum2[i + k + 1]); } if (ans == INF) { printf("-1\n"); return 0; } printf("%I64d\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct _flight { int day; int from; int to; long long cost; }; const long long DAY = 1000000; const long long MAXN = 100000; vector<long long> day[DAY + 10]; vector<_flight> F; long long sum_come[DAY + 10] = {0}; long long cost_come[MAXN + 10] = {0}; long long sum_leave[DAY + 10] = {0}; long long cost_leave[MAXN + 10] = {0}; int early = MAXN + 5; int late = 0; bool b1 = false; bool b2 = false; int n, m, k; int c = 0; int main() { cin >> n >> m >> k; if (m < 2 * n) { cout << -1 << endl; return 0; } F.resize(m); for (int i = 0; i < m; i++) { cin >> F[i].day >> F[i].from >> F[i].to >> F[i].cost; day[F[i].day].push_back(i); } for (int i = 1; i <= DAY; i++) { sum_come[i] = sum_come[i - 1]; if (day[i].empty()) { continue; } for (int j = 0; j < day[i].size(); j++) { _flight& f = F[day[i][j]]; if (f.from == 0) continue; if (cost_come[f.from] == 0) { c++; if (c == n) { b1 = true; early = i; } sum_come[i] += f.cost; cost_come[f.from] = f.cost; } else { if (cost_come[f.from] > f.cost) { sum_come[i] -= cost_come[f.from] - f.cost; cost_come[f.from] = f.cost; } } } } for (int i = DAY; i >= 1; i--) { sum_leave[i] = sum_leave[i + 1]; if (day[i].empty()) { continue; } for (int j = 0; j < day[i].size(); j++) { _flight& f = F[day[i][j]]; if (f.to == 0) continue; if (cost_leave[f.to] == 0) { c--; if (c == 0) { b2 = true; late = i; } sum_leave[i] += f.cost; cost_leave[f.to] = f.cost; } else { if (cost_leave[f.to] > f.cost) { sum_leave[i] -= cost_leave[f.to] - f.cost; cost_leave[f.to] = f.cost; } } } } if (!b1 || !b2) { cout << -1 << endl; return 0; } bool ok = false; long long mi = 0x7fffffffffffffff; for (int i = early; i + k + 1 <= late; i++) { ok = true; mi = min(mi, sum_come[i] + sum_leave[i + k + 1]); } if (ok) { cout << mi << endl; } else { cout << -1 << endl; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
# ---------------------------iye ha aam zindegi--------------------------------------------- import math import heapq, bisect import sys from collections import deque, defaultdict from fractions import Fraction mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n avl=AvlTree() #-----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default='z', func=lambda a, b: min(a ,b)): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: a + b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left)/ 2) # Check if middle element is # less than or equal to key if (arr[mid]<=key): count = mid+1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def countGreater( arr,n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ n,m,k=map(int,input().split()) incom=defaultdict(list) outgo=defaultdict(list) for i in range(m): d,f,t,cost=map(int,input().split()) if t==0: incom[d].append((f,cost)) else: outgo[d].append((t,cost)) cost=[9999999999999999999]*n cou=0 total_cost=0 l=[] for i in sorted(incom.keys()): for j in range(len(incom[i])): if cost[incom[i][j][0]-1]==9999999999999999999: total_cost+=incom[i][j][1] cou+=1 else: total_cost+=min(0,incom[i][j][1]-cost[incom[i][j][0]-1]) cost[incom[i][j][0]-1]=min(cost[incom[i][j][0]-1],incom[i][j][1]) if cou==n: l.append((i,total_cost)) if max(cost)==9999999999999999999: print(-1) sys.exit(0) cost=[9999999999999999999]*n cou=0 total_cost=0 l1=[] for i in sorted(outgo.keys(),reverse=True): for j in range(len(outgo[i])): if cost[outgo[i][j][0]-1]==9999999999999999999: total_cost+=outgo[i][j][1] cou+=1 else: total_cost+=min(0,outgo[i][j][1]-cost[outgo[i][j][0]-1]) cost[outgo[i][j][0]-1]=min(cost[outgo[i][j][0]-1],outgo[i][j][1]) if cou==n: l1.append((i,total_cost)) if max(cost)==9999999999999999999: print(-1) sys.exit(0) l1.reverse() mint=[0]*len(l1) mint[-1]=l1[-1][1] for i in range(len(l1)-2,-1,-1): mint[i]=min(l1[i][1],mint[i+1]) ans=9999999999999999 t=0 #print(l1,l,mint) for i in range(len(l)): d=l[i][0]+k+1 #print(d) f=0 if t==len(l1): break while(d>l1[t][0]): t+=1 if t==len(l1): f=1 break if f==0: ans=min(ans,l[i][1]+mint[t]) if ans==9999999999999999: print(-1) else: print(ans)
PYTHON3
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.util.*; import java.io.*; import java.math.*; import java.lang.*; public class Main { private static FastReader sc = new FastReader(System.in); private static OutputWriter out = new OutputWriter(System.out); public static void main(String[] args) throws Exception { int n = sc.nextInt(); int flights = sc.nextInt(); int days = sc.nextInt(); ArrayList<Flight>[] flightsReaching = (ArrayList<Flight>[]) new ArrayList[(int) (1e6 + 1)]; ArrayList<Flight>[] flightsDeparting = (ArrayList<Flight>[]) new ArrayList[(int) (1e6 + 1)]; for (int i = 0; i <= 1e6; i++) { flightsReaching[i] = new ArrayList<>(); flightsDeparting[i] = new ArrayList<>(); } TreeMap<Integer, Integer>[] minDepForI = (TreeMap<Integer, Integer>[]) new TreeMap[n + 1]; for (int i = 0; i <= n; i++) minDepForI[i] = new TreeMap<>(); for (int i = 0; i < flights; i++) { int day = sc.nextInt(); int dc = sc.nextInt(); int ac = sc.nextInt(); int cost = sc.nextInt(); if (ac == 0) { flightsReaching[day].add(new Flight(day, ac, dc, cost)); } else if (day > 1 + days) { flightsDeparting[day].add(new Flight(day, ac, dc, cost)); if (!minDepForI[ac].containsKey(cost)) minDepForI[ac].put(cost, 1); else minDepForI[ac].put(cost, minDepForI[ac].get(cost) + 1); } } int[] arrCost = new int[n + 1]; int[] depCost = new int[n + 1]; Arrays.fill(arrCost, Integer.MAX_VALUE); Arrays.fill(depCost, Integer.MAX_VALUE); long total = 0; for (int i = 1; i <= n; i++) { if (minDepForI[i].isEmpty()) { System.out.println(-1); System.exit(0); } depCost[i] = Math.min(depCost[i], minDepForI[i].firstKey()); total += depCost[i]; } HashSet<Integer> started = new HashSet<>(); long ans = Long.MAX_VALUE; out: for (int i = 2; i <= 1e6 - days; i++) { for (Flight j : flightsReaching[i - 1]) { started.add(j.departure); if (arrCost[j.departure] != Integer.MAX_VALUE) { total -= arrCost[j.departure]; total += Math.min(arrCost[j.departure], j.cost); } else { total += j.cost; } arrCost[j.departure] = Math.min(arrCost[j.departure], j.cost); } for (Flight j : flightsDeparting[i + days - 1]) { int city = j.arrival; int cost = j.cost; minDepForI[city].put(cost, minDepForI[city].get(cost) - 1); if (minDepForI[city].get(cost) == 0) { minDepForI[city].remove(cost); if (minDepForI[city].isEmpty()) break out; if (minDepForI[city].firstKey() > cost) total += (minDepForI[city].firstKey() - cost); depCost[city] = minDepForI[city].firstKey(); } } // System.out.println(i + " " + Arrays.toString(arrCost)); // System.out.println(i + " " + Arrays.toString(depCost)); // System.out.println(total); if (started.size() == n) { ans = Math.min(ans, total); } } if (started.size() == n && ans <= 1e17) System.out.println(ans); else System.out.println(-1); } } class Flight implements Comparable<Flight> { int day, arrival, departure, cost; public Flight(int d, int a, int dep, int c) { day = d; arrival = a; departure = dep; cost = c; } public int compareTo(Flight that) { return Integer.compare(this.cost, that.cost); } public String toString() { return "day " + day + " arrival " + arrival + " departure " + departure + " cost " + cost; } } class FastReader { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c == ',') { c = read(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String nextLine() { String s = readLine0(); while (s.trim().length() == 0) s = readLine0(); return s; } public String nextLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return nextLine(); } else { return readLine0(); } } public BigInteger nextBigInteger() { try { return new BigInteger(nextString()); } catch (NumberFormatException e) { throw new InputMismatchException(); } } public char nextCharacter() { int c = read(); while (isSpaceChar(c)) c = read(); return (char) c; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, nextInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value == -1; } public String next() { return nextString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct node { int d, f, t, c; } a[100010]; int n, m, k, c1[100010], c2[100010]; long long ans, ans1[100010], ans2[100010]; int find(int x, int l, int r) { int ret = -1, mid; while (l <= r) { mid = (l + r) >> 1; if (a[mid].d >= x) ret = mid, r = mid - 1; else l = mid + 1; } return ret; } int main() { ios::sync_with_stdio(false); int i, cnt = 0; cin >> n >> m >> k; for (i = 1; i <= m; i++) cin >> a[i].d >> a[i].f >> a[i].t >> a[i].c; for (i = 1; i <= n; i++) c1[i] = c2[i] = 1e7; ans = 10000000ll * n; sort(a + 1, a + m + 1, [&](node a, node b) { return a.d < b.d; }); for (i = 1; i <= m && cnt < n; i++) { if (a[i].t == 0) { if (c1[a[i].f] == 1e7) cnt++; if (c1[a[i].f] > a[i].c) { ans -= c1[a[i].f] - a[i].c; c1[a[i].f] = a[i].c; } } } ans1[i - 1] = ans; for (; i <= m; i++) { if (c1[a[i].f] > a[i].c) { ans -= c1[a[i].f] - a[i].c; c1[a[i].f] = a[i].c; } ans1[i] = ans; } for (i = m, cnt = 0, ans = 10000000ll * n; i && cnt < n; i--) { if (a[i].f == 0) { if (c2[a[i].t] == 1e7) cnt++; if (c2[a[i].t] > a[i].c) { ans -= c2[a[i].t] - a[i].c; c2[a[i].t] = a[i].c; } } } ans2[i + 1] = ans; for (; i; i--) { if (c2[a[i].t] > a[i].c) { ans -= c2[a[i].t] - a[i].c; c2[a[i].t] = a[i].c; } ans2[i] = ans; } for (i = 1; !ans1[i]; i++) ; for (ans = 10000000ll * n; i <= m; i++) { int p = find(a[i].d + k + 1, i + 1, m); if (ans2[p]) ans = min(ans, ans1[i] + ans2[p]); } cout << (ans == 10000000ll * n ? -1 : ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; public class P854D { static int days = 1000000; public static void main(String[] args) { FastScanner scan = new FastScanner(); int n = scan.nextInt(), m = scan.nextInt(), k = scan.nextInt(); Flight[] flights = new Flight[m]; ArrayList<Flight>[] arrivals = new ArrayList[n+1]; ArrayList<Flight>[] departures = new ArrayList[n+1]; for (int i = 0; i < arrivals.length; i++) arrivals[i] = new ArrayList<>(); for (int i = 0; i < departures.length; i++) departures[i] = new ArrayList<>(); for (int i = 0; i < m; i++) flights[i] = new Flight(scan.nextInt(), scan.nextInt(), scan.nextInt(), scan.nextInt()); Arrays.sort(flights); for (int i = 0; i < flights.length; i++) { Flight f = flights[i]; if (f.to == 0) { f.ind = arrivals[f.from].size(); arrivals[f.from].add(f); } else { f.ind = departures[f.to].size(); departures[f.to].add(f); } } int minStart = -1; for (int i = 1; i < arrivals.length; i++) { if (arrivals[i].isEmpty()) { System.out.println(-1); return; } minStart = Math.max(minStart, arrivals[i].get(0).day); } long[] minArr = new long[n+1]; long[] minCostArrival = new long[days+1]; int ind = 0; for (int i = 1; i <= days; i++) { minCostArrival[i] = minCostArrival[i-1]; while (ind < flights.length && flights[ind].day == i) { if (!flights[ind].arrival) { ind++; continue; } Flight f = flights[ind]; if (minArr[f.city] == 0 || f.cost < minArr[f.city]) { minCostArrival[i] += f.cost; minCostArrival[i] -= minArr[f.city]; minArr[f.city] = f.cost; } ind++; } } int maxEnd = Integer.MAX_VALUE; for (int i = 1; i < departures.length; i++) { if (departures[i].isEmpty()) { System.out.println(-1); return; } maxEnd = Math.min(maxEnd, departures[i].get(departures[i].size()-1).day); } long[] minDep = new long[n+1]; long[] minCostDeparture = new long[days+2]; ind = flights.length-1; for (int i = days; i >= 0; i--) { minCostDeparture[i] = minCostDeparture[i+1]; while (ind >= 0 && flights[ind].day == i) { if (flights[ind].arrival) { ind--; continue; } Flight f = flights[ind]; if (minDep[f.city] == 0 || f.cost < minDep[f.city]) { minCostDeparture[i] += f.cost; minCostDeparture[i] -= minDep[f.city]; minDep[f.city] = f.cost; } ind--; } } if (maxEnd - minStart <= k) { System.out.println(-1); return; } long min = Long.MAX_VALUE; for (int d = minStart; d+k+1 <= maxEnd; d++) min = Math.min(min, minCostArrival[d] + minCostDeparture[d+k+1]); System.out.println(min); } static class Flight implements Comparable<Flight> { boolean arrival; int day, from, to, ind, city; long cost; Flight(int d, int f, int t, long c) { day = d; from = f; to = t; cost = c; arrival = to == 0; city = arrival ? from : to; } public int compareTo(Flight f) { return day - f.day; } } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { e.printStackTrace(); } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextLine() { String line = ""; try { line = br.readLine(); } catch (Exception e) { e.printStackTrace(); } return line; } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long sz = 1e6 + 150; struct ticket { long long tm, from, to, price; }; bool cmp(ticket a, ticket b) { return a.tm < b.tm; } vector<long long> arrive(sz + 150, inf), leave(sz + 150, inf); long long n, m, k; vector<long long> used(sz + 100, inf); vector<ticket> a; int main() { cin >> n >> m >> k; ticket temp; long long i; for (i = 0; i < m; i++) { cin >> temp.tm >> temp.from >> temp.to >> temp.price; if (temp.from == 0) temp.tm--; else temp.tm++; a.push_back(temp); } sort(a.begin(), a.end(), cmp); long long sum = 0, num = 0; for (i = 0; i < m; i++) { ticket cur = a[i]; if (cur.from != 0) { if (used[cur.from] == inf) { sum += cur.price; num++; used[cur.from] = cur.price; } else { if (used[cur.from] > cur.price) { sum -= used[cur.from]; sum += cur.price; used[cur.from] = cur.price; } } if (num == n) arrive[cur.tm] = min(arrive[cur.tm], sum); } } for (i = 0; i <= sz + 10; i++) used[i] = inf; arrive[0] = inf; for (i = 1; i <= sz + 100; i++) arrive[i] = min(arrive[i], arrive[i - 1]); reverse(a.begin(), a.end()); sum = 0, num = 0; for (i = 0; i < m; i++) { ticket cur = a[i]; if (cur.to != 0) { if (used[cur.to] == inf) { sum += cur.price; num++; used[cur.to] = cur.price; } else { if (used[cur.to] > cur.price) { sum -= used[cur.to]; sum += cur.price; used[cur.to] = cur.price; } } if (num == n) leave[cur.tm] = min(leave[cur.tm], sum); } } leave[sz + 10] - inf; for (i = sz + 9; i >= 1; i--) leave[i] = min(leave[i], leave[i + 1]); long long best = inf; for (i = 1; i <= sz; i++) { long long j = i + k - 1; if (j > sz) break; best = min(best, arrive[i] + leave[j]); } if (best < inf) cout << best; else cout << -1; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; long long INF = 1e12; long long pref[1000006], suff[1000009]; long long curr[100003]; vector<pair<int, long long> > go[1000003], back[1000003]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int day, d, a; long long c; cin >> day >> d >> a >> c; if (d == 0) back[day].push_back(pair<int, long long>(a, c)); else go[day].push_back(pair<int, long long>(d, c)); } pref[0] = (long long)n * INF; suff[1000001] = (long long)n * INF; for (int i = 1; i <= n; i++) curr[i] = INF; for (int i = 1; i <= 1000000; i++) { pref[i] = pref[i - 1]; for (int j = 0; j < go[i].size(); j++) { if (go[i][j].second < curr[go[i][j].first]) { pref[i] = pref[i] - curr[go[i][j].first] + go[i][j].second; curr[go[i][j].first] = go[i][j].second; } } } for (int i = 1; i <= n; i++) curr[i] = INF; for (int i = 1000000; i >= 1; i--) { suff[i] = suff[i + 1]; for (int j = 0; j < back[i].size(); j++) { if (back[i][j].second < curr[back[i][j].first]) { suff[i] = suff[i] - curr[back[i][j].first] + back[i][j].second; curr[back[i][j].first] = back[i][j].second; } } } long long res = INF; for (int i = 1; i + k + 2 <= 1000000; i++) { res = min(res, pref[i] + suff[i + k + 1]); } if (res < INF) cout << res << "\n"; else cout << "-1\n"; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.*; import java.util.*; public class B { static InputStream is; public static void main(String[] args) throws IOException { is = System.in; int n = ni(); int m = ni(); int k = ni(); f[] fs = new f[m]; for (int i = 0; i < fs.length; i++) { fs[i] = new f(ni(),ni(),ni(),ni()); } Arrays.sort(fs); long oo = (long)1e12; long[] best = new long[(int)1e6+1]; Arrays.fill(best, oo); long sum = oo*n; long[] min = new long[n+1]; Arrays.fill(min, oo); for(int i =0; i < fs.length; i++){ long cost = fs[i].c; if(fs[i].f != 0 && cost < min[fs[i].f]){ sum -= min[fs[i].f]; sum += min[fs[i].f] = cost; if(sum < oo) best[fs[i].d] = Math.min(best[fs[i].d], sum); } } for (int i = 1; i < best.length; i++) { best[i] = Math.min(best[i], best[i-1]); } long ans = -1; min = new long[n+1]; Arrays.fill(min, oo); sum = oo*n; for(int i = fs.length-1; i >= 0; i--){ long cost = fs[i].c; int t = fs[i].t; if(t != 0 && cost < min[t]){ sum -= min[t]; sum += min[t] = cost; if(sum < oo && fs[i].d >= k+1){ long cont = sum+best[fs[i].d -k-1]; if(cont < oo){ if(ans == -1 || cont < ans) ans = cont; } } } } System.out.println(ans); } static class f implements Comparable<f>{ int d,f,t,c; public f(int d, int f, int t, int c) { super(); this.d = d; this.f = f; this.t = t; this.c = c; } @Override public int compareTo(f o) { if(d != o.d) return d - o.d; return o.f- f; } } private static byte[] inbuf = new byte[1024]; public static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; long long maxn = -1e9; long long minn = 1e15; long long mod = 1e9 + 7; const int maxx = 1e6 + 5; const int base = 311; struct viet { long long d, from, to, p; }; viet a[maxx]; bool cmp(viet x, viet y) { return x.d < y.d; } bool cmp1(viet x, viet y) { return x.d > y.d; } long long arr[maxx], visited[maxx], out[maxx], cur[maxx]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, k; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> a[i].d >> a[i].from >> a[i].to >> a[i].p; } sort(a + 1, a + m + 1, cmp); long long sum = 0, cnt = 0; for (int i = 1; i <= 1e6; i++) arr[i] = minn; for (int i = 1; i <= n; i++) { visited[i] = 0; } for (int i = 1; i <= m; i++) { if (a[i].from != 0) { if (visited[a[i].from] == 0) { cur[a[i].from] = a[i].p; sum = sum + a[i].p; cnt++; visited[a[i].from] = 1; } else { if (a[i].p < cur[a[i].from]) { sum = sum - cur[a[i].from] + a[i].p; cur[a[i].from] = a[i].p; } } if (cnt == n) { arr[a[i].d] = sum; } } } for (int i = 2; i <= 1e6; i++) { arr[i] = min(arr[i - 1], arr[i]); } sort(a + 1, a + m + 1, cmp1); sum = 0, cnt = 0; for (int i = 1; i <= 1e6; i++) out[i] = minn; for (int i = 1; i <= n; i++) { visited[i] = 0; cur[i] = 0; } for (int i = 1; i <= m; i++) { if (a[i].to != 0) { if (visited[a[i].to] == 0) { cur[a[i].to] = a[i].p; sum = sum + a[i].p; cnt++; visited[a[i].to] = 1; } else { if (a[i].p < cur[a[i].to]) { sum = sum - cur[a[i].to] + a[i].p; cur[a[i].to] = a[i].p; } } if (cnt == n) { out[a[i].d] = sum; } } } for (int i = 1e6 - 1; i >= k + 1; i--) { out[i] = min(out[i + 1], out[i]); } long long ans = 1e15; for (int i = k + 2; i <= 1e6; i++) { ans = min(ans, out[i] + arr[i - k - 1]); } if (ans == minn) cout << -1; else cout << ans; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e6 + 9; struct mb { int d, f, t, c; }; long long dp1[MAXN], dp2[MAXN]; int v[MAXN]; vector<mb> arr, dep; bool cmp1(mb a, mb b) { if (a.d < b.d) return true; return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) { mb x; cin >> x.d >> x.f >> x.t >> x.c; if (x.t == 0) arr.push_back(x); else dep.push_back(x); } sort(arr.begin(), arr.end(), cmp1); long long cost = 0; int rem = n, id = 0; for (mb x : arr) { if (v[x.f] == 0) { cost += x.c; v[x.f] = x.c; rem--; } if (v[x.f] > x.c) { cost -= v[x.f] - x.c; v[x.f] = x.c; } id++; if (rem == 0) break; } if (rem != 0) return cout << "-1\n", 0; dp1[arr[id - 1].d] = cost; for (; id < arr.size(); id++) if (v[arr[id].f] > arr[id].c) { dp1[arr[id].d] = dp1[arr[id - 1].d] - (v[arr[id].f] - arr[id].c); v[arr[id].f] = arr[id].c; } else dp1[arr[id].d] = dp1[arr[id - 1].d]; long long cur = 0; for (int i = 0; i < MAXN; i++) { if (dp1[i] != 0) cur = dp1[i]; dp1[i] = cur; } fill(v, v + MAXN, 0); sort(dep.rbegin(), dep.rend(), cmp1); cost = 0; rem = n, id = 0; for (mb x : dep) { if (v[x.t] == 0) { cost += x.c; v[x.t] = x.c; rem--; } if (v[x.t] > x.c) { cost -= v[x.t] - x.c; v[x.t] = x.c; } id++; if (rem == 0) break; } if (rem != 0) return cout << "-1\n", 0; dp2[dep[id - 1].d] = cost; for (; id < dep.size(); id++) if (v[dep[id].t] > dep[id].c) { dp2[dep[id].d] = dp2[dep[id - 1].d] - (v[dep[id].t] - dep[id].c); v[dep[id].t] = dep[id].c; } else dp2[dep[id].d] = dp2[dep[id - 1].d]; cur = 0; for (int i = MAXN - 1; i >= 0; i--) { if (dp2[i] != 0) cur = dp2[i]; dp2[i] = cur; } long long res = 1e18; for (int i = 1; i + k + 1 < MAXN; i++) if (dp1[i] && dp2[i + k + 1] && res > dp1[i] + dp2[i + k + 1]) res = dp1[i] + dp2[i + k + 1]; if (res == 1e18) res = -1; cout << res << "\n"; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int N, M, K, i, dti, day, ind; pair<pair<int, int>, pair<int, long long> > A[100000]; long long tot, dep[1000001], ari[1000001], har[100001], ans = -1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M >> K; for (i = 0; i < M; i++) { cin >> A[i].first.first >> A[i].first.second >> A[i].second.first >> A[i].second.second; } sort(A, A + M); for (day = 1; day <= 1000000; day++) { while (ind < M && A[ind].first.first == day) { if (A[ind].second.first == 0) { if (har[A[ind].first.second] == 0) { dti++; tot += A[ind].second.second; har[A[ind].first.second] = A[ind].second.second; } else { tot -= max(tot - tot, har[A[ind].first.second] - A[ind].second.second); har[A[ind].first.second] = min(har[A[ind].first.second], A[ind].second.second); } } ind++; } if (dti == N) { dep[day] = tot; } else { dep[day] = -1; } } ind--; for (i = 1; i <= N; i++) { har[i] = 0; } dti = 0; tot = 0; for (day = 1000000; day > 0; day--) { while (ind >= 0 && A[ind].first.first == day) { if (A[ind].first.second == 0) { if (har[A[ind].second.first] == 0) { dti++; tot += A[ind].second.second; har[A[ind].second.first] = A[ind].second.second; } else { tot -= max(tot - tot, har[A[ind].second.first] - A[ind].second.second); har[A[ind].second.first] = min(har[A[ind].second.first], A[ind].second.second); } } ind--; } if (dti == N) { ari[day] = tot; } else { ari[day] = -1; } } for (day = 1; day + K + 1 <= 1000000; day++) { if (dep[day] != -1) { if (ari[day + K + 1] == -1) { break; } if (ans == -1) { ans = dep[day] + ari[day + K + 1]; } else { ans = min(ans, dep[day] + ari[day + K + 1]); } } } cout << ans << '\n'; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2000010; int c[N]; struct cmp { bool operator()(const int& a, const int& b) const { if (c[a] == c[b]) return a < b; return c[a] < c[b]; } }; vector<pair<int, int>> in[N], out[N]; set<int, cmp> pq[N]; int bin[N]; int main() { ios::sync_with_stdio(false); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) { int d, f, t; cin >> d >> f >> t >> c[i]; if (t == 0) { in[d].emplace_back(f, i); } else { out[d].emplace_back(t, i); if (d > k) { pq[t].insert(i); } } } ll cur = 0; for (int i = 1; i <= n; i++) { if (pq[i].empty()) { cout << -1 << endl; return 0; } cur += c[*pq[i].begin()]; } ll ans = -1; int gin = 0; bool bad = false; for (int i = 1; i <= 1000000 && !bad; i++) { if (gin == n) { if (ans == -1 || cur < ans) { ans = cur; } } for (auto& p : in[i]) { int f, id; tie(f, id) = p; if (!bin[f]) { cur += c[id]; bin[f] = c[id]; gin++; } else { cur -= bin[f]; bin[f] = min(bin[f], c[id]); cur += bin[f]; } } for (auto& p : out[i + k]) { int t, id; tie(t, id) = p; cur -= c[*pq[t].begin()]; pq[t].erase(id); if (pq[t].empty()) { bad = true; break; } cur += c[*pq[t].begin()]; } } cout << ans << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int M = 1e6 + 5; const long long INF = 1e15; struct Flight { int d, f, t, c; friend bool operator<(const Flight& a, const Flight& b) { return a.d < b.d; } }; static int n, m, k; Flight flt[N]; int Min[N]; long long L[M], R[M]; int update(int i, int c, long long& cost) { if (Min[i] == -1) { Min[i] = c; cost += c; return 1; } if (c < Min[i]) { cost += c - Min[i]; Min[i] = c; } return 0; } int main() { while (scanf("%d%d%d", &n, &m, &k) != EOF) { for (int i = 0; i < m; i++) scanf("%d%d%d%d", &flt[i].d, &flt[i].f, &flt[i].t, &flt[i].c); sort(flt, flt + m); for (int i = 0; i < M; i++) L[i] = R[i] = INF; int cnt = 0; long long cost = 0; memset(Min, -1, sizeof(Min)); for (int i = 0; i < m; i++) if (flt[i].f != 0) { cnt += update(flt[i].f, flt[i].c, cost); if (cnt == n) L[flt[i].d] = cost; } for (int i = 1; i < M; i++) L[i] = min(L[i - 1], L[i]); cost = cnt = 0; memset(Min, -1, sizeof(Min)); for (int i = m - 1; i >= 0; i--) if (flt[i].t != 0) { cnt += update(flt[i].t, flt[i].c, cost); if (cnt == n) R[flt[i].d] = cost; } for (int i = M - 2; i >= 0; i--) R[i] = min(R[i + 1], R[i]); cost = INF; for (int i = 0; i + k + 1 < M; i++) cost = min(cost, L[i] + R[i + k + 1]); if (cost == INF) puts("-1"); else printf("%lld\n", cost); } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long inf = 1e15; struct st { long long d, s, e, c; }; long long chk[1000005], mnS[1000005], mnE[1000005]; st s[1000005]; bool cmpr(st s1, st s2) { return s1.d < s2.d; } int main() { long long i, j, k; long long n, m, sm, cur; cin >> n >> m >> k; for (i = 0; i < m; ++i) scanf("%lld %lld %lld %lld", &s[i].d, &s[i].s, &s[i].e, &s[i].c); sort(s, s + m, cmpr); memset(chk, 0, sizeof chk); for (i = 0; i < 1000005; ++i) mnE[i] = inf, mnS[i] = inf; for (i = 0, cur = 0, sm = 0; i < m; ++i) { if (s[i].s) { if (!chk[s[i].s]) { chk[s[i].s] = s[i].c; cur++; sm += s[i].c; } else { sm -= chk[s[i].s]; chk[s[i].s] = min(chk[s[i].s], s[i].c); sm += chk[s[i].s]; } } if (cur == n) mnS[s[i].d] = sm; } memset(chk, 0, sizeof chk); for (i = m - 1, cur = 0, sm = 0; i >= 0; --i) { if (s[i].e) { if (!chk[s[i].e]) { chk[s[i].e] = s[i].c; cur++; sm += s[i].c; } else { sm -= chk[s[i].e]; chk[s[i].e] = min(chk[s[i].e], s[i].c); sm += chk[s[i].e]; } } if (cur == n) mnE[s[i].d] = sm; } for (i = 1; i < 1000005; ++i) mnS[i] = min(mnS[i - 1], mnS[i]); for (i = 1000005 - 2; i >= 0; --i) mnE[i] = min(mnE[i], mnE[i + 1]); long long ans = inf; for (i = 0; i < 1000005; ++i) { if (i + k + 1 < 1000005 - 2) ans = min(ans, mnS[i] + mnE[i + k + 1]); } if (ans >= inf) cout << -1 << endl; else cout << ans << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int RAN = 1e6 + 5; const long long oo = 1ll << 60; int n, m, x, y, t, k; bool M[RAN]; long long c, tot, C[RAN], sol = oo, X[RAN], Y[RAN]; struct par { int x, y; long long c; }; vector<par> v[RAN]; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%lld", &t, &x, &y, &c); v[t].push_back(par{x, y, c}); } fill(M + 1, M + n + 1, false); int can = 0; for (int i = 1; i < RAN; i++) { for (int j = 0; j < (int)v[i].size(); j++) { par a = v[i][j]; if (a.x == 0) continue; if (!M[a.x]) { M[a.x] = true; C[a.x] = a.c; tot += a.c; can++; } if (a.c < C[a.x]) { tot = tot - C[a.x] + a.c; C[a.x] = a.c; } } if (can == n) X[i] = tot; } can = 0; tot = 0; fill(M + 1, M + n + 1, false); for (int i = RAN - 1; i > 0; i--) { for (int j = 0; j < (int)v[i].size(); j++) { par a = v[i][j]; if (a.y == 0) continue; if (!M[a.y]) { M[a.y] = true; C[a.y] = a.c; tot += a.c; can++; } if (a.c < C[a.y]) { tot = tot - C[a.y] + a.c; C[a.y] = a.c; } } if (can == n) Y[i] = tot; } for (int i = 1; i < RAN - k - 2; i++) { if (X[i] && Y[i + k + 1]) sol = min(sol, X[i] + Y[i + k + 1]); } if (sol == oo) printf("-1\n"); else printf("%lld\n", sol); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> const long long int INF = (1LL << 62) - 1; using namespace std; int N, M, K; set<int> inCapital; set<pair<int, long long int> > S; struct flight { int d, f, t; long long int c; bool operator<(const flight &x) const { return d < x.d; } }; flight F[100005]; long long int ans_arr[100005], ans_dep[100005]; int main() { while (cin >> N >> M >> K) { inCapital.clear(); S.clear(); for (int i = 0; i < M; i++) { cin >> F[i].d >> F[i].f >> F[i].t >> F[i].c; } sort(F, F + M); for (int i = 1; i <= N; i++) ans_arr[i] = ans_dep[i] = INF; long long int s = 0, sans = -1; for (int i = 0; i < M; i++) { if (F[i].t == 0) { inCapital.insert(F[i].f); if (ans_arr[F[i].f] != INF) s -= ans_arr[F[i].f]; ans_arr[F[i].f] = min(ans_arr[F[i].f], F[i].c); s += ans_arr[F[i].f]; if (inCapital.size() == N) { S.insert(pair<int, long long int>(-F[i].d, s)); } } } if (inCapital.size() < N) { cout << "-1" << endl; continue; } s = 0; for (int i = M - 1; i >= 0; i--) { if (F[i].f == 0) { inCapital.erase(F[i].t); if (ans_dep[F[i].t] != INF) s -= ans_dep[F[i].t]; ans_dep[F[i].t] = min(ans_dep[F[i].t], F[i].c); s += ans_dep[F[i].t]; if (inCapital.size() == 0) { set<pair<int, long long int> >::iterator it = S.upper_bound(pair<int, long long int>(-(F[i].d - K), INF)); if (it != S.end()) { if (sans == -1) sans = (*it).second + s; else sans = min(sans, (*it).second + s); } } } } if (inCapital.size() > 0) { cout << "-1" << endl; continue; } cout << sans << endl; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.util.*; public class Main { static long[] deltaToCapital = new long[1000_007]; static long[] deltaFromCapital = new long[1000_007]; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); Map<String, List<Flight>> map = new HashMap<>(m * 2); for (int i = 0; i < m; i++) { int d = in.nextInt(); int f = in.nextInt(); int t = in.nextInt(); int c = in.nextInt(); Flight flight = new Flight(f, t, c, d); String key = f + "" + t; List<Flight> flights = map.get(key); if (flights == null) { flights = new ArrayList<>(); flights.add(flight); map.put(key, flights); } else { flights.add(flight); } } if (map.keySet().size() / 2 < n) { p("-1"); return; } Comparator<Flight> comparatorIncr = (x, y) -> { int result = x.day - y.day; if (result == 0) result = x.cost - y.cost; return result; }; int left = 0; for (int i = 1; i <= n; i++) { String keyTo = i + "" + 0; List<Flight> flights = map.get(keyTo); flights.sort(comparatorIncr); long prevCost = Long.MAX_VALUE; for (int j = 0; j < flights.size(); j++) { Flight f = flights.get(j); if (f.cost >= prevCost) { continue; } if (j == 0) { deltaToCapital[f.day] += f.cost; if (f.day > left) left = f.day; } else { deltaToCapital[f.day] += f.cost - prevCost; } prevCost = f.cost; } } Comparator<Flight> comparatorDecr = (x, y) -> { int result = y.day - x.day; if (result == 0) result = y.cost - x.cost; return result; }; int right = Integer.MAX_VALUE; for (int i = n; i > 0; i--) { String keyFrom = 0 + "" + i; List<Flight> flights = map.get(keyFrom); flights.sort(comparatorDecr); long prevCost = Long.MAX_VALUE; for (int j = 0; j < flights.size(); j++) { Flight f = flights.get(j); if (f.cost >= prevCost) { continue; } if (j == 0) { deltaFromCapital[f.day] += f.cost; if (f.day < right) right = f.day; } else { deltaFromCapital[f.day] += f.cost - prevCost; } prevCost = f.cost; } } for (int i = 1000_002; i >= 0; i--) { deltaFromCapital[i] += deltaFromCapital[i + 1]; } int r = Math.min(1000_002, right + 10); for (int i = 0; i < r; i++) { deltaToCapital[i + 1] += deltaToCapital[i]; } if (right - left - 1 < k) { p(-1); return; } long cost = Long.MAX_VALUE; for (int i = left; i <= right - k - 1; i++) { long temp = deltaToCapital[i] + deltaFromCapital[i + k + 1]; if(temp < cost) cost = temp; } p(cost); } static class Flight { int from; int to; int cost; int day; public Flight(int from, int to, int cost, int day) { this.from = from; this.to = to; this.cost = cost; this.day = day; } public int getDay() { return day; } } public static int gcd(int a, int b) { int t; while (b != 0) { t = b; b = a % b; a = t; } return a; } static void p(Object str) { System.out.println(str); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > go[1000001]; vector<pair<int, int> > gogo[1000001]; vector<int> mb; int masf[100001]; set<pair<int, int> > mass[100001]; signed main() { int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= m; ++i) { int d, f, t, c; scanf("%d %d %d %d", &d, &f, &t, &c); if (f == 0) { gogo[d].push_back(make_pair(t, c)); mass[t].insert(make_pair(c, d)); } else { go[d].push_back(make_pair(f, c)); mb.push_back(d + 1); } } long long ansr = 0; long long ansl = 0; long long ans = 1234567890123456789; for (int i = 1; i <= n; ++i) { if (mass[i].empty()) return 0 * printf("-1"); ansr += mass[i].begin()->first; } int l = 0; int r = 0; int pp = 0; int cnt = n; sort(mb.begin(), mb.end()); for (auto start : mb) { while (l < 1e6 && l + 1 < start) { ++l; for (auto it : go[l]) { if (masf[it.first] == 0) { --cnt; ansl += it.second; masf[it.first] = it.second; } else { if (it.second < masf[it.first]) { ansl -= masf[it.first] - it.second; masf[it.first] = it.second; } } } } while (r < 1e6 && r + 1 < start + k) { ++r; for (auto it : gogo[r]) { mass[it.first].erase(make_pair(it.second, r)); if (mass[it.first].empty()) pp = 1; else { ansr += max(0, mass[it.first].begin()->first - it.second); } } } if (pp) break; if (!cnt) ans = min(ans, ansl + ansr); } if (ans == 1234567890123456789) cout << -1 << endl; else cout << ans << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; long long n, k, m; vector<pair<pair<long long, long long>, pair<long long, long long> > > data; signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); cin >> n >> m >> k; for (long long i = 0; i < m; i++) { pair<pair<long long, long long>, pair<long long, long long> > tmp; cin >> tmp.first.first >> tmp.first.second >> tmp.second.first >> tmp.second.second; data.push_back(tmp); } sort((data).begin(), (data).end()); vector<long long> score(n + 1, 1001000100010001000); vector<pair<long long, long long> > go; long long sum = 0, cnt = 0; for (long long i = 0; i < m; i++) { long long time = data[i].first.first; if (data[i].first.second) { if (score[data[i].first.second] == 1001000100010001000) { score[data[i].first.second] = data[i].second.second; sum += data[i].second.second; cnt++; } else if (score[data[i].first.second] > data[i].second.second) { sum -= score[data[i].first.second] - data[i].second.second; score[data[i].first.second] = data[i].second.second; } else { continue; } if (cnt == n) { go.push_back(pair<long long, long long>(time, sum)); } } } score = vector<long long>(n + 1, 1001000100010001000); vector<pair<long long, long long> > back; sum = 0, cnt = 0; for (long long i = m - 1; i >= 0; i--) { long long time = data[i].first.first; if (data[i].second.first) { if (score[data[i].second.first] == 1001000100010001000) { score[data[i].second.first] = data[i].second.second; sum += data[i].second.second; cnt++; } else if (score[data[i].second.first] > data[i].second.second) { sum -= score[data[i].second.first] - data[i].second.second; score[data[i].second.first] = data[i].second.second; } else { continue; } if (cnt == n) { back.push_back(pair<long long, long long>(time, sum)); } } } if (!go.size() || !back.size()) { std::cout << -1 << std::endl; return 0; } long long ans = 1001000100010001000, now = 1001000100010001000, ptr = 0, backscore = 1001000100010001000; for (long long i = go.size() - 1; i >= 0; i--) { now = go[i].second; long long time = go[i].first; while (ptr < back.size() && time + k < back[ptr].first) { backscore = min(backscore, back[ptr].second); ptr++; } ans = min(ans, backscore + now); } if (ans == 1001000100010001000) { std::cout << -1 << std::endl; } else { std::cout << ans << std::endl; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct lot { int c; bool kier; int koszt; lot(int c = -1, bool kier = 0, int koszt = 0) : c(c), kier(kier), koszt(koszt) {} }; long long sum; const int rozm = 1e6 + 1; int ile, ile_lot, czas; vector<vector<lot> > loty; vector<lot> tab; vector<long long> dp; vector<long long> dp2; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> ile >> ile_lot >> czas; tab.resize(ile + 1); loty.resize(rozm); dp.resize(rozm, -1); dp2.resize(rozm, -1); for (int i = 0; i < ile_lot; ++i) { int t; int c; int kier; int koszt; int p, p2; cin >> t >> p >> p2 >> koszt; kier = (p2 == 0 ? true : false); c = (kier ? p : p2); loty[t].push_back(lot(c, kier, koszt)); } int brk = ile; for (int i = 0; i < rozm; ++i) { for (auto&& lo : loty[i]) { if (lo.kier == false) continue; if (tab[lo.c].c == -1 or tab[lo.c].koszt > lo.koszt) { if (tab[lo.c].c == -1) --brk; sum -= tab[lo.c].koszt; tab[lo.c] = lo; sum += lo.koszt; } } if (brk == 0) dp[i] = sum; } sum = 0; brk = ile; for (int i = 1; i <= ile; ++i) tab[i] = lot(); for (int i = rozm - 1; i >= 0; --i) { for (auto&& lo : loty[i]) { if (lo.kier == true) continue; if (tab[lo.c].c == -1 or tab[lo.c].koszt > lo.koszt) { if (tab[lo.c].c == -1) --brk; sum -= tab[lo.c].koszt; tab[lo.c] = lo; sum += lo.koszt; } } if (brk == 0) dp2[i] = sum; } long long res = LLONG_MAX; for (int i = 0; i < rozm; ++i) if (i + czas + 1 < rozm and dp2[i + czas + 1] != -1 and dp[i] != -1) res = min(res, dp2[i + czas + 1] + dp[i]); cout << (res == LLONG_MAX ? -1 : res) << '\n'; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int n, m, i, j, l, z, y, k, x; long long num, sum; using namespace std; const long long LLM = 1e18; const int maxn = 1e6 + 5; vector<pair<int, int> > fuck2[maxn]; vector<pair<int, int> > fuck1[maxn]; long long c[maxn], a1[maxn], a2[maxn]; long long x1, x2, x3, x4; int main() { scanf("%d%d", &n, &m); scanf("%d", &k); for (int i = 1; i <= m; i++) { scanf("%lld%lld", &x1, &x2); scanf("%lld%lld", &x3, &x4); if (!x2) fuck2[x1].push_back(make_pair(x3, x4)); else fuck1[x1].push_back(make_pair(x2, x4)); } for (int i = 1; i <= 1e6 + 1; i++) { a1[i] = LLM; a2[i] = LLM; } for (int i = 1; i <= 1e6 + 1; i++) c[i] = 1e7; num = 0; sum = (long long)(n) * (1e7); for (int i = 1; i <= 1e6 + 1; i++) { for (auto x : fuck1[i]) { x1 = x.first; x2 = x.second; if (c[x1] == (1e7)) num++; sum -= c[x1]; c[x1] = min(c[x1], x2); sum += c[x1]; } if (num >= n) a1[i] = min(a1[i], sum); } for (int i = 1; i <= 1e6 + 1; i++) c[i] = 1e7; num = 0; sum = (long long)(n) * (1e7); for (int i = 1e6; i >= 1; i--) { for (auto x : fuck2[i]) { x1 = x.first; x2 = x.second; if (c[x1] == (1e7)) num++; sum -= c[x1]; c[x1] = min(c[x1], x2); sum += c[x1]; } if (num >= n) a2[i] = min(a2[i], sum); } long long ans = 5e18; for (int i = 1; i <= 1e6; i++) if (i + k + 1 <= 1e6 && a1[i] != LLM && a2[i + k + 1] != LLM) ans = min(ans, a1[i] + a2[i + k + 1]); if (ans == 5e18) cout << "-1" << endl; else cout << ans << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-9, PI = acos(-1.); const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const int N = 1e5 + 5, M = 1e6 + 5; int n, m, k, d[N], f[N], t[N], c[N], o[N]; int mi, ma; int vis[N]; int cc[N], cs[N]; long long tvis, sum; long long first[4 * M]; void update(int p, int l, int r, int x, long long v) { if (l == r and l == x) { first[p] = v; return; } if (r < x or l > x) return; update(2 * p, l, (l + r) / 2, x, v); update(2 * p + 1, (l + r) / 2 + 1, r, x, v); if (!first[2 * p]) first[p] = first[2 * p + 1]; else if (!first[2 * p + 1]) first[p] = first[2 * p]; else first[p] = min(first[2 * p], first[2 * p + 1]); } long long query(int p, int l, int r, int i, int j) { if (l > j or r < i) return 0; if (i <= l and r <= j) return first[p]; long long q0 = query(2 * p, l, (l + r) / 2, i, j); long long q1 = query(2 * p + 1, (l + r) / 2 + 1, r, i, j); if (!q0) return q1; if (!q1) return q0; return min(q0, q1); } int main() { memset((cc), (63), sizeof(cc)); scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) scanf("%d%d%d%d", &d[i], &f[i], &t[i], &c[i]), o[i] = i; sort(o, o + m, [](int i, int j) { return d[i] < d[j]; }); mi = -1, ma = -1; tvis = 0; memset((vis), (0), sizeof(vis)); for (int i = 0; i < m; i++) if (f[o[i]] and !vis[f[o[i]]]) { vis[f[o[i]]] = 1; if (++tvis == n) { mi = d[o[i]]; break; } } tvis = 0; memset((vis), (0), sizeof(vis)); for (int i = m - 1; i >= 0; i--) if (t[o[i]] and !vis[t[o[i]]]) { vis[t[o[i]]] = 1; if (++tvis == n) { ma = d[o[i]]; break; } } if (mi == -1 or ma == -1 or ma - mi <= k) return printf("-1\n"), 0; tvis = 0; sum = 0; memset((vis), (0), sizeof(vis)); for (int i = m - 1; i >= 0; i--) if (t[o[i]]) { if (!vis[t[o[i]]]) { vis[t[o[i]]] = 1; tvis++; cs[t[o[i]]] = c[o[i]]; sum += c[o[i]]; } else if (c[o[i]] < cs[t[o[i]]]) { sum += c[o[i]] - cs[t[o[i]]]; cs[t[o[i]]] = c[o[i]]; } if (tvis == n) update(1, 0, M - 1, d[o[i]], sum); } long long ans = 0x3f3f3f3f3f3f3f3f; tvis = 0; sum = 0; memset((vis), (0), sizeof(vis)); for (int i = 0; i < m; i++) if (f[o[i]]) { if (!vis[f[o[i]]]) { vis[f[o[i]]] = 1; tvis++; cc[f[o[i]]] = c[o[i]]; sum += c[o[i]]; } else if (c[o[i]] < cc[f[o[i]]]) { sum += c[o[i]] - cc[f[o[i]]]; cc[f[o[i]]] = c[o[i]]; } if (tvis == n) { long long q = query(1, 0, M - 1, d[o[i]] + k + 1, M - 1); if (q) ans = min(ans, sum + q); } } printf("%lld\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * @author pttrung */ public class B_Round_433_Div1 { public static long MOD = 1000000007; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int max = 1000000; ArrayList<Integer>[] arr = new ArrayList[max]; ArrayList<Integer>[] dep = new ArrayList[max]; int[][] data = new int[m][3]; for (int i = 0; i < m; i++) { int d = in.nextInt() - 1; int u = in.nextInt(); int v = in.nextInt(); int p = in.nextInt(); data[i][0] = u; data[i][1] = v; data[i][2] = p; if (u == 0) { if (dep[d] == null) { dep[d] = new ArrayList<>(); } dep[d].add(i); } else { if (arr[d] == null) { arr[d] = new ArrayList<>(); } arr[d].add(i); } // System.out.println(Arrays.toString(data[i])); } Node[] q = new Node[n]; int[] pre = new int[n]; Arrays.fill(pre, -1); for (int i = max - 1; i >= 0; i--) { if (dep[i] != null) { for (int j : dep[i]) { int v = data[j][1] - 1; //System.out.println("BEFORE " + Arrays.toString(data[j]) + " " + q[v]); Node node = new Node(); node.day = i; if (q[v] != null) { node.next = q[v]; node.min = Integer.min(q[v].min, data[j][2]); } else { node.min = data[j][2]; } q[v] = node; // System.out.println("AFTER " + Arrays.toString(data[j]) + " " + q[v]); } } } boolean ok = true; long total = 0; for (int i = 0; i < n; i++) { if (q[i] == null) { ok = false; } else { total += q[i].min; } } //System.out.println(Arrays.toString(q)); int count = 0; long last = 0; int end = 0; long result = -1; for (int i = 0; i <= max - k && ok; i++) { if (i > 0 && arr[i - 1] != null) { for (int j : arr[i - 1]) { int v = data[j][0] - 1; if (pre[v] == -1) { count++; pre[v] = data[j][2]; last += pre[v]; } else if (pre[v] > data[j][2]) { last -= pre[v]; last += data[j][2]; pre[v] = data[j][2]; } } } while (end < i + k && ok) { if (dep[end] != null) { for (int j : dep[end]) { int v = data[j][1] - 1; total -= q[v].min; q[v] = q[v].next; if (q[v] == null) { ok = false; break; } total += q[v].min; } } end++; } //System.out.println(last + " " + total + " " + count + " " + i); if (ok && count == n) { if (result == -1 || result > last + total) { result = last + total; } } } out.println(result); out.close(); } static class Node { int day; int min; Node next = null; @Override public String toString() { return "Node{" + "day=" + day + ", min=" + min + '}'; } } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b, long MOD) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2, MOD); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
/* * Author Ayub Subhaniya * Institute DA-IICT */ import java.io.*; import java.math.*; import java.util.*; public class Codeforces429D { InputStream in; PrintWriter out; int MAX=(int)1e6+7; void solve() { int n=ni(); int m=ni(); int k=ni(); if (m==0) { out.println(-1); return; } long left[]=new long[MAX]; long right[]=new long[MAX]; Arrays.fill(left, Long.MAX_VALUE); Arrays.fill(right, Long.MAX_VALUE); Pair p[]=new Pair[m]; for (int i=0;i<m;i++) p[i]=new Pair(ni(),ni(),ni(),nl()); Arrays.sort(p); int cnt=0; boolean included[]=new boolean[n+1]; long cost[]=new long[n+1]; FenwickTree ft=new FenwickTree(n+1); for (Pair pp:p) { if (pp.x!=0) { if (!included[pp.x]) { cnt++; included[pp.x]=true; ft.update(pp.x, pp.cost-cost[pp.x]); cost[pp.x]=pp.cost; } else if(cost[pp.x]>pp.cost) { ft.update(pp.x, pp.cost-cost[pp.x]); cost[pp.x]=pp.cost; } } if (cnt==n) { left[pp.day]=Math.min(left[pp.day],ft.query(n)); } } for (int i=1;i<MAX;i++) left[i]=Math.min(left[i-1],left[i]); cnt=0; Arrays.fill(included, false); Arrays.fill(cost, 0); ft=new FenwickTree(n+1); for (int i=m-1;i>=0;i--) { Pair pp=p[i]; if (pp.x==0) { if (!included[pp.y]) { cnt++; included[pp.y]=true; ft.update(pp.y, pp.cost-cost[pp.y]); cost[pp.y]=pp.cost; } else if(cost[pp.y]>pp.cost) { ft.update(pp.y, pp.cost-cost[pp.y]); cost[pp.y]=pp.cost; } } if (cnt==n) { right[pp.day]=Math.min(right[pp.day],ft.query(n)); } } for (int i=MAX-2;i>=0;i--) right[i]=Math.min(right[i+1],right[i]); long min=Long.MAX_VALUE; for (int i=1;i<MAX-k;i++) { if (left[i-1]!=Long.MAX_VALUE&&right[i+k]!=Long.MAX_VALUE) min=Math.min(min, left[i-1]+right[i+k]); } if (min!=Long.MAX_VALUE) out.println(min); else out.println(-1); } class Pair implements Comparable<Pair> { int x; int y; int day; long cost; int ext; Pair(int c, int a,int b,long d) { x = a; y = b; day = c; cost=d; ext=Math.max(x, y); } @Override public int compareTo(Pair o) { if (day == o.day) return Integer.compare(ext, o.ext); else return Integer.compare(day, o.day); } } class FenwickTree { int size; long bit[]; FenwickTree(int n) { size = n; bit = new long[size + 1]; } void update(int x, long val) { for (; x <= size; x += x & (-x)) bit[x] += val; } long query(int x) { long sum = 0; for (; x > 0; x -= x & (-x)) sum += bit[x]; return sum; } } void run() throws Exception { String INPUT = "C:/Users/ayubs/Desktop/input.txt"; in = oj ? System.in : new FileInputStream(INPUT); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Codeforces429D().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = in.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean inSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && inSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(inSpaceChar(b))) { // when nextLine, (inSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(inSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct data { int day, from, to, cost; }; bool cmp(data a, data b) { return a.day < b.day; } data inp[100000 + 7]; long long lft[1000000 + 7], rgt[1000000 + 7]; int city[100000 + 7]; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < m; i++) scanf("%d %d %d %d", &inp[i].day, &inp[i].from, &inp[i].to, &inp[i].cost); sort(inp, inp + m, cmp); int cont = 0, id = 0, tp; long long sum = 0; lft[0] = 10000000000000LL; memset(city, -1, sizeof city); for (int i = 1; i <= 1000000; i++) { while (id < m && inp[id].day == i) { if (inp[id].to == 0) { tp = inp[id].from; if (city[tp] == -1 || city[tp] > inp[id].cost) { if (city[tp] == -1) cont++; else sum -= city[tp]; city[tp] = inp[id].cost; sum += city[tp]; } } id++; } lft[i] = lft[i - 1]; if (cont == n) lft[i] = min(lft[i], sum); } memset(city, -1, sizeof city); cont = 0; sum = 0; id = m - 1; rgt[1000001] = 10000000000000LL; for (int i = 1000000; i > 0; i--) { while (id >= 0 && i == inp[id].day) { if (inp[id].from == 0) { tp = inp[id].to; if (city[tp] == -1 || inp[id].cost < city[tp]) { if (city[tp] == -1) cont++; else sum -= city[tp]; sum += inp[id].cost; city[tp] = inp[id].cost; } } id--; } rgt[i] = rgt[i + 1]; if (cont == n) rgt[i] = min(rgt[i], sum); } long long fans = 10000000000000LL; for (int i = 1; i + k < 1000000; i++) fans = min(fans, lft[i] + rgt[i + k + 1]); if (fans == 10000000000000LL) fans = -1; printf("%lld\n", fans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.util.*; import java.io.*; public class _0853_B_JuryMeeting { public static class Query implements Comparable<Query>{ int time, id, val; public Query(int id, int time, int val) { this.id = id; this.time = time; this.val = val; } public int compareTo(Query q) { return Integer.compare(this.time, q.time); } } public static class Pair{ int pos, val; public Pair(int pos, int val) {this.pos = pos; this.val = val;} } public static void main(String[] args) throws IOException { int N = readInt(), M = readInt(), K = readInt(); LinkedList<Pair> monoq[] = new LinkedList[N+1]; for(int i = 1; i<=N; i++) monoq[i] = new LinkedList<>(); PriorityQueue<Query> pq2 = new PriorityQueue<>(), pq1 = new PriorityQueue<>(), pq3 = new PriorityQueue<>(); for(int i = 1; i<=M; i++) { int t = readInt(), fr = readInt(), to = readInt(), c = readInt(); if(fr == 0) {pq2.add(new Query(to, t, c)); pq3.add(new Query(to, t, c));} else pq1.add(new Query(fr, t, c)); } while(!pq2.isEmpty()) { Query q = pq2.poll(); int id = q.id, v = q.val; while(!monoq[id].isEmpty() && monoq[id].peekLast().val >= v) monoq[id].removeLast(); monoq[id].addLast(new Pair(q.time, v)); } long tot = 0, ans = Long.MAX_VALUE, cnt = N; for(int i = 1; i<=N; i++) if(monoq[i].isEmpty()) {println(-1); exit();} else tot += monoq[i].peekFirst().val; int val[] = new int[N+1]; Arrays.fill(val, Integer.MAX_VALUE);while(!pq1.isEmpty()) { int time = pq1.peek().time; while(!pq1.isEmpty() && pq1.peek().time == time) { Query p = pq1.poll(); if(val[p.id] == Integer.MAX_VALUE) {val[p.id] = p.val; tot += val[p.id]; cnt--;} else {tot -= val[p.id]; val[p.id] = Math.min(val[p.id], p.val); tot += val[p.id];} } while(!pq3.isEmpty() && pq3.peek().time <= time+K) { int id = pq3.peek().id, t = pq3.poll().time; if(monoq[id].isEmpty() || monoq[id].peekFirst().pos != t) continue; tot -= monoq[id].pollFirst().val; if(monoq[id].isEmpty()) cnt++; else tot += monoq[id].peekFirst().val; } if(cnt == 0) ans = Math.min(tot, ans); } println(ans == Long.MAX_VALUE ? -1 : ans); exit(); } final private static int BUFFER_SIZE = 1 << 16; private static DataInputStream din = new DataInputStream(System.in); private static byte[] buffer = new byte[BUFFER_SIZE]; private static int bufferPointer = 0, bytesRead = 0; static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); public static String readLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = Read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public static String read() throws IOException { byte[] ret = new byte[1024]; int idx = 0; byte c = Read(); while (c <= ' ') { c = Read(); } do { ret[idx++] = c; c = Read(); } while (c != -1 && c != ' ' && c != '\n' && c != '\r'); return new String(ret, 0, idx); } public static int readInt() throws IOException { int ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static long readLong() throws IOException { long ret = 0; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public static double readDouble() throws IOException { double ret = 0, div = 1; byte c = Read(); while (c <= ' ') c = Read(); boolean neg = (c == '-'); if (neg) c = Read(); do { ret = ret * 10 + c - '0'; } while ((c = Read()) >= '0' && c <= '9'); if (c == '.') { while ((c = Read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private static void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private static byte Read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } static void print(Object o) { pr.print(o); } static void println(Object o) { pr.println(o); } static void flush() { pr.flush(); } static void println() { pr.println(); } static void exit() throws IOException { din.close(); pr.close(); System.exit(0); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.FileReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author OmarYasser */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, Scanner sc, PrintWriter out) { int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt(); int MAX_DAYS = (int) 1e6 + 10; ArrayList<TaskD.Query> dayQueries[] = new ArrayList[MAX_DAYS]; for (int i = 0; i < MAX_DAYS; i++) dayQueries[i] = new ArrayList<>(); for (int i = 0; i < m; i++) { int day = sc.nextInt(); TaskD.Query q = new TaskD.Query(sc.nextInt(), sc.nextInt(), sc.nextInt()); dayQueries[day].add(q); } long minCostSoFar[] = new long[n + 1]; Arrays.fill(minCostSoFar, (long) 1e12); long[] cumMinCostArrival = new long[MAX_DAYS]; for (int i = 1; i <= n; i++) cumMinCostArrival[0] += minCostSoFar[i]; for (int i = 1; i < MAX_DAYS; i++) { cumMinCostArrival[i] = cumMinCostArrival[i - 1]; for (TaskD.Query q : dayQueries[i]) { if (q.to == 0) { if (minCostSoFar[q.from] > q.cost) { cumMinCostArrival[i] = cumMinCostArrival[i] - minCostSoFar[q.from] + q.cost; minCostSoFar[q.from] = q.cost; } } } } minCostSoFar = new long[n + 1]; Arrays.fill(minCostSoFar, (long) 1e12); long[] cumMinCostDeparture = new long[MAX_DAYS]; for (int i = 1; i <= n; i++) cumMinCostDeparture[MAX_DAYS - 1] += minCostSoFar[i]; for (int i = MAX_DAYS - 2; i >= 1; i--) { cumMinCostDeparture[i] = cumMinCostDeparture[i + 1]; for (TaskD.Query q : dayQueries[i]) { if (q.from == 0) { if (minCostSoFar[q.to] > q.cost) { cumMinCostDeparture[i] = cumMinCostDeparture[i] - minCostSoFar[q.to] + q.cost; minCostSoFar[q.to] = q.cost; } } } } long best = (long) 1e12; for (int startContestDay = 1; startContestDay <= MAX_DAYS; startContestDay++) { int endContestDay = startContestDay + k - 1; if (endContestDay + 1 < MAX_DAYS) best = Math.min(best, cumMinCostArrival[startContestDay - 1] + cumMinCostDeparture[endContestDay + 1]); } out.println(best > 1L * m * (long) 1e6 ? -1 : best); } static class Query { int from; int to; int cost; Query(int f, int t, int c) { from = f; to = t; cost = c; } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() { while (st == null || !st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const int INF = 0x3f3f3f3f; const long long llINF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; void read(int &ans) { long long x = 0, w = 1; char ch = 0; while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar(); } ans = x * w; } int v[N]; long long mi[N]; long long ma[N]; set<int> se; struct node { int d, f, t, c; } a[N]; bool cmp(node x, node y) { return x.d < y.d; } int main() { memset((mi), (INF), sizeof(mi)); memset((ma), (INF), sizeof(ma)); long long ans = mi[0]; long long ans1 = mi[0]; int n, m, k, d, f, t, c; int s1, e1, s2, e2; s1 = e1 = s2 = e2 = 0; int flag = 0; read(n), read(m), read(k); for (int i = 0; i < m; ++i) { read(d), read(f), read(t), read(c); a[i] = {d, f, t, c}; flag = max(flag, d); } sort(a, a + m, cmp); long long sum = 0; for (int i = 0; i < m; ++i) { if (a[i].f == 0) continue; if (se.size() && se.find(a[i].f) != se.end()) { if (v[a[i].f] > a[i].c) { sum = sum - v[a[i].f] + a[i].c; v[a[i].f] = a[i].c; } } else { se.insert(a[i].f); v[a[i].f] = a[i].c; sum += a[i].c; } if (se.size() == n) { if (s1 == 0) e1 = s1 = a[i].d; else e1 = a[i].d; mi[a[i].d] = min(mi[a[i].d], sum); } } se.clear(); sum = 0; for (int i = m - 1; i >= 0; --i) { if (a[i].t == 0) continue; if (se.size() && se.find(a[i].t) != se.end()) { if (v[a[i].t] > a[i].c) { sum = sum - v[a[i].t] + a[i].c; v[a[i].t] = a[i].c; } } else { se.insert(a[i].t); v[a[i].t] = a[i].c; sum += a[i].c; } if (se.size() == n) { if (e2 == 0) s2 = e2 = a[i].d; else s2 = a[i].d; ma[a[i].d] = min(ma[a[i].d], sum); } } for (int i = e1; i <= flag; ++i) mi[i] = mi[e1]; for (int i = 1; i <= s2; ++i) ma[i] = ma[s2]; for (int i = s1 + 1; i <= e1; ++i) { if (mi[i] > mi[i - 1]) mi[i] = mi[i - 1]; } for (int i = e2; i > s2; --i) { if (ma[i - 1] > ma[i]) ma[i - 1] = ma[i]; } for (int i = s1; i <= flag; ++i) { if (i + k + 1 <= flag) ans = min(ans, mi[i] + ma[i + k + 1]); } if (ans >= ans1) puts("-1"); else cout << ans << "\n"; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Comparator; import java.util.PriorityQueue; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public class VandI implements Comparable<VandI> { int v, i; public VandI(int v, int i) { this.v = v; this.i = i; } @Override public int compareTo(VandI o) { int x = Integer.compare(v, o.v); if (x == 0) { return Integer.compare(i, o.i); } return x; } } enum FlightType { FlightIn, FlightOut } public class Flight { FlightType flightType; int cost; int day; int relevantJuryMember; public Flight(int day, int from, int to, int cost) { this.cost = cost; if (from == 0) { relevantJuryMember = to; flightType = FlightType.FlightOut; } else { relevantJuryMember = from; flightType = FlightType.FlightIn; } this.day = day; } } public class TripPlanner { int[] wayToMetro; PriorityQueue<Flight>[] wayOutOfMetro; Comparator<Flight> compareByDay = new Comparator<Flight>() { @Override public int compare(Flight o1, Flight o2) { return Integer.compare(o1.day, o2.day); } }; Comparator<Flight> compareByCost = new Comparator<Flight>() { @Override public int compare(Flight o1, Flight o2) { return Integer.compare(o1.cost, o2.cost); } }; int n, k; int countPriorityQueueCreation = 0; public TripPlanner(InputReader in) { n = in.nextInt(); wayToMetro = new int[n]; wayOutOfMetro = new PriorityQueue[n]; int m = in.nextInt(); k = in.nextInt(); for (; m > 0; m--) { addNewFlight(new Flight(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt())); } } void addNewFlight(Flight f) { if (f.flightType == FlightType.FlightIn) { addFlightIn(f); } else { addFlightOut(f); } } PriorityQueue<Flight> avalibleFlightIn = new PriorityQueue<>(compareByDay); void addFlightIn(Flight f) { if (wayToMetro[f.relevantJuryMember - 1] == 0) { countPriorityQueueCreation++; wayToMetro[f.relevantJuryMember - 1] = Integer.MAX_VALUE; } avalibleFlightIn.add(f); } PriorityQueue<Flight> unavailableFlightOut = new PriorityQueue<>(compareByDay); void addFlightOut(Flight f) { if (wayOutOfMetro[f.relevantJuryMember - 1] == null) { countPriorityQueueCreation++; wayOutOfMetro[f.relevantJuryMember - 1] = new PriorityQueue<>(compareByCost); } wayOutOfMetro[f.relevantJuryMember - 1].add(f); unavailableFlightOut.add(f); } long calculateAvailableKDayCost() { if (countPriorityQueueCreation != 2 * n) { return -1; } long minCost = Long.MAX_VALUE; long currentCost = 0; int numberOfPeopleWhoArrived = 0; for (PriorityQueue<Flight> q : wayOutOfMetro) { currentCost += q.peek().cost; } int currentDay = 0; while (avalibleFlightIn.isEmpty() == false) { Flight f = avalibleFlightIn.poll(); currentDay = f.day + 1; if (wayToMetro[f.relevantJuryMember - 1] > f.cost) { if (wayToMetro[f.relevantJuryMember - 1] == Integer.MAX_VALUE) { numberOfPeopleWhoArrived++; } else { currentCost -= wayToMetro[f.relevantJuryMember - 1]; } wayToMetro[f.relevantJuryMember - 1] = f.cost; currentCost += f.cost; } while (unavailableFlightOut.isEmpty() == false && unavailableFlightOut.peek().day < currentDay + k) { f = unavailableFlightOut.poll(); if (f == wayOutOfMetro[f.relevantJuryMember - 1].peek()) { wayOutOfMetro[f.relevantJuryMember - 1].remove(f); currentCost -= f.cost; if (wayOutOfMetro[f.relevantJuryMember - 1].isEmpty()) { if (minCost == Long.MAX_VALUE) return -1; return minCost; } currentCost += wayOutOfMetro[f.relevantJuryMember - 1].peek().cost; } else { wayOutOfMetro[f.relevantJuryMember - 1].remove(f); } } if (numberOfPeopleWhoArrived == n) { minCost = Math.min(minCost, currentCost); } } return minCost; } } public void solve(int testNumber, InputReader in, PrintWriter out) { TripPlanner tp = new TripPlanner(in); out.println(tp.calculateAvailableKDayCost()); } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextFloat() { return Double.parseDouble(next()); } public double nextDouble() { return Double.parseDouble(next()); } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<pair<int, pair<int, int>>> arv; vector<pair<int, pair<int, int>>> dep; for (int i = 0; i < m; i++) { int d, s, e, c; scanf("%d%d%d%d", &d, &s, &e, &c); if (e == 0) { arv.emplace_back(d, make_pair(c, s)); } else { dep.emplace_back(d, make_pair(c, e)); } } sort(arv.begin(), arv.end()); sort(dep.begin(), dep.end()); const long long kINF = 1e14; vector<long long> arv_cost(1000010, kINF); vector<int> per_city(n + 1, -1); long long cur_cost = 0; int cnt = 0; for (size_t i = 0; i < arv.size(); i++) { int day = arv[i].first; int cost = arv[i].second.first; int city = arv[i].second.second; if (per_city[city] == -1) { cnt++; per_city[city] = cost; cur_cost += cost; } else { if (per_city[city] > cost) { cur_cost -= per_city[city]; cur_cost += cost; per_city[city] = cost; } } if (cnt == n) { arv_cost[day] = cur_cost; } } for (int i = 1; i < 1000001; i++) { arv_cost[i] = min(arv_cost[i - 1], arv_cost[i]); } cur_cost = 0; cnt = 0; vector<long long> dep_cost(1000010, kINF); per_city = vector<int>(n + 1, -1); for (int i = (int)dep.size() - 1; i >= 0; i--) { int day = dep[i].first; int cost = dep[i].second.first; int city = dep[i].second.second; if (per_city[city] == -1) { cnt++; per_city[city] = cost; cur_cost += cost; } else { if (per_city[city] > cost) { cur_cost -= per_city[city]; cur_cost += cost; per_city[city] = cost; } } if (cnt == n) { dep_cost[day] = cur_cost; } } for (int i = 1000000; i > k; i--) { dep_cost[i] = min(dep_cost[i + 1], dep_cost[i]); } long long res = kINF; for (int i = 0; i + k + 1 < 1000001; i++) { if (arv_cost[i] + dep_cost[i + k + 1] < res) { res = arv_cost[i] + dep_cost[i + k + 1]; } } if (res == kINF) { printf("%d\n", -1); } else { printf("%lld\n", res); } }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline bool smin(T &a, const T &b) { return a > b ? a = b : a; } template <typename T> inline bool smax(T &a, const T &b) { return a < b ? a = b : a; } const long long N = (long long)2e6 + 6, mod = (long long)0, oo = 1e11 + 100; long long best[N], pre[N], suf[N]; vector<pair<long long, long long>> arrive[N], depart[N]; int32_t main() { long long n, m, k; cin >> n >> m >> k; for (long long j = 0; j < m; ++j) { long long d, f, t, c; cin >> d >> f >> t >> c; if (f == 0) { depart[d].push_back(make_pair(--t, c)); } else { arrive[d].push_back(make_pair(--f, c)); } } { for (long long j = 0; j < n; ++j) best[j] = oo; long long sum = n * oo; for (long long day = 0; day < N; ++day) { for (auto first : arrive[day]) { long long dif = max(0LL, best[first.first] - first.second); best[first.first] = min(best[first.first], first.second); sum -= dif; } pre[day] = sum; } } { for (long long j = 0; j < n; ++j) best[j] = oo; long long sum = n * oo; for (long long day = N - 1; day >= 0; --day) { for (auto first : depart[day]) { long long dif = max(0LL, best[first.first] - first.second); best[first.first] = min(best[first.first], first.second); sum -= dif; } suf[day] = sum; } } long long res = oo; for (long long day = 0; day < N - k - 1; ++day) { res = min(res, pre[day] + suf[day + k + 1]); } if (res >= oo) { res = -1; } cout << res << '\n'; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1000008; const long long M = 1000000000000ll; vector<pair<long long, long long> > a[N], b[N]; priority_queue<pair<long long, pair<long long, long long> > > d; pair<long long, pair<long long, long long> > dp; long long n, m, w, ans, cl[N], cr[N], c[N]; long long maxx(long long x, long long y) { if (x > y) y = x; return y; } long long minx(long long x, long long y) { if (x < y) y = x; return y; } int main(void) { std::ios::sync_with_stdio(false); long long i, j, k, p1, p2, st, ed, now; cin >> n >> m >> w; for (i = 1; i <= m; i++) { cin >> p1 >> j >> k >> p2; if (j) a[j].push_back(make_pair(p1, p2)); else b[k].push_back(make_pair(p1, p2)); } for (i = 1; i <= n; i++) { if (a[i].empty() || b[i].empty()) { cout << -1; return 0; } sort(a[i].begin(), a[i].end()); sort(b[i].begin(), b[i].end()); } st = a[1][0].first; for (i = 2; i <= n; i++) st = maxx(st, a[i][0].first); ed = b[1][b[1].size() - 1].first; for (i = 2; i <= n; i++) ed = minx(ed, b[i][b[i].size() - 1].first); while (!d.empty()) d.pop(); for (i = 1; i <= n; i++) c[i] = M; now = n * M; for (i = 1; i <= n; i++) { for (j = 0; j < (int)a[i].size() && a[i][j].first <= st; j++) if (a[i][j].second < c[i]) { now -= c[i] - a[i][j].second; c[i] = a[i][j].second; } if (j < (int)a[i].size()) d.push(make_pair(-a[i][j].first, make_pair(i, j))); } cl[st] = now; for (i = st + 1; i <= ed; i++) { while (!d.empty() && -d.top().first <= i) { dp = d.top(); d.pop(); if (a[dp.second.first][dp.second.second].second < c[dp.second.first]) { now -= c[dp.second.first] - a[dp.second.first][dp.second.second].second; c[dp.second.first] = a[dp.second.first][dp.second.second].second; } if (dp.second.second + 1 < (int)a[dp.second.first].size()) d.push(make_pair(-a[dp.second.first][dp.second.second + 1].first, make_pair(dp.second.first, dp.second.second + 1))); } cl[i] = now; } while (!d.empty()) d.pop(); for (i = 1; i <= n; i++) c[i] = M; now = n * M; for (i = 1; i <= n; i++) { for (j = (int)b[i].size() - 1; j >= 0 && b[i][j].first >= ed; j--) if (b[i][j].second < c[i]) { now -= c[i] - b[i][j].second; c[i] = b[i][j].second; } if (j >= 0) d.push(make_pair(b[i][j].first, make_pair(i, j))); } cr[ed] = now; for (i = ed - 1; i >= st; i--) { while (!d.empty() && d.top().first >= i) { dp = d.top(); d.pop(); if (b[dp.second.first][dp.second.second].second < c[dp.second.first]) { now -= c[dp.second.first] - b[dp.second.first][dp.second.second].second; c[dp.second.first] = b[dp.second.first][dp.second.second].second; } if (dp.second.second - 1 >= 0) d.push(make_pair(b[dp.second.first][dp.second.second - 1].first, make_pair(dp.second.first, dp.second.second - 1))); } cr[i] = now; } k = M; for (i = st; i + w + 1 <= ed; i++) k = minx(k, cl[i] + cr[i + w + 1]); if (k < M) cout << k; else cout << -1; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; void Weapons19() { long long int n, m, k; cin >> n >> m >> k; map<long long int, vector<pair<long long int, long long int>>> arr, dep; for (long long int i = 0; i < m; i++) { long long int a, b, c, d; cin >> a >> b >> c >> d; if (b == 0) dep[a].push_back({c, d}); else arr[a].push_back({b, d}); } vector<long long int> cost(n + 1, 1e13); long long int curval = 0; for (long long int i = 1; i <= n; i++) curval += cost[i]; vector<long long int> dp(1000006, 1e13); for (long long int i = 1; i < 1000006; i++) { for (auto a : arr[i]) { if (cost[a.first] > a.second) { curval -= cost[a.first] - a.second; cost[a.first] = a.second; } } dp[i] = curval; } for (auto &a : cost) a = 1e13; long long int ans = 1e13; curval = 1e13; curval *= n; for (long long int i = 1000006; i >= k; i--) { for (auto a : dep[i]) { if (cost[a.first] > a.second) { curval -= cost[a.first] - a.second; cost[a.first] = a.second; } } ans = min(ans, curval + dp[i - k - 1]); } if (ans > 1e12) cout << -1; else cout << ans; } int32_t main() { cout << fixed << setprecision(16); cin.sync_with_stdio(false); cin.tie(0); cout.tie(0); Weapons19(); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int n, m, K; long long M[101000]; long long B[1010000], E[1010000], S, res = 1e18; struct point { int t, a, b, c; bool operator<(const point &p) const { return t < p.t; } } w[101000]; int main() { int i; scanf("%d%d%d", &n, &m, &K); for (i = 1; i <= m; i++) { scanf("%d%d%d%d", &w[i].t, &w[i].a, &w[i].b, &w[i].c); } sort(w + 1, w + m + 1); for (i = 1; i <= n; i++) M[i] = 1e12; B[0] = S = (long long)1e12 * n; int pv = 1; for (i = 1; i <= 1000000; i++) { while (w[pv].t <= i && pv <= m) { if (!w[pv].b) { if (M[w[pv].a] > w[pv].c) { S -= M[w[pv].a] - w[pv].c; M[w[pv].a] = w[pv].c; } } pv++; } B[i] = S; } for (i = 1; i <= n; i++) M[i] = 1e12; E[1000001] = S = (long long)1e12 * n; pv = m; for (i = 1000000; i >= 1; i--) { while (w[pv].t >= i && pv >= 1) { if (!w[pv].a) { if (M[w[pv].b] > w[pv].c) { S -= M[w[pv].b] - w[pv].c; M[w[pv].b] = w[pv].c; } } pv--; } E[i] = S; } for (i = 0; i <= 1000000 - K; i++) { res = min(res, B[i] + E[i + K + 1]); } printf("%lld\n", res > 1e12 ? -1 : res); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int maxd = 2000036; const int64_t inf = 1000000000000000018; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k; cin >> n >> m >> k; vector<vector<pair<pair<int, int>, int64_t> > > edges(maxd); for (int i = 0; i < m; ++i) { pair<int, pair<pair<int, int>, int64_t> > e; cin >> e.first >> e.second.first.first >> e.second.first.second >> e.second.second; edges[e.first].push_back(e.second); } vector<int64_t> pref_days(maxd, inf), suff_days(maxd, inf); int64_t hence = 0; set<int> reachable; vector<int64_t> c_min(n + 1, inf); for (int d = maxd - 1; d >= 0; --d) { for (pair<pair<int, int>, int64_t> e : edges[d]) { if (e.first.second) { reachable.insert(e.first.second); if (c_min[e.first.second] > e.second) { if (c_min[e.first.second] != inf) hence -= c_min[e.first.second]; c_min[e.first.second] = e.second; hence += c_min[e.first.second]; } } } if (int(reachable.size()) == n) suff_days[d] = hence; } hence = 0; reachable.clear(); c_min.assign(n + 1, inf); int64_t ans = inf; for (int d = 1; d <= maxd / 2; ++d) { for (pair<pair<int, int>, int64_t> e : edges[d]) { if (e.first.first) { reachable.insert(e.first.first); if (c_min[e.first.first] > e.second) { if (c_min[e.first.first] != inf) hence -= c_min[e.first.first]; c_min[e.first.first] = e.second; hence += c_min[e.first.first]; } } } if (int(reachable.size()) == n) pref_days[d] = hence; ans = min(ans, pref_days[d] + suff_days[d + k + 1]); } if (ans == inf) ans = -1; cout << ans << '\n'; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e11; struct souvik { int d, f, t, c; } q[100003]; long long int ans[1000005], f[1000005], e[1000005]; bool comp(souvik q, souvik w) { return q.d < w.d; } int main() { int n, m, i, j, k; long long int res = 0; scanf("%d %d %d", &n, &m, &k); for (i = 0; i < m; i++) scanf("%d %d %d %d", &q[i].d, &q[i].f, &q[i].t, &q[i].c); sort(q, q + m, comp); for (i = 1; i <= n; i++) ans[i] = INF, res += ans[i]; j = 0; for (i = 0; i <= 1000000; i++) { while (j < m && q[j].d == i) { if (q[j].t == 0) { if (ans[q[j].f] > q[j].c) { res += q[j].c - ans[q[j].f]; ans[q[j].f] = q[j].c; } } j++; } f[i] = res; } res = 0; for (i = 1; i <= n; i++) ans[i] = INF, res += ans[i]; j = m - 1; for (i = 1000000; i >= 0; i--) { while (j >= 0 && q[j].d == i) { if (q[j].f == 0) { if (ans[q[j].t] > q[j].c) { res += q[j].c - ans[q[j].t]; ans[q[j].t] = q[j].c; } } j--; } e[i] = res; } res = INF + 5LL; for (i = 1; i + k <= 1000000; i++) res = min(res, f[i - 1] + e[i + k]); if (res > INF) printf("-1\n"); else printf("%lld\n", res); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct s { int d, t, c; s() {} s(int di, int ti, int ci) { d = di; t = ti; c = ci; } }; struct ans { int d; long long c; ans() {} ans(int di, long long ci) { d = di; c = ci; } }; vector<s> v[2]; vector<ans> a[2]; bool comp(s A, s B) { return A.d < B.d; } int cost[2][100010]; int main() { int n, m, kk; scanf("%d %d %d", &n, &m, &kk); for (int i = 0; i < m; ++i) { int d, f, t, c; scanf("%d %d %d %d", &d, &f, &t, &c); if (t == 0) { v[0].push_back(s(d, f, c)); } else { v[1].push_back(s(d, t, c)); } } sort(v[0].begin(), v[0].end(), comp); sort(v[1].begin(), v[1].end(), comp); reverse(v[1].begin(), v[1].end()); for (int k = 0; k < 2; ++k) { int cnt = 0; long long sum = 0, tmp = 0x3FFFFFFFFFFF; for (int i = 0; i < int(v[k].size()); ++i) { if (!cost[k][v[k][i].t]) { cost[k][v[k][i].t] = v[k][i].c; sum += v[k][i].c; cnt++; } else if (cost[k][v[k][i].t] > v[k][i].c) { sum += (v[k][i].c - cost[k][v[k][i].t]); cost[k][v[k][i].t] = v[k][i].c; } if (i + 1 == v[k].size() || v[k][i].d != v[k][i + 1].d) { if (cnt == n) { if (sum < tmp) { tmp = sum; a[k].push_back(ans(v[k][i].d, sum)); } } } } } long long fans = 0x3FFFFFFFFFFF; int f = a[1].size() - 1; for (int i = 0; i < int(a[0].size()); ++i) { while (f != -1 && a[1][f].d - a[0][i].d <= kk) { f--; } if (f != -1 && fans > 0ll + a[0][i].c + a[1][f].c) { fans = 0ll + a[0][i].c + a[1][f].c; } } if (fans == 0x3FFFFFFFFFFF) { printf("-1\n"); } else { printf("%lld\n", fans); } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.*; import java.util.*; import java.math.*; import java.util.concurrent.*; public final class round_432_d { static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); static FastScanner sc=new FastScanner(br); static PrintWriter out=new PrintWriter(System.out); static Random rnd=new Random(); static long[] bit1,bit2; static int maxn=(int)(1e6+6); static void update1(int idx,long val) { while(idx<maxn) { bit1[idx]=Math.min(bit1[idx],val);idx+=idx&-idx; } } static void update2(int idx,long val) { while(idx>0) { bit2[idx]=Math.min(bit2[idx],val);idx-=idx&-idx; } } static long query1(int idx) { long ret=Long.MAX_VALUE; while(idx>0) { ret=Math.min(ret,bit1[idx]);idx-=idx&-idx; } return ret; } static long query2(int idx) { long ret=Long.MAX_VALUE; while(idx<maxn) { ret=Math.min(ret,bit2[idx]);idx+=idx&-idx; } return ret; } static void build() { for(int i=0;i<maxn;i++) { bit1[i]=bit2[i]=Long.MAX_VALUE; } } public static void main(String args[]) throws Exception { int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt(); Node[] a=new Node[m]; for(int i=0;i<m;i++) { a[i]=new Node(sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextLong()); } Arrays.sort(a); bit1=new long[maxn];bit2=new long[maxn]; build(); int ctr=0;long[] last=new long[n+1];Arrays.fill(last,Long.MAX_VALUE); long curr=0; for(int i=0;i<m;i++) { if(a[i].t==0) { if(last[a[i].f]==Long.MAX_VALUE) { ctr++; last[a[i].f]=a[i].c; curr+=a[i].c; } else { curr-=last[a[i].f]; last[a[i].f]=Math.min(last[a[i].f],a[i].c); curr+=last[a[i].f]; } if(ctr==n) { update1(a[i].d,curr); } } } Arrays.fill(last,Long.MAX_VALUE);ctr=0;curr=0; for(int i=m-1;i>=0;i--) { if(a[i].t!=0) { if(last[a[i].t]==Long.MAX_VALUE) { ctr++; last[a[i].t]=a[i].c; curr+=a[i].c; } else { curr-=last[a[i].t]; last[a[i].t]=Math.min(last[a[i].t],a[i].c); curr+=last[a[i].t]; } if(ctr==n) { update2(a[i].d,curr); } } } long min=Long.MAX_VALUE; for(int i=2;i<maxn;i++) { long val1=query1(i-1),val2=query2(i+k); if(val1<Long.MAX_VALUE && val2<Long.MAX_VALUE) { min=Math.min(min,val1+val2); } } out.println(min<Long.MAX_VALUE?min:-1);out.close(); } private static class Node implements Comparable<Node> { int d,f,t;long c; public Node(int d,int f,int t,long c) { this.d=d;this.f=f;this.t=t;this.c=c; } public int compareTo(Node x) { return Integer.compare(this.d,x.d); } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public String next() throws Exception { return nextToken().toString(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; struct Flight { int cost; int city; }; const int D = 1e6; int main() { ios_base::sync_with_stdio(0); int n, m, k; cin >> n >> m >> k; vector<vector<Flight>> to(D + 1); vector<vector<Flight>> from(D + 1); for (int i = 0; i < m; ++i) { int d, v, u, c; cin >> d >> v >> u >> c; if (v == 0) { from[d].push_back({c, u}); } else { to[d].push_back({c, v}); } } vector<long long> f(D + 1); { vector<int> best(n + 1, -1); int cnt = 0; long long cur = 0; for (int d = 1; d <= D; ++d) { for (auto f : to[d]) { if (best[f.city] == -1) { cnt++; cur += f.cost; best[f.city] = f.cost; } else { if (f.cost < best[f.city]) { cur -= best[f.city]; cur += f.cost; best[f.city] = f.cost; } } } if (cnt == n) { f[d] = cur; } else { f[d] = -1; } } } vector<long long> g(D + 1); { vector<int> best(n + 1, -1); int cnt = 0; long long cur = 0; for (int d = D; d >= 1; --d) { for (auto f : from[d]) { if (best[f.city] == -1) { cnt++; cur += f.cost; best[f.city] = f.cost; } else { if (f.cost < best[f.city]) { cur -= best[f.city]; cur += f.cost; best[f.city] = f.cost; } } } if (cnt == n) { g[d] = cur; } else { g[d] = -1; } } } long long res = -1; for (int d = 2; d + k <= D; ++d) { if (f[d - 1] != -1 and g[d + k] != -1) { if (res == -1 or res > f[d - 1] + g[d + k]) { res = f[d - 1] + g[d + k]; } } } cout << res << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; inline char nc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { char ch = nc(); int sum = 0; while (!(ch >= '0' && ch <= '9')) ch = nc(); while (ch >= '0' && ch <= '9') sum = sum * 10 + ch - 48, ch = nc(); return sum; } const int MAXN = (int)1e5 + 10; const int MAXM = (int)1e6 + 10; const long long INF = 0x3f3f3f3f3f3f3f3f; long long f[MAXM], g[MAXM]; struct plane { int d, f, t, c; plane(int d, int f, int t, int c) : d(d), f(f), t(t), c(c) {} bool operator<(const plane &p2) const { return d < p2.d; } }; vector<plane> go, back; int gomn[MAXN], mnback[MAXN]; int n, m, k; int mn = INF, mx; bool vis[MAXN]; int main() { go.reserve(MAXM); back.reserve(MAXM); n = read(), m = read(), k = read(); for (int i = 1; i <= m; i++) { int d, f, t, c; d = read(), f = read(), t = read(), c = read(); if (f) { go.push_back(plane(d, f, t, c)); } else { back.push_back(plane(d, f, t, c)); } mn = min(mn, d); mx = max(mx, d); } sort(go.begin(), go.end()); sort(back.begin(), back.end()); memset(f, 0x3f, sizeof(f)); memset(g, 0x3f, sizeof(g)); int v = n, ptr = 0; long long sum = 0; for (int i = mn; i <= mx; i++) { f[i] = f[i - 1]; for (; go[ptr].d <= i && ptr < go.size(); ptr++) { if (!v) { f[i] = min(f[i], f[i] - gomn[go[ptr].f] + go[ptr].c); gomn[go[ptr].f] = min(gomn[go[ptr].f], go[ptr].c); } else if (!vis[go[ptr].f]) { v--; sum += go[ptr].c; gomn[go[ptr].f] = go[ptr].c; vis[go[ptr].f] = true; if (!v) f[i] = sum; } else { sum = min(sum, sum - gomn[go[ptr].f] + go[ptr].c); gomn[go[ptr].f] = min(gomn[go[ptr].f], go[ptr].c); } } } if (v) { putchar('-'); putchar('1'); return 0; } sum = 0; v = n; memset(vis, false, sizeof(vis)); ptr = back.size() - 1; for (int i = mx; i >= mn; i--) { g[i] = g[i + 1]; for (; ptr >= 0 && back[ptr].d >= i; ptr--) { if (!v) { g[i] = min(g[i], g[i] - mnback[back[ptr].t] + back[ptr].c); mnback[back[ptr].t] = min(mnback[back[ptr].t], back[ptr].c); } else if (!vis[back[ptr].t]) { v--; vis[back[ptr].t] = true; sum += back[ptr].c; mnback[back[ptr].t] = back[ptr].c; if (!v) g[i] = sum; } else { sum = min(sum, sum - mnback[back[ptr].t] + back[ptr].c); mnback[back[ptr].t] = min(mnback[back[ptr].t], back[ptr].c); } } } if (v) { putchar('-'); putchar('1'); return 0; } long long ans = INF; for (int i = mn; i <= mx && i + k + 1 <= mx; i++) ans = min(ans, f[i] + g[i + k + 1]); if (ans >= INF) { putchar('-'); putchar('1'); } else { int a[20]; a[0] = 0; while (ans) { a[++a[0]] = ans % 10; ans /= 10; } for (int i = a[0]; i >= 1; i--) { putchar(a[i] + '0'); } } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; import java.io.BufferedReader; import java.util.Comparator; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { long max = (long) (2e11) + 1; public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); ArrayList<Flight> arr = new ArrayList<>(); for (int i = 0; i < m; i++) { arr.add(new Flight(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt())); } Collections.sort(arr, new Comparator<Flight>() { public int compare(Flight flight, Flight t1) { return Integer.compare(flight.day, t1.day); } }); long[] suffMin = new long[(int) (1e6 + 1)]; Arrays.fill(suffMin, max); long[] curMin = new long[N + 1]; Arrays.fill(curMin, max); long cur = max * N; for (int i = m - 1; i >= 0; i--) { if (arr.get(i).a == 0) { cur += Math.min(0, arr.get(i).cost - curMin[arr.get(i).b]); curMin[arr.get(i).b] = Math.min(arr.get(i).cost, curMin[arr.get(i).b]); suffMin[arr.get(i).day] = cur; } } for (int j = (int) (1e6) - 1; j >= 0; j--) { suffMin[j] = Math.min(suffMin[j], suffMin[j + 1]); } long res = max; curMin = new long[N + 1]; Arrays.fill(curMin, max); cur = max * N; for (int i = 0; i < m; i++) { if (arr.get(i).day + k + 1 <= (int) (1e6)) { if (arr.get(i).b == 0) { cur += Math.min(0, arr.get(i).cost - curMin[arr.get(i).a]); curMin[arr.get(i).a] = Math.min(arr.get(i).cost, curMin[arr.get(i).a]); res = Math.min(res, cur + suffMin[arr.get(i).day + k + 1]); } } } if (res == max) { out.println(-1); } else { out.println(res); } } class Flight { int day; int a; int b; int cost; Flight(int day, int a, int b, int cost) { this.day = day; this.a = a; this.b = b; this.cost = cost; } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int maxm = 200005; struct node { int d, f, t; long long c; bool operator<(const node &b) const { return d < b.d; } } nodes[maxm]; const long long INF = 1e12; long long L[1000005], R[1000005]; long long mincost[maxm]; int main() { long long n; int m, k; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { scanf("%d%d%d%lld", &nodes[i].d, &nodes[i].f, &nodes[i].t, &nodes[i].c); } sort(nodes + 1, nodes + 1 + m); for (int i = 0; i <= 1000001; i++) L[i] = R[i] = n * INF; for (int i = 1; i <= n; i++) mincost[i] = INF; for (int i = 1, j = 1; i <= 1000000 - k + 1; i++) { L[i] = L[i - 1]; while (nodes[j].d < i && j <= m) { if (!nodes[j].t && nodes[j].c < mincost[nodes[j].f]) { L[i] = L[i] - mincost[nodes[j].f] + nodes[j].c; mincost[nodes[j].f] = nodes[j].c; } j++; } } for (int i = 1; i <= n; i++) mincost[i] = INF; for (int i = 1000000 - k + 1, j = m; i; i--) { R[i] = R[i + 1]; while (nodes[j].d >= i + k && j) { if (!nodes[j].f && nodes[j].c < mincost[nodes[j].t]) { R[i] = R[i] - mincost[nodes[j].t] + nodes[j].c; mincost[nodes[j].t] = nodes[j].c; } j--; } } long long ans = 1e18; for (int i = 1; i <= 1000000; i++) ans = min(ans, L[i] + R[i]); printf("%lld", ans > INF ? -1 : ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; const int N = 1e6 + 10; vector<pair<pair<long long, long long>, long long> > a, b; long long pre[N], nxt[N]; int prem[N], nxtm[N]; long long mask[N]; int main() { int n, m, K; scanf("%d%d%d", &n, &m, &K); for (int i = 0; i < m; i++) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (f == 0) { b.emplace_back(make_pair(d, c), t); } else { a.emplace_back(make_pair(d, c), f); } } sort(a.begin(), a.end()); int Size = 0; for (int i = 1; i <= 1000000; i++) { pre[i] = pre[i - 1]; prem[i] = prem[i - 1]; while (Size < a.size() && a[Size].first.first == i) { if (!mask[a[Size].second]) { mask[a[Size].second] = a[Size].first.second; pre[i] += a[Size].first.second; prem[i]++; } else if (mask[a[Size].second] > a[Size].first.second) { pre[i] += a[Size].first.second - mask[a[Size].second]; mask[a[Size].second] = a[Size].first.second; } Size++; } } sort(b.begin(), b.end()); Size = b.size() - 1; memset(mask, 0, sizeof mask); for (int i = 1000000; i >= 1; i--) { nxt[i] = nxt[i + 1]; nxtm[i] = nxtm[i + 1]; while (Size >= 0 && b[Size].first.first == i) { if (!mask[b[Size].second]) { nxt[i] += b[Size].first.second; mask[b[Size].second] = b[Size].first.second; nxtm[i]++; } else if (mask[b[Size].second] > b[Size].first.second) { nxt[i] += b[Size].first.second - mask[b[Size].second]; mask[b[Size].second] = b[Size].first.second; } Size--; } } long long ans = 1LL << 60; for (int i = 1; i + K + 1 <= 1000000; i++) { if (prem[i] != n) continue; if (nxtm[i + K + 1] != n) break; ans = min(ans, pre[i] + nxt[i + K + 1]); } printf("%I64d\n", (ans == (1LL << 60)) ? -1 : ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int D = 2001000; int M, n, m, k, d, x, y, c; int totgo, totback, rest, last, i; int f[N]; long long sum, lef[D], rig[D]; struct re { int day, loc, cost; } a[N], b[N]; inline bool cmp(re x, re y) { return x.day < y.day; } inline bool cmp2(re x, re y) { return x.day > y.day; } int main() { scanf("%d%d%d", &n, &m, &k); totgo = totback = 0; for (i = 1; i <= m; ++i) { scanf("%d%d%d%d", &d, &x, &y, &c); if (x > 0) { a[++totgo].loc = x; a[totgo].cost = c; a[totgo].day = d; } else { b[++totback].loc = y; b[totback].cost = c; b[totback].day = d; } } sort(a + 1, a + totgo + 1, cmp); sort(b + 1, b + totback + 1, cmp2); rest = n; sum = 0; last = 0; lef[last] = -1; for (i = 1; i <= totgo;) { int today = a[i].day; while (today == a[i].day) { if (f[a[i].loc] == 0) { f[a[i].loc] = a[i].cost; sum += a[i].cost; --rest; } else if (f[a[i].loc] > a[i].cost) { sum -= f[a[i].loc] - a[i].cost; f[a[i].loc] = a[i].cost; } ++i; } if (rest == 0) lef[today] = sum; else lef[today] = -1; for (++last; last < today; ++last) lef[last] = lef[last - 1]; } int maxst = last; memset(f, 0, sizeof(f[0]) * (n + 5)); rest = n; last = b[1].day + 1; sum = 0; rig[last] = -1; for (i = 1; i <= totback;) { int today = b[i].day; while (today == b[i].day) { if (f[b[i].loc] == 0) { f[b[i].loc] = b[i].cost; sum += b[i].cost; --rest; } else if (f[b[i].loc] > b[i].cost) { sum -= f[b[i].loc] - b[i].cost; f[b[i].loc] = b[i].cost; } ++i; } if (rest == 0) rig[today] = sum; else rig[today] = -1; for (--last; last > today; --last) rig[last] = rig[last + 1]; } for (--last; last > 0; --last) rig[last] = rig[last + 1]; long long ans = -1; for (i = 1; i <= maxst; ++i) { if (lef[i] <= 0 || rig[i + k + 1] <= 0) continue; sum = lef[i] + rig[i + k + 1]; if (ans == -1 || ans > sum) ans = sum; } cout << ans << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f; const int MAXN = 1e5 + 10; const int MAXM = 1e6 + 10; long long b[MAXN]; long long dp1[MAXM], dp2[MAXM]; struct node { long long d, f, t, c; } a[MAXN]; bool cmp(node x, node y) { return x.d < y.d; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) scanf("%lld%lld%lld%lld", &a[i].d, &a[i].f, &a[i].t, &a[i].c); sort(a, a + m, cmp); memset((b), (-1), sizeof(b)); for (int i = 0; i < MAXM; i++) { dp1[i] = 1e17; dp2[i] = 1e17; } int num = 0; long long sum = 0; for (int i = 0; i < m; i++) { if (a[i].f == 0) continue; if (b[a[i].f] == -1) { num++; sum += a[i].c; b[a[i].f] = a[i].c; } else { if (b[a[i].f] > a[i].c) { sum -= b[a[i].f]; sum += a[i].c; b[a[i].f] = a[i].c; } } if (num == n) dp1[a[i].d] = sum; } memset((b), (-1), sizeof(b)); num = 0; sum = 0; for (int i = m - 1; i >= 0; i--) { if (a[i].t == 0) continue; if (b[a[i].t] == -1) { num++; sum += a[i].c; b[a[i].t] = a[i].c; } else { if (b[a[i].t] > a[i].c) { sum -= b[a[i].t]; sum += a[i].c; b[a[i].t] = a[i].c; } } if (num == n) dp2[a[i].d] = sum; } for (int i = 1; i < MAXM; i++) dp1[i] = min(dp1[i], dp1[i - 1]); for (int i = MAXM - 2; i >= 0; i--) dp2[i] = min(dp2[i], dp2[i + 1]); long long ans = 1e17; for (int i = 0; i < MAXM - k - 1; i++) { if (dp1[i] == 1e17 || dp2[i + k + 1] == 1e17) continue; ans = min(ans, dp1[i] + dp2[i + k + 1]); } if (ans == 1e17) ans = -1; printf("%lld\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const double PI = acos(-1.0); const double EPS = 1e-6; inline int readint() { int sum = 0; char c = getchar(); bool f = 0; while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return f ? -sum : sum; } inline long long readLL() { long long sum = 0; char c = getchar(); bool f = 0; while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return f ? -sum : sum; } struct node { int d, f, t, c; }; bool cmp(const node& a, const node& b) { return a.d < b.d; } node a[100005]; bool v1[1000005], v2[1000005]; long long pre[1000005], suf[1000005]; int ct1[100005], ct2[100005]; int main() { int n = readint(), m = readint(), k = readint(); for (int i = 1; i <= m; i++) a[i].d = readint(), a[i].f = readint(), a[i].t = readint(), a[i].c = readint(); sort(a + 1, a + m + 1, cmp); k++; int st = 0, en = 0; int cnt = 0; int pos; for (int i = 1; i <= m; i++) { if (a[i].d != st && st) { pos = i; break; } if (a[i].f == 0) continue; cnt += (v1[a[i].f] == 0); if (cnt == n && !st) st = a[i].d; v1[a[i].f] = 1; if (!ct1[a[i].f]) ct1[a[i].f] = a[i].c; ct1[a[i].f] = min(a[i].c, ct1[a[i].f]); } if (!st) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) pre[st] += ct1[i]; int pppre = st; for (int i = pos; i <= m; i++) { if (a[i].f == 0) continue; if (ct1[a[i].f] > a[i].c) { pre[a[i].d] = pre[pppre] + a[i].c - ct1[a[i].f]; pppre = a[i].d; ct1[a[i].f] = a[i].c; } } for (int i = st; i <= 1000000; i++) { if (!pre[i]) pre[i] = pre[i - 1]; } cnt = 0; for (int i = m; i >= 1; i--) { if (a[i].d != en && en) { pos = i; break; } if (a[i].t == 0) continue; cnt += (v2[a[i].t] == 0); if (cnt == n && !en) en = a[i].d; v2[a[i].t] = 1; if (!ct2[a[i].t]) ct2[a[i].t] = a[i].c; ct2[a[i].t] = min(a[i].c, ct2[a[i].t]); } if (!en || en - st < k) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) suf[en] += ct2[i]; int ppre = en; for (int i = pos; i >= 1; i--) { if (a[i].t == 0) continue; if (ct2[a[i].t] > a[i].c) { suf[a[i].d] = suf[ppre] + a[i].c - ct2[a[i].t]; ppre = a[i].d; ct2[a[i].t] = a[i].c; } } for (int i = en; i >= 1; i--) { if (!suf[i]) suf[i] = suf[i + 1]; } long long mians = pre[st] + suf[en]; for (int i = st; i + k <= en; i++) { mians = min(mians, pre[i] + suf[i + k]); } printf("%lld\n", mians); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct node { int t, x, y; long long c; } a[1000005]; bool cmp(node &a, node &b) { return a.t < b.t; } long long dp1[1000005], dp2[1000005], ji[1000005], last[1000005]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) scanf("%d%d%d%lld", &a[i].t, &a[i].x, &a[i].y, &a[i].c); sort(a, a + m, cmp); for (int i = 0; i < 1000005; i++) dp1[i] = dp2[i] = 1e15; int num = 0; long long sum = 0; for (int i = 0; i < m; i++) { if (!a[i].x) continue; if (!ji[a[i].x]) { ji[a[i].x]++; num++; last[a[i].x] = a[i].c; sum += a[i].c; } else if (a[i].c < last[a[i].x]) { sum -= last[a[i].x]; sum += a[i].c; last[a[i].x] = a[i].c; } if (num == n) dp1[a[i].t] = min(dp1[a[i].t], sum); } memset(ji, 0, sizeof(ji)); num = sum = 0; for (int i = m - 1; i >= 0; i--) { if (!a[i].y) continue; if (!ji[a[i].y]) { ji[a[i].y]++; num++; last[a[i].y] = a[i].c; sum += a[i].c; } else if (a[i].c < last[a[i].y]) { sum -= last[a[i].y]; sum += a[i].c; last[a[i].y] = a[i].c; } if (num == n) dp2[a[i].t] = min(dp2[a[i].t], sum); } for (int i = 1; i < 1000005; i++) dp1[i] = min(dp1[i], dp1[i - 1]); for (int i = 1000005 - 2; i; i--) dp2[i] = min(dp2[i], dp2[i + 1]); long long ans = 1e15; for (int i = 1; i + k + 1 < 1000005; i++) ans = min(ans, dp1[i] + dp2[i + k + 1]); if (ans == 1e15) ans = -1; printf("%lld\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long power(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret *= a; a *= a; if (ret >= MOD) ret %= MOD; if (a >= MOD) a %= MOD; b >>= 1; } return ret; } long long invmod(long long x) { return power(x, MOD - 2); } vector<pair<long long, long long> > arrive[1000005], depart[1000005]; long long ans1[1000005], ans2[1000005]; long long vst[1000005]; int main() { long long n, m, k, i, d, f, t, c; cin >> n >> m >> k; for (i = 0; i < m; i++) { scanf("%lld %lld %lld %lld", &d, &f, &t, &c); if (f == 0) depart[d].push_back(make_pair(t, c)); else arrive[d].push_back(make_pair(f, c)); } memset(vst, -1, sizeof vst); ans1[0] = 10000000000003LL; long long totcost = 0, count = 0; for (i = 1; i <= 1e6 + 2; i++) { for (auto it : arrive[i]) { long long place = it.first, cost = it.second; if (vst[place] == -1) totcost += cost, vst[place] = cost, count++; else { if (cost < vst[place]) totcost -= vst[place], vst[place] = cost, totcost += vst[place]; } } if (count == n) ans1[i] = totcost; else ans1[i] = 10000000000003LL; ans1[i] = min(ans1[i - 1], ans1[i]); } memset(vst, -1, sizeof vst); totcost = 0, count = 0; ans2[1000002] = 10000000000003LL; for (i = 1e6 + 1; i >= 1; i--) { for (auto it : depart[i]) { long long place = it.first, cost = it.second; if (vst[place] == -1) totcost += cost, vst[place] = cost, count++; else { if (cost < vst[place]) totcost -= vst[place], vst[place] = cost, totcost += vst[place]; } } if (count == n) ans2[i] = totcost; else ans2[i] = 10000000000003LL; ans2[i] = min(ans2[i + 1], ans2[i]); } long long ans = 10000000000003LL; for (i = 1; i + k <= 1e6 + 2; i++) { ans = min(ans1[i - 1] + ans2[i + k], ans); } if (ans == 10000000000003LL) cout << -1; else cout << ans; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Main { static BufferedReader reader; static StringTokenizer tokenizer; static PrintWriter writer; static String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public static void main(String[] args) throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); banana(); reader.close(); writer.close(); } static boolean check(int t) { return false; } static final int MX = 1000002; static final long MXP = 1L * MX * MX; static ArrayList<ArrayList<Flight>> alfrom = new ArrayList<>(); static ArrayList<ArrayList<Flight>> alto = new ArrayList<>(); static long tot[][] = new long[2][MX]; static long diff[][] = new long[2][MX]; static void add(ArrayList<Flight> al, int ft) { Collections.sort(al); if (ft == 1) { Collections.reverse(al); } long sofar = MXP; for (Flight f : al) { if (f.cost < sofar) { diff[ft][f.day] += f.cost - sofar; sofar = f.cost; } } } static void banana() throws NumberFormatException, IOException { int n = nextInt(); int m = nextInt(); int k = nextInt(); for (int i = 0; i <= n; i++) { alfrom.add(new ArrayList<>()); alto.add(new ArrayList<>()); } for (int j = 0; j < m; j++) { int day = nextInt(); int from = nextInt(); int to = nextInt(); int cost = nextInt(); Flight f = new Flight(); f.day = day; f.cost = cost; if (from == 0) { alto.get(to).add(f); } else { alfrom.get(from).add(f); } } for (int i = 1; i <= n; i++) { ArrayList<Flight> alf = alfrom.get(i); add(alf, 0); ArrayList<Flight> alt = alto.get(i); add(alt, 1); } { long curr = 1L * n * MXP; for (int i = 0; i < MX; i++) { curr += diff[0][i]; tot[0][i] = curr; } } { long curr = 1L * n * MXP; for (int i = MX - 1; i >= 0; i--) { curr += diff[1][i]; tot[1][i] = curr; } } long res = 1L * n * MXP; for (int i = 0; i + k + 1 < MX; i++) { long cand = tot[0][i] + tot[1][i + k + 1]; if (cand < res) { res = cand; } } if (res >= MXP) { writer.println(-1); } else { writer.println(res); } } } class Flight implements Comparable<Flight> { int day; long cost; @Override public int compareTo(Flight o) { if (day == o.day) { return Long.compare(cost, o.cost); } return Integer.compare(day, o.day); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long N = 1e6 + 10; vector<pair<long long, long long> > ans[N]; vector<pair<long long, long long> > ans1[N]; long long mp[100005]; long long check[100005]; long long check1[100005]; long long p1[N], p[N]; int32_t main() { long long n, m, k; cin >> n >> m >> k; for (long long i = 1; i <= m; i++) { long long x, y, z, z1; cin >> x >> y >> z >> z1; if (y != 0) { ans[x].push_back({y, z1}); } else { ans1[x].push_back({z, z1}); } } for (long long i = 1; i <= 1000000; i++) { p1[i] = -1; p[i] = -1; } long long ans2 = 0; long long cnt = 0; for (long long i = 1000000; i >= 1; i--) { for (auto it : ans1[i]) { long long k = it.first; if (mp[k] == 0) { check[k] = it.second; ans2 += it.second; mp[k] = 1; cnt++; } else { if (check[k] > it.second) { ans2 -= check[k]; ans2 += it.second; check[k] = it.second; } } } if (cnt == n) { p[i] = ans2; } } for (long long i = 1; i <= n; i++) { mp[i] = 0; } ans2 = 0; cnt = 0; for (long long i = 1; i <= 1000000; i++) { for (auto it : ans[i]) { long long k = it.first; if (mp[k] == 0) { check1[k] = it.second; ans2 += it.second; mp[k] = 1; cnt++; } else { if (check1[k] > it.second) { ans2 -= check1[k]; ans2 += it.second; check1[k] = it.second; } } } if (cnt == n) { p1[i] = ans2; } } long long mn = 1e15; for (long long i = 1; i <= 1000000; i++) { if (i + k + 1 <= 1000000) { if (p1[i] != -1 && p[i + k + 1] != -1) { mn = min(mn, p1[i] + p[i + k + 1]); } } } if (mn == 1e15) mn = -1; cout << mn << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > go_add[1000000 + 5]; vector<pair<int, int> > ret_add[1000000 + 5]; int go_cost[1000000 + 5], ret_cost[1000000 + 5]; multiset<int> go_avl[1000000 + 5], ret_avl[1000000 + 5]; int main() { int i, n, m, k, d, u, v, c, min_d, r; long long int mn = 0, curr = 0; scanf("%d %d %d", &n, &m, &k); k += 2; for (i = 1; i <= m; i++) { scanf("%d %d %d %d", &d, &u, &v, &c); if (u == 0) ret_add[d].push_back(pair<int, int>(v, c)); else go_add[d].push_back(pair<int, int>(u, c)); } int counter = 0; for (d = 1; d <= 1000000; d++) { for (auto x : go_add[d]) { v = x.first; c = x.second; go_avl[v].insert(c); if (go_avl[v].size() == 1) counter++; } if (counter == n) break; } if (counter != n) { puts("-1"); return 0; } for (i = 1; i <= n; i++) { go_cost[i] = *go_avl[i].begin(); mn += go_cost[i]; } min_d = d; for (d = 1000000; d >= min_d + k - 1; d--) { for (auto x : ret_add[d]) { v = x.first; c = x.second; ret_avl[v].insert(c); } } for (i = 1; i <= n; i++) if (ret_avl[i].size()) { ret_cost[i] = *(ret_avl[i].begin()); mn += ret_cost[i]; } else { puts("-1"); return 0; } curr = mn; for (d = min_d + 1; d <= 1000000 - k + 1; d++) { r = d + k - 1; for (auto x : go_add[d]) { v = x.first; c = x.second; go_avl[v].insert(c); if (go_cost[v] > c) { curr -= go_cost[v]; curr += c; go_cost[v] = c; } } for (auto x : ret_add[r - 1]) { v = x.first; c = x.second; ret_avl[v].erase(ret_avl[v].find(c)); if (!ret_avl[v].size()) { cout << mn << endl; return 0; } else if (ret_cost[v] != *ret_avl[v].begin()) { curr -= ret_cost[v]; ret_cost[v] = *ret_avl[v].begin(); curr += ret_cost[v]; } } mn = min(mn, curr); } cout << mn << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int mx = 2e5 + 10; int d[mx], f[mx], t[mx], c[mx]; long long l[mx], r[mx]; pair<pair<int, int>, pair<int, int> > a[mx]; map<int, int> M, mm; int main() { ios_base::sync_with_stdio(false), cin.tie(), cout.tie(); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) cin >> a[i].first.first >> a[i].first.second >> a[i].second.first >> a[i].second.second; ; sort(a, a + m); for (int i = 0; i < m; i++) d[i] = a[i].first.first, f[i] = a[i].first.second, t[i] = a[i].second.first, c[i] = a[i].second.second; long long sum = 0; int cnt = 0; for (int i = 0; i < m; i++) { if (!t[i] && f[i]) { int u = f[i]; sum -= M[u]; if (!M[u]) M[u] = c[i], cnt++; else M[u] = min(M[u], c[i]); sum += M[u]; } if (cnt == n) l[i] = sum; else l[i] = -1; } sum = 0, cnt = 0; for (int i = m - 1; i >= 0; i--) { if (!f[i] && t[i]) { int u = t[i]; sum -= mm[u]; if (!mm[u]) mm[u] = c[i], cnt++; else mm[u] = min(mm[u], c[i]); sum += mm[u]; } if (cnt == n) r[i] = sum; else r[i] = -1; } long long ans = 1e18; int p = 0; for (int i = 0; i < m; i++) { while (p < m - 1 && d[p] - d[i] - 1 < k) p++; if (d[p] - d[i] - 1 >= k && l[i] != -1 && r[p] != -1) ans = min(ans, l[i] + r[p]); } if (ans == 1e18) cout << -1 << endl; else cout << ans << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10, M = 1e6 + 20; const long long inf = 1e12 + 10; vector<pair<int, int> > v[M][2]; int n, m, k; long long f[M], g[M]; long long curc[N]; long long solve() { fill(curc + 1, curc + n + 1, inf); g[M - 1] = inf * n; for (int i = M - 2; i >= 1; i--) { g[i] = g[i + 1]; for (auto j : v[i][0]) if (curc[j.first] > j.second) { g[i] = g[i] - curc[j.first] + j.second; curc[j.first] = j.second; } } fill(curc + 1, curc + n + 1, inf); f[0] = inf * n; for (int i = 1; i < M; i++) { f[i] = f[i - 1]; for (auto j : v[i][1]) if (curc[j.first] > j.second) { f[i] = f[i] - curc[j.first] + j.second; curc[j.first] = j.second; } } long long ans = inf; for (int i = 1; i < M - k - 5; i++) ans = min(ans, f[i] + g[i + k + 1]); if (ans == inf) return -1; return ans; } void prepare() { scanf("%d%d%d", &n, &m, &k); while (m--) { int d, x, y, wei; scanf("%d%d%d%d", &d, &x, &y, &wei); if (x == 0) v[d][0].push_back(pair<int, int>(y, wei)); else v[d][1].push_back(pair<int, int>(x, wei)); } } int main() { prepare(); cout << solve(); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int n, m, k; vector<pair<int, pair<int, int>>> dolaze; vector<pair<int, pair<int, int>>> odlaze; bool included[1000005]; priority_queue<pair<int, pair<int, int>>> pq; int maxday = 0; long long dp1[1000005]; long long dp2[1000005]; int cena[1000005]; int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < m; i++) { int day, from, to, cost; cin >> day >> from >> to >> cost; if (from == 0) { odlaze.push_back(make_pair(day, make_pair(to, cost))); } else { dolaze.push_back(make_pair(-day, make_pair(from, cost))); pq.push(dolaze[dolaze.size() - 1]); } maxday = max(maxday, day); } for (int i = 1; i <= 1e6; i++) dp1[i] = (1e8) * n, cena[i] = 1e8; int num = 0; long long res = (1e8) * n; bool flag = true; while (!pq.empty()) { int day = -pq.top().first; int who = pq.top().second.first; int cost = pq.top().second.second; pq.pop(); if (!included[who]) included[who] = 1, num++; if (cena[who] > cost) res -= cena[who], cena[who] = cost, res += cena[who]; if (num == n) dp1[day] = res; } if (num != n) flag = false; for (auto x : odlaze) pq.push(x); for (int i = 1; i <= 1e6; i++) dp2[i] = (1e8) * n, cena[i] = 1e8, included[i] = false; num = 0; res = (1e8) * n; while (!pq.empty()) { int day = pq.top().first; int who = pq.top().second.first; int cost = pq.top().second.second; pq.pop(); if (!included[who]) included[who] = 1, num++; if (cena[who] > cost) res -= cena[who], cena[who] = cost, res += cena[who]; if (num == n) dp2[day] = res; } if (num != n) flag = false; for (int i = 2; i <= 1e6; i++) { dp1[i] = min(dp1[i], dp1[i - 1]); } for (int i = 1e6 - 1; i > 0; i--) { dp2[i] = min(dp2[i], dp2[i + 1]); } if (!flag) cout << -1; else { res = 1e14; bool ok = false; for (int i = 1; i <= maxday; i++) { if (i + k + 1 > 1e6) break; if (dp1[i] == (1e8) * n || dp2[i + k + 1] == (1e8) * n) continue; res = min(res, dp1[i] + dp2[i + k + 1]); ok = true; } if (ok) cout << res; else cout << -1; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f3f3f3f3f; struct node { int d, f, t, c; } a[100005]; int b[100005]; long long dp[1000005]; long long dp1[1000005]; bool cmp(node x, node y) { return x.d < y.d; } int main() { int n, m, k; while (~scanf("%d%d%d", &n, &m, &k)) { for (int i = 0; i < m; i++) scanf("%d%d%d%d", &a[i].d, &a[i].f, &a[i].t, &a[i].c); sort(a, a + m, cmp); int d = a[m - 1].d; for (int i = 1; i <= d; i++) dp[i] = INF; for (int i = 1; i <= n; i++) b[i] = 0; int num = 0; long long ans = 0; for (int i = 0; i < m; i++) { if (a[i].f == 0) continue; if (!b[a[i].f]) { num++; ans += a[i].c; b[a[i].f] = a[i].c; } else { if (a[i].c < b[a[i].f]) { ans -= b[a[i].f]; b[a[i].f] = a[i].c; ans += a[i].c; } } if (num == n) dp[a[i].d] = min(dp[a[i].d], ans); } for (int i = 2; i <= d; i++) dp[i] = min(dp[i - 1], dp[i]); for (int i = 1; i <= d; i++) dp1[i] = INF; for (int i = 1; i <= n; i++) b[i] = 0; num = 0; ans = 0; for (int i = m - 1; i >= 0; i--) { if (a[i].t == 0) continue; if (!b[a[i].t]) { num++; ans += a[i].c; b[a[i].t] = a[i].c; } else { if (a[i].c < b[a[i].t]) { ans -= b[a[i].t]; b[a[i].t] = a[i].c; ans += a[i].c; } } if (num == n) dp1[a[i].d] = min(dp1[a[i].d], ans); } for (int i = d - 1; i >= 1; i--) dp1[i] = min(dp1[i + 1], dp1[i]); ans = INF; for (int i = 1; i + k < d; i++) { if (dp[i] == INF || dp1[i + k + 1] == INF) continue; ans = min(ans, dp[i] + dp1[i + k + 1]); } if (ans == INF) printf("-1\n"); else printf("%lld\n", ans); } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, M = 1e6 + 10; int n, m, k; long long cost[N], a[M], b[M]; struct node { int d, f, t, c; } s[N]; bool cmp(node a, node b) { return a.d < b.d; } int main() { while (~scanf("%d%d%d", &n, &m, &k)) { for (int i = 1; i <= m; i++) scanf("%d%d%d%d", &s[i].d, &s[i].f, &s[i].t, &s[i].c); sort(s + 1, s + m + 1, cmp); for (int i = 1; i <= n; i++) cost[i] = 1e12; long long now = 0; int cc = 1; for (int i = 1; i <= n; i++) now += cost[i]; for (int i = 1; i <= 1000000; i++) { while (cc <= m && s[cc].d == i) { if (s[cc].t == 0 && s[cc].c < cost[s[cc].f]) { now += s[cc].c - cost[s[cc].f]; cost[s[cc].f] = s[cc].c; } cc++; } a[i] = now; } for (int i = 1; i <= n; i++) cost[i] = 1e12; now = 0, cc = m; for (int i = 1; i <= n; i++) now += cost[i]; for (int i = 1000000; i >= 1; i--) { while (cc >= 1 && s[cc].d == i) { if (s[cc].f == 0 && s[cc].c < cost[s[cc].t]) { now += s[cc].c - cost[s[cc].t]; cost[s[cc].t] = s[cc].c; } cc--; } b[i] = now; } long long ans = 1e18; for (int i = 1; i <= 1000000 - k - 1; i++) ans = min(ans, a[i] + b[i + k + 1]); if (ans < 1e12) printf("%I64d\n", ans); else puts("-1"); } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const double pi = 3.141592653589793; long long inf = 1000000000000000LL; int flag[21111111]; vector<pair<long long, long long> > vec[1111111], vecc[1111111]; long long f[2111111]; long long s[2111111]; long long low[2111111]; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m, k; long long a, x, y, c; cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> a >> x >> y >> c; if (x == 0) { vecc[a].push_back(make_pair(y, c)); } else vec[a].push_back(make_pair(x, c)); } c = 0; int ind = 0; memset(flag, 0, sizeof flag); long long cost = 0; int d1 = -1, d2 = -1; for (int i = 0; i <= n; i++) low[i] = inf; for (int i = 1; i <= 1000000; i++) { s[i] = inf; for (pair<long long, long long> x : vec[i]) { long long ct = x.first, cs = x.second; if (flag[ct] == 0) { flag[ct] = 1; cost += cs; low[ct] = cs; c++; } else if (low[ct] > cs) { cost -= low[ct]; low[ct] = cs; cost += cs; } } if (c == n) { s[i] = min(s[i], cost); } } c = 0; ind = m - 1; memset(flag, 0, sizeof flag); cost = 0; for (int i = 0; i <= n; i++) low[i] = inf; for (int i = 1000000; i >= 1; i--) { f[i] = inf; for (pair<long long, long long> x : vecc[i]) { long long ct = x.first, cs = x.second; if (flag[ct] == 0) { flag[ct] = 1; cost += cs; low[ct] = cs; c++; } else if (low[ct] > cs) { cost -= low[ct]; low[ct] = cs; cost += cs; } } if (c == n) { f[i] = min(f[i], cost); } } long long ans = inf; for (int i = 1; i <= 1000000; i++) { if (i + k + 1 <= 1000000) { ans = min(ans, s[i] + f[i + k + 1]); } } if (ans >= inf || ans <= 0) { cout << "-1" << endl; } else { cout << ans << endl; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; const long long INF = 1e18; struct node { long long d, f, t, c; } a[N]; long long b[N]; long long dp[1000006], dp1[1000005]; bool cmp(node p, node q) { return p.d < q.d; } int main() { int n, m, k; long long x = 1000000, y = -1; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { scanf("%lld%lld%lld%lld", &a[i].d, &a[i].f, &a[i].t, &a[i].c); } sort(a + 1, a + 1 + m, cmp); for (int i = 1; i <= 1000001; i++) b[i] = -1, dp[i] = INF; long long sum = 0, s = 0; for (int i = 1; i <= m; i++) { if (a[i].f == 0) continue; if (b[a[i].f] == -1) { s++; b[a[i].f] = a[i].c; sum += a[i].c; } else { if (b[a[i].f] > a[i].c) { sum -= b[a[i].f]; sum += a[i].c; b[a[i].f] = a[i].c; } } if (s == n) { dp[a[i].d] = sum; x = min(x, a[i].d); } } for (int i = 1; i <= 1000001; i++) b[i] = -1, dp1[i] = INF; sum = 0; s = 0; for (int i = m; i >= 1; i--) { if (a[i].t == 0) continue; if (b[a[i].t] == -1) { s++; b[a[i].t] = a[i].c; sum += a[i].c; } else { if (b[a[i].t] > a[i].c) { sum -= b[a[i].t]; sum += a[i].c; b[a[i].t] = a[i].c; } } if (s == n) { dp1[a[i].d] = sum; y = max(y, a[i].d); } } for (int i = x + 1; i <= 1000000; i++) dp[i] = min(dp[i - 1], dp[i]); for (int i = y - 1; i >= 1; i--) dp1[i] = min(dp1[i + 1], dp1[i]); long long ans = INF; for (int i = 1; i <= 1000000 - k - 1; i++) { if (dp[i] == INF || dp1[i + k + 1] == INF) continue; if (ans > dp[i] + dp1[i + k + 1]) { ans = dp[i] + dp1[i + k + 1]; } } if (ans == INF) cout << -1 << endl; else cout << ans << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct flight { int d, f, t, c; flight() {} flight(int d, int f, int t, int c) : d(d), f(f), t(t), c(c) {} bool operator<(const flight& a) const { return d > a.d; } }; int n, m, k; priority_queue<flight> arr, dep; priority_queue<pair<int, int> > cheap[100010]; long long minarr[100010], arrcost, depcost; int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (t == 0) { arr.push(flight(d, f, t, c)); } else { dep.push(flight(d, f, t, c)); cheap[t].push(make_pair(-c, d)); } } for (int i = 1; i <= n; i++) { if (cheap[i].empty()) { cout << -1 << '\n'; return 0; } depcost += -cheap[i].top().first; } memset(minarr, -1, sizeof(minarr)); int num = 0; long long ans = -1; while (!arr.empty()) { flight f = arr.top(); arr.pop(); int u = f.f; if (minarr[u] == -1) { num++; minarr[u] = f.c; arrcost += f.c; } else if (f.c < minarr[u]) { arrcost += f.c - minarr[u]; minarr[u] = f.c; } if (num < n) continue; bool done = 0; while (!dep.empty() && dep.top().d <= f.d + k) { flight df = dep.top(); dep.pop(); depcost -= -cheap[df.t].top().first; while (!cheap[df.t].empty() && cheap[df.t].top().second <= df.d) cheap[df.t].pop(); if (cheap[df.t].empty()) { done = 1; break; } depcost += -cheap[df.t].top().first; } if (done) break; if (ans == -1 || arrcost + depcost < ans) { ans = arrcost + depcost; } } cout << ans << '\n'; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; template <class T> inline void upd1(T& a, T b) { a > b ? a = b : 0; } template <class T> inline void upd2(T& a, T b) { a < b ? a = b : 0; } struct ano { operator long long() { long long x = 0, y = 0, c = getchar(); while (c < 48) y = c == 45, c = getchar(); while (c > 47) x = x * 10 + c - 48, c = getchar(); return y ? -x : x; } } buf; const long long inf = 1e12; const int N = 1e5 + 5; vector<tuple<int, int, int>> a, b; int n = buf, c[N]; long long f[N], g[N], t[N]; template <class T> void sol(T a, T b, long long* c) { fill_n(t + 1, n, inf); long long s = n * inf; for (; a < b; ++a) { int u = get<1>(*a), w = get<2>(*a); if (w < t[u]) s -= t[u] - w, t[u] = w; *c++ = s; } } int main() { int m = buf, k = buf; while (m--) { int t = buf, u = buf, v = buf, w = buf; if (u) a.push_back(make_tuple(t, u, w)); else b.push_back(make_tuple(t, v, w)); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); reverse(b.begin(), b.end()); sol(a.begin(), a.end(), f); sol(b.begin(), b.end(), g); for (int i = 0; i < a.size(); ++i) c[i] = get<0>(a[i]); long long s = inf; for (int i = 0; i < b.size(); ++i) { int j = upper_bound(c, c + a.size(), get<0>(b[i]) - k - 1) - c - 1; if (~j) upd1(s, g[i] + f[j]); } printf( "%I64d" "\n", s < inf ? s : -1); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct sg { sg(long long _a, long long _b, long long _c) { num = _a; day = _b; cost = _c; } long long num; long long day; long long cost; }; bool mmm(const sg i, const sg j) { if (i.day != j.day) return i.day < j.day; else return i.cost < j.cost; } bool mmm2(const sg i, const sg j) { if (i.day != j.day) return i.day > j.day; else return i.cost < j.cost; } const long long M = LLONG_MAX / 2; vector<sg> to, back; long long n, m, k, q, w, e, r, chk[100001], hap[1000010][2], hap2[1000010][2]; long long dab = M, cnt; int main() { long long i, j; scanf("%lld %lld %lld", &n, &m, &k); for (i = 0; i < m; i++) { scanf("%lld %lld %lld %lld", &q, &w, &e, &r); if (e == 0) to.push_back(sg(w, q, r)); else if (w == 0) back.push_back(sg(e, q, r)); } sort(to.begin(), to.end(), mmm); sort(back.begin(), back.end(), mmm2); long long v = 0, vv = 0; for (i = 0; i < to.size(); i++) { if (hap[to[i].day][1] == 0) { hap[to[i].day][1] = v; hap[to[i].day][0] = vv; } if (chk[to[i].num] == 0) { hap[to[i].day][1]++; chk[to[i].num] = to[i].cost; hap[to[i].day][0] += to[i].cost; } else { if (to[i].cost < chk[to[i].num]) { hap[to[i].day][0] -= chk[to[i].num]; chk[to[i].num] = to[i].cost; hap[to[i].day][0] += to[i].cost; } } v = hap[to[i].day][1]; vv = hap[to[i].day][0]; } for (i = 1; i <= 1000000; i++) { if (hap[i][0] == 0 && hap[i][1] == 0) { hap[i][0] = hap[i - 1][0]; hap[i][1] = hap[i - 1][1]; } } memset(chk, 0, sizeof chk); v = 0; vv = 0; for (i = 0; i < back.size(); i++) { if (hap2[back[i].day][1] == 0) { hap2[back[i].day][1] = v; hap2[back[i].day][0] = vv; } if (chk[back[i].num] == 0) { hap2[back[i].day][1]++; chk[back[i].num] = back[i].cost; hap2[back[i].day][0] += back[i].cost; } else { if (back[i].cost < chk[back[i].num]) { hap2[back[i].day][0] -= chk[back[i].num]; chk[back[i].num] = back[i].cost; hap2[back[i].day][0] += back[i].cost; } } v = hap2[back[i].day][1]; vv = hap2[back[i].day][0]; } for (i = 1000000; i >= 1; i--) { if (hap2[i][0] == 0 && hap2[i][1] == 0) { hap2[i][0] = hap2[i + 1][0]; hap2[i][1] = hap2[i + 1][1]; } } int g; for (i = 1; i <= 1000000; i++) { g = i + k + 1; if (g > 1000000) break; if (hap[i][1] != n || hap2[g][1] != n) continue; cnt = hap[i][0] + hap2[g][0]; if (dab > cnt) dab = cnt; } if (dab == M) printf("-1"); else printf("%lld", dab); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int n, m, k; long long pref[1000010], suf[1000010], pref_sz[1000010], suf_sz[1000010], cost[1000010]; vector<pair<long long, long long> > a[1000010], b[1000010]; int main() { long long d, x, y, c; cin >> n >> m >> k; for (int i = 0; i < m; ++i) { cin >> d >> x >> y >> c; if (!y) { a[d].push_back(make_pair(x, c)); } else { b[d].push_back(make_pair(y, c)); } } memset(cost, 0, sizeof(cost)); for (int i = 1; i <= 1000000; ++i) { pref[i] = pref[i - 1]; pref_sz[i] = pref_sz[i - 1]; for (int j = 0; j < a[i].size(); ++j) { if (!cost[a[i][j].first]) { ++pref_sz[i]; cost[a[i][j].first] = a[i][j].second; pref[i] += a[i][j].second; } else { if (cost[a[i][j].first] > a[i][j].second) { pref[i] -= cost[a[i][j].first]; cost[a[i][j].first] = a[i][j].second; pref[i] += a[i][j].second; } } } } memset(cost, 0, sizeof(cost)); for (int i = 1000000; i >= 1; --i) { suf[i] = suf[i + 1]; suf_sz[i] = suf_sz[i + 1]; for (int j = 0; j < b[i].size(); ++j) { if (!cost[b[i][j].first]) { ++suf_sz[i]; cost[b[i][j].first] = b[i][j].second; suf[i] += b[i][j].second; } else { if (cost[b[i][j].first] > b[i][j].second) { suf[i] -= cost[b[i][j].first]; cost[b[i][j].first] = b[i][j].second; suf[i] += b[i][j].second; } } } } long long ans = 8000000000000000000; for (int i = 1; i + k + 1 <= 1000000; ++i) { if (pref_sz[i] == n && suf_sz[i + k + 1] == n) { ans = min(ans, (long long)pref[i] + (long long)suf[i + k + 1]); } } if (ans == 8000000000000000000) { cout << -1; } else { cout << ans; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.util.*; import java.io.*; import java.math.*; public class D{ static int n,m,k; static enum ComMode{Time,Cost}; static ComMode mode; static class edge implements Comparable<edge>{ public int t,u,v; public long c; public edge(int t, int u, int v, long c){ this.t=t; this.u=u; this.v=v; this.c=c; } @Override public int compareTo(edge o) { return Integer.compare(this.t, o.t); } } static edge e[]; static long res,arr[],dep[],f[],sumf,cf; static void process() { mode=ComMode.Time; f=new long [n+2]; int i,v,t; arr=new long[2000003]; Arrays.sort(e); Arrays.fill(arr, -1); Arrays.fill(f, -1); cf=0; sumf=0; i=0; for (t=0;t<arr.length;t++) { if (t>0) if (arr[t-1]>=0) arr[t]=arr[t-1]; for (;i<m;i++) { if (e[i].t>t) break; v=e[i].u; if (e[i].v==0) { if (f[v]==-1) { f[v]=e[i].c; sumf+=f[v]; cf++; }else { if (f[v]>e[i].c) { sumf-=f[v]; f[v]=e[i].c; sumf+=f[v]; } } if (cf==n) { if (arr[t]==-1) arr[t]=sumf; else arr[t]=Math.min(arr[t],sumf); if (t>0) if (arr[t-1]>=0) { if (arr[t]>=0) arr[t]=Math.min(arr[t-1],arr[t]); else arr[t]=arr[t-1]; } } } } } dep=new long[2000003]; Arrays.fill(dep, -1); Arrays.fill(f, -1); cf=0; sumf=0; i=m-1; for (t=dep.length-2;t>=0;t--){ if (dep[t+1]>=0) dep[t]=dep[t+1]; for (;i>=0;i--){ if (e[i].t<t) break; v=e[i].v; if (e[i].u==0) { if (f[v]==-1) { f[v]=e[i].c; sumf+=f[v]; cf++; }else { if (f[v]>e[i].c) { sumf-=f[v]; f[v]=e[i].c; sumf+=f[v]; } } if (cf==n) { if (dep[t]==-1) { dep[t]=sumf; } else dep[t]=Math.min(dep[t],sumf); if (t<dep.length-1) if (dep[t+1]>=0) { if (dep[t]>=0) dep[t]=Math.min(dep[t+1],dep[t]); else dep[t]=dep[t+1]; } } } } } res=-1; for (i=0;(i+k+1)<dep.length;i++) if ((arr[i]>-1)&&(dep[i+k+1]>-1)){ if (res<0) res=arr[i]+dep[i+k+1]; else res=Math.min(res, arr[i]+dep[i+k+1]); } } static void MainMethod()throws Exception{ n=reader.nextInt(); m=reader.nextInt(); k=reader.nextInt(); e=new edge[m]; int i; int d,f,t; long c; for (i=0;i<m;i++) { d=reader.nextInt(); f=reader.nextInt(); t=reader.nextInt(); c=reader.nextLong(); e[i]=new edge(d, f, t, c); } process(); printer.println(res); } public static void main(String[] args)throws Exception{ MainMethod(); printer.close(); } static void halt(){ printer.close(); System.exit(0); } static PrintWriter printer=new PrintWriter(new OutputStreamWriter(System.out)); static class reader{ static BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer token=new StringTokenizer(""); static String readNextLine() throws Exception{ return bReader.readLine(); } static String next() throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return token.nextToken(); } static int nextInt()throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return Integer.parseInt(token.nextToken()); } static long nextLong()throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return Long.parseLong(token.nextToken()); } static double nextDouble()throws Exception{ while (token.hasMoreTokens()==false){ token=new StringTokenizer(bReader.readLine()); } return Double.parseDouble(token.nextToken()); } } static class MyMathCompute{ static long [][] MatrixMultiplyMatrix(long [][] A, long [][] B, long mod) throws Exception{ int n=A.length, m=B[0].length; int p=A[0].length; int i,j,k; if (B.length!=p) throw new Exception("invalid matrix input"); long [][] res=new long [n][m]; for (i=0;i<n;i++) for (j=0;j<m;j++){ if (A[i].length!=p) throw new Exception("invalid matrix input"); res[i][j]=0; for (k=0;k<p;k++) res[i][j]=(res[i][j]+((A[i][k]*B[k][j])% mod))% mod; } return res; } static double [][] MatrixMultiplyMatrix(double [][] A, double [][] B ) throws Exception{ int n=A.length, m=B[0].length; int p=A[0].length; int i,j,k; if (B.length!=p) throw new Exception("invalid matrix input"); double [][] res=new double [n][m]; for (i=0;i<n;i++) for (j=0;j<m;j++){ if (A[i].length!=p) throw new Exception("invalid matrix input"); res[i][j]=0; for (k=0;k<p;k++) res[i][j]=res[i][j]+(A[i][k]*B[k][j]); } return res; } static long [][] MatrixPow(long [][] A,long n, long mod) throws Exception{ if (n==1) return A; long [][] res=MatrixPow(A, n/2, mod); res=MatrixMultiplyMatrix(res, res, mod); if ((n % 2) == 1) res=MatrixMultiplyMatrix(A,res, mod); return res; } static double [][] MatrixPow(double [][] A,long n) throws Exception{ if (n==1) return A; double[][] res=MatrixPow(A, n/2); res=MatrixMultiplyMatrix(res, res); if ((n % 2) == 1) res=MatrixMultiplyMatrix(A,res); return res; } static long pow(long a,long n,long mod){ a= a % mod; if (n==0) return 1; long k=pow(a,n/2,mod); if ((n % 2)==0) return ((k*k)%mod); else return (((k*k) % mod)*a) % mod; } static double pow(double a,long n){ if (n==0) return 1; double k=pow(a,n/2); if ((n % 2)==0) return (k*k); else return (((k*k) )*a) ; } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; vector<pair<int, pair<int, int> > > go, Back; int cost[1000002], dp[1000005]; long long pref[1000005], suf[1000005]; int main() { int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (f) { go.push_back({d, {f, c}}); } else { Back.push_back({d, {t, c}}); } } sort(go.begin(), go.end()); int p = 0, sz = 0; long long sum = 0; for (int i = 1; i <= 1000000; i++) { if (sz == n) { pref[i] = sum; } while (p < go.size() && go[p].first <= i) { int cut = go[p].first; int id = go[p].second.first; int how = go[p].second.second; if (dp[id] == 0) { sz++; sum += how; dp[id] = how; } else if (how < dp[id]) { sum -= dp[id]; sum += how; dp[id] = how; } p++; } } sort(Back.begin(), Back.end()); reverse(Back.begin(), Back.end()); p = 0, sz = 0; sum = 0; memset(dp, 0, sizeof(dp)); for (int i = 1000000; i >= 1; i--) { if (sz == n) { suf[i] = sum; } while (p < Back.size() && Back[p].first >= i) { int cut = Back[p].first; int id = Back[p].second.first; int how = Back[p].second.second; if (dp[id] == 0) { sz++; dp[id] = how; sum += how; } else if (how < dp[id]) { sum -= dp[id]; sum += how; dp[id] = how; } p++; } } priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > my; for (int i = 1; i <= 1000000; i++) if (suf[i]) my.push({suf[i], i}); long long res = 1e15; for (int i = 1; i <= 1000000; i++) { if (pref[i]) { while (!my.empty() && my.top().second <= i + k - 2) { my.pop(); } if (!my.empty()) res = min(res, pref[i] + my.top().first); } } if (res >= 1e15) res = -1; cout << res << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; int ans, n, m, f1[1000005], vis[1000005], vis1[1000005], num, g1[1000005], k; long long f[1000005], g[1000005]; struct he { int d, s, t, c; } a[1000005]; bool cmp(he a, he b) { return a.d < b.d; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%d", &a[i].d, &a[i].s, &a[i].t, &a[i].c); } sort(a + 1, a + 1 + m, cmp); for (int i = 1; i <= m; i++) { f[i] = f[i - 1]; if (a[i].t == 0) { if (!vis[a[i].s]) { vis[a[i].s] = a[i].c; num++; f[i] += (long long)a[i].c; } else { if (a[i].c < vis[a[i].s]) { f[i] = f[i] - (long long)vis[a[i].s] + (long long)a[i].c; vis[a[i].s] = a[i].c; } } } f1[i] = num; } num = 0; for (int i = m; i >= 1; i--) { g[i] = g[i + 1]; if (a[i].s == 0) { if (!vis1[a[i].t]) { vis1[a[i].t] = a[i].c; num++; g[i] += (long long)a[i].c; } else { if (a[i].c < vis1[a[i].t]) { g[i] = g[i] - (long long)vis1[a[i].t] + (long long)a[i].c; vis1[a[i].t] = a[i].c; } } } g1[i] = num; } int pos1 = 1; int pos2 = m; while ((a[pos1].t != 0 || f1[pos1] != n) && pos1 <= m) pos1++; if (pos1 > m) { printf("-1\n"); return 0; } while ((a[pos2].s != 0 || a[pos2].d - a[pos1].d > k || g1[pos2] != n) && pos2 >= 0) pos2--; if (pos2 == 0) { printf("-1\n"); return 0; } while ((a[pos2].d - a[pos1].d <= k || a[pos2].s != 0) && pos2 <= m) pos2++; if (g1[pos2] != n || pos2 > m) { printf("-1\n"); return 0; } pos1 = pos2; while (pos1 >= 1 && (a[pos1].t != 0 || a[pos2].d - a[pos1].d <= k)) pos1--; long long ans = (long long)1e16; while (pos2 <= m && pos1 <= m) { ans = min(ans, f[pos1] + g[pos2]); pos1++; while ((a[pos1].t != 0 || f1[pos1] != n) && pos1 <= m) pos1++; while ((a[pos2].s != 0 || a[pos2].d - a[pos1].d <= k || g1[pos2] != n) && pos2 <= m) pos2++; } printf("%I64d\n", ans); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; long long arr_day[1000010], dep_day[1000010]; int main() { long long n, m, k; cin >> n >> m >> k; vector<pair<long long, pair<long long, long long> > > arrs, deps; for (int i = 0; i < m; i++) { long long d, f, t, c; scanf("%lld%lld%lld%lld", &d, &f, &t, &c); if (f == 0) { deps.push_back(make_pair(d, make_pair(t, c))); } else { arrs.push_back(make_pair(d, make_pair(f, c))); } } sort(arrs.begin(), arrs.end()); sort(deps.rbegin(), deps.rend()); unordered_map<long long, long long> arr_cost, dep_cost; long long t_cost = 0; for (int i = 0; i < arrs.size(); i++) { long long from = arrs[i].second.first; long long cost = arrs[i].second.second; long long day = arrs[i].first; if (arr_cost.find(from) != arr_cost.end()) { long long prev = arr_cost[from]; if (cost < prev) { arr_cost[from] = cost; t_cost -= (prev - cost); } } else { arr_cost[from] = cost; t_cost += cost; } if (arr_cost.size() == n) arr_day[day] = t_cost; } t_cost = 0; for (int i = 0; i < deps.size(); i++) { long long to = deps[i].second.first; long long cost = deps[i].second.second; long long day = deps[i].first; if (dep_cost.find(to) != dep_cost.end()) { long long prev = dep_cost[to]; if (cost < prev) { dep_cost[to] = cost; t_cost -= (prev - cost); } } else { dep_cost[to] = cost; t_cost += cost; } if (dep_cost.size() == n) dep_day[day] = t_cost; } for (int i = 1; i <= 1000005; i++) { if (arr_day[i] == 0 || (arr_day[i] > arr_day[i - 1] && arr_day[i - 1] > 0)) { arr_day[i] = arr_day[i - 1]; } } for (int i = 1000005; i >= 1; i--) { if (dep_day[i] == 0 || (dep_day[i] > dep_day[i + 1] && dep_day[i + 1] > 0)) { dep_day[i] = dep_day[i + 1]; } } long long ans = LLONG_MAX; for (int i = 0; i < deps.size(); i++) { long long day = deps[i].first; if (dep_day[day] > 0) { long long last_arr_day = day - k - 1; if (last_arr_day <= 0) continue; if (arr_day[last_arr_day] > 0) { ans = min(ans, arr_day[last_arr_day] + dep_day[day]); } } } if (ans == LLONG_MAX) cout << "-1\n"; else cout << ans << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
// CodeForces Round #853 B train import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class JuryMeeting { class Flight implements Comparable<Flight> { int d,f,t,c; Flight(String []ss) { d = Integer.parseInt(ss[0]); f = Integer.parseInt(ss[1]); t = Integer.parseInt(ss[2]); c = Integer.parseInt(ss[3]); } @Override public int compareTo(Flight o) { int comp = d - o.d; return comp; } } class Delivery { long []co; long fullCost; Delivery(int n) { co = new long[n+1]; fullCost = 0; for (int i=1; i<=n; i++) { co[i] = UNOBTAIN; fullCost += co[i]; } } void addFlight(Flight fl) { int ci = (fl.f==0 ? fl.t : fl.f); if (fl.c < co[ci]) { fullCost -= (co[ci] - fl.c); co[ci] = fl.c; } } } int n,m,k; Flight []fa; int maxD; long minCost; final long UNOBTAIN = 1000111222333l; private void readData(BufferedReader bin) throws IOException { String s = bin.readLine(); String []ss = s.split(" "); n = Integer.parseInt(ss[0]); m = Integer.parseInt(ss[1]); k = Integer.parseInt(ss[2]); // read flights fa = new Flight[m]; for (int i=0; i<m; i++) { s = bin.readLine(); ss = s.split(" "); fa[i] = new Flight(ss); if (maxD < fa[i].d) { maxD = fa[i].d; } } } void printRes() { System.out.println(minCost); } private void calculate() { long []minCosta = new long[maxD+1]; long []minCostd = new long[maxD+1]; Arrays.sort(fa); Delivery dv = new Delivery(n); // flies to metropolis int last = 0; for (int i=0; i<m; i++) { if (fa[i].t==0) { for (int j=last; j<=fa[i].d; j++) { minCosta[j] = dv.fullCost; } dv.addFlight(fa[i]); last = fa[i].d+1; } } for (int j=last; j<=maxD; j++) { minCosta[j] = dv.fullCost; } // flies from metropolis , last flieghts first dv = new Delivery(n); last = maxD; for (int i=m-1; i>=0; i--) { if (fa[i].f==0) { for (int j=last; j>=fa[i].d; j--) { minCostd[j] = dv.fullCost; } dv.addFlight(fa[i]); last = fa[i].d-1; } } for (int j=last; j>=0; j--) { minCostd[j] = dv.fullCost; } minCost = UNOBTAIN; for (int i=0; i<maxD; i++) { int io = i+k-1; if (io>maxD) { break; } long cost = minCosta[i] + minCostd[io]; if (cost < minCost) { minCost = cost; } } if (minCost >= UNOBTAIN) { minCost = -1; } } public static void main(String[] args) throws IOException { // BufferedReader bin = new BufferedReader(new FileReader("cactus.in")); BufferedReader bin = new BufferedReader( new InputStreamReader(System.in)); JuryMeeting l = new JuryMeeting(); l.readData(bin); l.calculate(); l.printRes(); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int T = 1e6; int d[100005], x[100005], c[100005], id[100005]; bool cmp(int a, int b) { return d[a] < d[b]; } long long f[T + 6], r[T + 6], h[100005]; int main() { int i, j, k; int n, m, K; scanf("%d%d%d", &n, &m, &K); for (i = 1; i <= m; i++) { scanf("%d%d%d%d", d + i, x + i, &j, c + i); if (j) { d[i] += T; x[i] = j; } id[i] = i; } sort(id + 1, id + m + 1, cmp); long long ans = 1e12 * n; j = 1; fill(h + 1, h + n + 1, 1e12); for (i = 1; i <= T; i++) { while (d[k = id[j]] == i) { if (h[x[k]] > c[k]) { ans -= h[x[k]] - c[k]; h[x[k]] = c[k]; } j++; } f[i] = ans; } j = m; ans = 1e12 * n; fill(h + 1, h + n + 1, 1e12); for (i = T; i >= 1; i--) { while (d[k = id[j]] == i + T) { if (h[x[k]] > c[k]) { ans -= h[x[k]] - c[k]; h[x[k]] = c[k]; } j--; } r[i] = ans; } ans = 1e12; for (i = 1; i <= T - K - 1; i++) { ans = min(ans, f[i] + r[i + K + 1]); } printf("%I64d", ans == 1e12 ? -1 : ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int sz = 1e6 + 9; int n, m, k; long long tr[sz], tr_sum, tr_cnt; long long ret[sz], ret_sum, ret_cnt; int d, first, t, c; vector<pair<pair<int, int>, pair<int, int>>> v; vector<pair<long long, long long>> home; long long home_ans[sz]; long long ans = 1e18; int days[sz]; int main() { cin >> n >> m >> k; for (int i = 0; i < m; ++i) { cin >> d >> first >> t >> c; v.push_back(make_pair(make_pair(d, first), make_pair(t, c))); } sort(v.begin(), v.end()); for (int i = v.size() - 1; i >= 0; i--) { d = v[i].first.first; first = v[i].first.second; t = v[i].second.first; c = v[i].second.second; if (first) continue; if (!ret[t]) { ret[t] = c; ret_sum += c; ret_cnt++; } else if (ret[t] > c) { ret_sum -= ret[t]; ret[t] = c; ret_sum += c; } if (ret_cnt == n) home.push_back(make_pair(d, ret_sum)); } sort(home.begin(), home.end()); int tmp = 0; for (auto u : home) days[tmp++] = u.first; for (int i = home.size() - 1; i >= 0; i--) { if (i == home.size() - 1) home_ans[i] = home[i].second; else home_ans[i] = min(home_ans[i + 1], home[i].second); } for (int i = 0; i < v.size(); ++i) { d = v[i].first.first; first = v[i].first.second; t = v[i].second.first; c = v[i].second.second; if (t) continue; if (!tr[first]) { tr[first] = c; tr_sum += c; tr_cnt++; } else if (tr[first] > c) { tr_sum -= tr[first]; tr[first] = c; tr_sum += c; } if (tr_cnt == n) { int pos = lower_bound(days, days + home.size(), d + 1 + k) - days; if (pos >= home.size()) continue; ans = min(ans, tr_sum + home_ans[pos]); } } if (ans == 1e18) cout << -1; else cout << ans; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e5 + 9, maxm = (int)1e6 + 9, INF = 0x3f3f3f3f; const long long INF2 = 0x3f3f3f3f3f3f3f3fLL; int n, m, dt, w[maxn]; long long f[maxm], g[maxm]; struct Node { int d, f, t, c; void read() { scanf("%d%d%d%d", &d, &f, &t, &c); } bool operator<(Node const &x) const { return d < x.d; } } a[maxn]; int main() { int mx = 0; scanf("%d%d%d", &n, &m, &dt); ++dt; for (int i = 1; i <= m; ++i) { a[i].read(); mx = max(mx, a[i].d); } ++mx; sort(a + 1, a + m + 1); { int rem = n; long long cnt = 0; memset(w + 1, 0x3f, n * sizeof(int)); memset(f + 1, 0x3f, mx * sizeof(long long)); for (int i = 1; i <= m; ++i) { if (a[i].t) continue; if (a[i].c < w[a[i].f]) { if (w[a[i].f] == INF) { --rem; w[a[i].f] = 0; } cnt += a[i].c - w[a[i].f]; w[a[i].f] = a[i].c; if (!rem) f[a[i].d] = min(f[a[i].d], cnt); } } for (int i = 2; i <= mx; ++i) f[i] = min(f[i], f[i - 1]); } { int rem = n; long long cnt = 0; memset(w + 1, 0x3f, n * sizeof(int)); memset(g + 1, 0x3f, mx * sizeof(long long)); for (int i = m; i >= 1; --i) { if (a[i].f) continue; if (a[i].c < w[a[i].t]) { if (w[a[i].t] == INF) { --rem; w[a[i].t] = 0; } cnt += a[i].c - w[a[i].t]; w[a[i].t] = a[i].c; if (!rem) g[a[i].d] = min(g[a[i].d], cnt); } } for (int i = mx - 1; i >= 1; --i) g[i] = min(g[i], g[i + 1]); } long long ans = INF2; for (int i = 1; i + dt <= mx; ++i) ans = min(ans, f[i] + g[i + dt]); if (ans < INF2) printf("%I64d\n", ans); else puts("-1"); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; set<pair<pair<long long, long long>, long long> > s[2]; long long aux[100005][2], best[1000005][2]; int main() { long long n, m, k, d, f, t, c; scanf("%lld %lld %lld", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%lld %lld %lld %lld", &d, &f, &t, &c); if (t == 0) s[0].insert(make_pair(make_pair(d, f), c)); else s[1].insert(make_pair(make_pair(-d, t), c)); } for (int i = 0; i <= n; i++) { aux[i][0] = -1ll; aux[i][1] = -1ll; } for (int i = 0; i < 1000005; i++) { best[i][0] = -1ll; best[i][1] = -1ll; } long long left = n, sum = 0ll; for (auto a : s[0]) { d = a.first.first; f = a.first.second; c = a.second; if (aux[f][0] == -1) { aux[f][0] = c; sum += c; left--; } else { if (c < aux[f][0]) { sum -= aux[f][0]; aux[f][0] = c; sum += aux[f][0]; } } if (!left) { if (best[d - 1][0] == -1) best[d][0] = sum; else best[d][0] = min(best[d - 1][0], sum); } } left = n, sum = 0ll; for (auto a : s[1]) { d = -a.first.first; f = a.first.second; c = a.second; if (aux[f][1] == -1) { aux[f][1] = c; sum += c; left--; } else { if (c < aux[f][1]) { sum -= aux[f][1]; aux[f][1] = c; sum += aux[f][1]; } } if (!left) { if (best[d + 1][1] == -1) best[d][1] = sum; else best[d][1] = min(best[d + 1][1], sum); } } for (int i = 2; i <= 1000000; i++) { if (best[i][0] == -1) best[i][0] = best[i - 1][0]; } for (int i = 999999; i >= 1; i--) { if (best[i][1] == -1) best[i][1] = best[i + 1][1]; } long long ans = 1000000000000000000ll; for (long long i = 1; i + k + 1 <= 1000000; i++) { if (best[i][0] != -1 && best[i + k + 1][1] != -1) { ans = min(ans, best[i][0] + best[i + k + 1][1]); } } if (ans == 1000000000000000000ll) ans = -1ll; printf("%lld\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct flight { int d, f, t, c; }; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); vector<flight> flights(m); for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &flights[i].d, &flights[i].f, &flights[i].t, &flights[i].c); } sort(flights.begin(), flights.end(), [](const flight &a, const flight &b) { return a.d < b.d; }); vector<long long> miniCostTo(n + 1, (1LL << 50)); vector<long long> totalTo(1000001, (1LL << 50)); vector<bool> haveTo(n + 1); int toCounter = 0; long long costTo = 0; int toIndex = 0; for (int i = 0; i <= 1000000; i++) { if (toCounter == n) { totalTo[i] = costTo; } while (toIndex < m && flights[toIndex].d == i) { flight f = flights[toIndex]; if (!f.t) { if (miniCostTo[f.f] > f.c) { if (miniCostTo[f.f] != (1LL << 50)) costTo -= miniCostTo[f.f] - f.c; miniCostTo[f.f] = f.c; } if (!haveTo[f.f]) { haveTo[f.f] = true; costTo += f.c; toCounter++; } } toIndex++; } } vector<long long> miniCostFrom(n + 1, (1LL << 50)); vector<long long> totalFrom(1000001, (1LL << 50)); vector<bool> haveFrom(n + 1); int fromCounter = 0; long long costFrom = 0; int fromIndex = m - 1; for (int i = 1000000; i >= 0; i--) { if (fromCounter == n) { totalFrom[i] = costFrom; } while (fromIndex >= 0 && flights[fromIndex].d == i) { flight f = flights[fromIndex]; if (!f.f) { if (miniCostFrom[f.t] > f.c) { if (miniCostFrom[f.t] != (1LL << 50)) costFrom -= miniCostFrom[f.t] - f.c; miniCostFrom[f.t] = f.c; } if (!haveFrom[f.t]) { haveFrom[f.t] = true; costFrom += f.c; fromCounter++; } } fromIndex--; } } long long cheapest = (1LL << 50); for (int i = 0; i + k - 1 <= 1000000; i++) { cheapest = min(cheapest, totalTo[i] + totalFrom[i + k - 1]); } if (cheapest == (1LL << 50)) printf("-1\n"); else printf("%I64d\n", cheapest); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct nod { int d, f, t, c; }; long long ans1[1000005], ans2[1000005]; int cmp(nod x, nod y) { return x.d < y.d; } nod arr[100005]; int n, m, k; int vis[100005]; long long sum, num; int main() { while (cin >> n >> m >> k) { for (int i = 1; i <= m; i++) cin >> arr[i].d >> arr[i].f >> arr[i].t >> arr[i].c; sort(arr + 1, arr + m + 1, cmp); memset(vis, 0, sizeof(vis)); num = 0; sum = 0; int now = 1; for (int day = 1; day <= 1000000; day++) { for (now; now <= m && arr[now].d <= day; now++) { if (arr[now].f == 0) continue; if (vis[arr[now].f] != 0 && vis[arr[now].f] < arr[now].c) continue; if (vis[arr[now].f] == 0) num++; sum = sum - vis[arr[now].f] + arr[now].c; vis[arr[now].f] = arr[now].c; } if (num != n) ans1[day] = 9999999999999999LL; else ans1[day] = sum; } long long best = 9999999999999999LL; num = 0; sum = 0; now = m; memset(vis, 0, sizeof(vis)); for (int day = 1000000; day - k - 1 >= 1; day--) { for (now; now >= 1 && arr[now].d >= day; now--) { if (arr[now].t == 0) continue; if (vis[arr[now].t] != 0 && vis[arr[now].t] < arr[now].c) continue; if (vis[arr[now].t] == 0) num++; sum = sum - vis[arr[now].t] + arr[now].c; vis[arr[now].t] = arr[now].c; } if (num == n) best = min(best, sum + ans1[day - k - 1]); } if (best == 9999999999999999LL) best = -1; cout << best << endl; } }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import java.util.*; public class q5 { static int gcd(int a, int b) { if(b==0) return a; else return gcd(b,a%b); } public static void main(String[] args) throws IOException { Reader.init(System.in); PrintWriter out=new PrintWriter(System.out); int n=Reader.nextInt(),m=Reader.nextInt(); int k=Reader.nextInt(); ArrayList<NoD>[] flight=new ArrayList[1000001]; for(int i=0;i<=1000000;i++) flight[i]=new ArrayList<NoD>(); int[] price=new int[n+1]; int[] price2=new int[n+1]; long[] totcost=new long[1000001]; long[] totcost2=new long[1000001]; Arrays.fill(totcost, -1); Arrays.fill(totcost2, -1); for(int i=0;i<m;i++) { int a=Reader.nextInt(); int b=Reader.nextInt(); int c=Reader.nextInt(); int d=Reader.nextInt(); flight[a].add(new NoD(b,c,d)); } int unvisited=n; long sum=0; for(int i=1;i<=1000000;i++) { for(NoD a:flight[i]) { if(a.e==0) { if(price[a.s]==0) { price[a.s]=a.c; sum+=a.c; unvisited--; } else { if(a.c<price[a.s]) { sum+=a.c-price[a.s]; price[a.s]=a.c; } } } } if(unvisited==0) totcost[i]=sum; } sum=0; unvisited=n; for(int i=1000000;i>0;i--) { for(NoD a:flight[i]) { if(a.s==0) { if(price2[a.e]==0) { price2[a.e]=a.c; sum+=a.c; unvisited--; } else { if(a.c<price2[a.e]) { sum+=a.c-price2[a.e]; price2[a.e]=a.c; } } } } if(unvisited==0) totcost2[i]=sum; } long ans=-1; for(int i=2;i<=1000000;i++) { if(totcost[i-1]!=-1) { if(i+k<=1000000) { if(totcost2[i+k]!=-1) { if(ans==-1) ans=totcost[i-1]+totcost2[i+k]; else ans=Math.min(ans, totcost[i]+totcost2[i+k]); } } } } out.print(ans); out.flush(); } } class NoD{ int s,e,c; NoD(int aa,int bb,int cc){ s=aa;e=bb;c=cc; } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init() throws IOException { reader = new BufferedReader( new FileReader("detect.in")); tokenizer = new StringTokenizer(""); } static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String nextLine() throws IOException{ return reader.readLine(); } static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = int(2e5) + 5; const int LN = (int)3e6 + 7; const int inf = (int)1e9 + 7; const long long linf = (long long)1e18 + 7; int n, m, k; int u[N]; long long mn[N]; int ans[N]; int cnt = 0; vector<pair<pair<int, int>, int> > here[LN]; long long now = 0; long long pref[LN], suff[LN]; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= m; ++i) { int d, f, t, c; scanf("%d %d %d %d", &d, &f, &t, &c); here[d].push_back({{f, t}, c}); } for (int i = 1; i <= n; ++i) { mn[i] = linf; } fill(suff, suff + LN, linf); fill(pref, pref + LN, linf); for (int i = 1; i <= (int)2e6; ++i) { pref[i] = pref[i - 1]; if (cnt == n) { pref[i] = now; } for (auto j : here[i]) { int fr = j.first.first; int to = j.first.second; if (!to) { if (!u[fr]) { u[fr] = 1; mn[fr] = j.second; now += j.second; ++cnt; } else { if (mn[fr] > j.second) { now -= mn[fr]; now += j.second; mn[fr] = j.second; } } } } } now = 0; cnt = 0; for (int i = 1; i <= n; ++i) { mn[i] = linf; u[i] = 0; } for (int i = (int)2e6; i >= 1; --i) { for (auto j : here[i]) { int fr = j.first.first; int to = j.first.second; if (!fr) { if (!u[to]) { u[to] = 1; mn[to] = j.second; now += j.second; ++cnt; } else { if (mn[to] > j.second) { now -= mn[to]; now += j.second; mn[to] = j.second; } } } } suff[i] = suff[i + 1]; if (cnt == n) { suff[i] = now; } } long long ans = linf; for (int d = 1; d + k <= (int)2e6; ++d) { if (pref[d] != linf && suff[d + k] != linf) { if (ans > pref[d] + suff[d + k]) { ans = pref[d] + suff[d + k]; } } } if (ans == linf) { printf("-1\n"); return 0; } printf("%lld", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T min(T a, T b, T c, T d) { return min(a, min(b, min(c, d))); } template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } template <class T> T max(T a, T b, T c, T d) { return max(a, max(b, max(c, d))); } bool cmp(const pair<int, int>& a, const pair<int, int>& b) { return (a.first > b.first || (a.first == b.first && a.second >= b.second)); } long long GCD(long long a, long long b) { return (a % b) ? GCD(b, a % b) : b; } const string namePro = "tmp"; int d[1000009], f[1000009], t[1000009], p[1000009]; vector<pair<int, pair<int, int> > > fromCity, toCity; long long minFrom[1000009], minTo[1000009]; int check[1000009]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = (1); i <= (m); ++i) scanf("%d%d%d%d", &d[i], &f[i], &t[i], &p[i]); for (int i = (1); i <= (m); ++i) { if (t[i] == 0) fromCity.push_back(make_pair(d[i], make_pair(f[i], p[i]))); else toCity.push_back(make_pair(d[i], make_pair(t[i], p[i]))); } sort(fromCity.begin(), fromCity.end()); sort(toCity.rbegin(), toCity.rend()); int cnt = n; long long sum = 0LL; for (int i = (1); i <= (n); ++i) check[i] = 0; for (int i = (0); i <= (1000007); ++i) minFrom[i] = 1000000000000000007LL; for (int i = (0); i <= ((int)fromCity.size() - 1); ++i) { int D = fromCity[i].first; int F = fromCity[i].second.first; int P = fromCity[i].second.second; if (!check[F]) { check[F] = P; sum += P; --cnt; } else { sum = min(sum, sum - check[F] + P); check[F] = min(check[F], P); } if (!cnt) minFrom[D] = min(minFrom[D], sum); } for (int i = (1); i <= (1000007); ++i) minFrom[i] = min(minFrom[i], minFrom[i - 1]); cnt = n; sum = 0LL; for (int i = (1); i <= (n); ++i) check[i] = 0; for (int i = (0); i <= (1000007); ++i) minTo[i] = 1000000000000000007LL; for (int i = (0); i <= ((int)toCity.size() - 1); ++i) { int D = toCity[i].first; int T = toCity[i].second.first; int P = toCity[i].second.second; if (!check[T]) { check[T] = P; sum += P; --cnt; } else { sum = min(sum, sum - check[T] + P); check[T] = min(check[T], P); } if (!cnt) minTo[D] = min(minTo[D], sum); } for (int i = (1000006); i >= (0); --i) minTo[i] = min(minTo[i], minTo[i + 1]); long long ans = 1000000000000000007LL; for (int i = (0); i <= (1000003 - k); ++i) ans = min(ans, minFrom[i] + minTo[i + k + 1]); if (ans != 1000000000000000007LL) cout << ans << endl; else puts("-1"); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18 + 10; const int maxn = 1e6 + 10; struct node { int d, l, r; long long c; } a[maxn]; long long L[maxn], R[maxn], vis[maxn]; bool cmp(node x, node y) { return x.d < y.d; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; ++i) { scanf("%d%d%d%I64d", &a[i].d, &a[i].l, &a[i].r, &a[i].c); } sort(a + 1, a + 1 + m, cmp); memset(L, -1, sizeof L); memset(R, -1, sizeof R); memset(vis, 0, sizeof vis); long long cnt = 0; long long tot = 0; for (int i = 1; i <= m; ++i) { if (a[i].r != 0) continue; if (!vis[a[i].l]) { vis[a[i].l] = a[i].c; cnt++; tot += a[i].c; } else { if (vis[a[i].l] > a[i].c) { tot = tot - vis[a[i].l] + a[i].c; vis[a[i].l] = a[i].c; } } if (cnt == n) { L[i] = tot; } } memset(vis, 0, sizeof vis); cnt = 0; tot = 0; for (int i = m; i >= 1; --i) { if (a[i].l != 0) continue; if (!vis[a[i].r]) { vis[a[i].r] = a[i].c; cnt++; tot += a[i].c; } else { if (vis[a[i].r] > a[i].c) { tot = tot - vis[a[i].r] + a[i].c; vis[a[i].r] = a[i].c; } } if (cnt == n) { R[i] = tot; } } long long ans = INF; for (int i = m; i >= 1; --i) { if (R[i] == -1) continue; if (R[i + 1] != -1) R[i] = min(R[i], R[i + 1]); } for (int i = 1; i <= m; ++i) { if (L[i] == -1) continue; int day = a[i].d + k + 1; int l = i + 1, r = m, len = 50; while (len--) { int mid = (l + r) / 2; if (a[mid].d >= day) r = mid - 1; else l = mid + 1; } if (l == -1) continue; if (R[l] != -1) { ans = min(ans, L[i] + R[l]); } } if (ans == INF) { puts("-1"); } else { cout << ans << endl; } }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const long long inf = 1e15; struct node { int t, x, y; long long z; node() {} node(int _t, int _x, int _y, long long _z) : t(_t), x(_x), y(_y), z(_z) {} bool operator<(const node& p) const { return t < p.t; } } a[N]; long long A[N], B[N], C[N]; int nA[N], nB[N]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%I64d", &a[i].t, &a[i].x, &a[i].y, &a[i].z); } sort(a + 1, a + 1 + m); int cnt = 0; for (int i = 1; i <= m; i++) { if (a[i].y == 0) { if (!C[a[i].x]) A[i] = A[i - 1] + a[i].z, cnt++, C[a[i].x] = a[i].z; else { A[i] = A[i - 1] - C[a[i].x]; C[a[i].x] = min(C[a[i].x], a[i].z); A[i] += C[a[i].x]; } } else { A[i] = A[i - 1]; } nA[i] = cnt; } memset(C, 0, sizeof(C)); cnt = 0; for (int i = m; i >= 1; i--) { if (a[i].x == 0) { if (!C[a[i].y]) B[i] = B[i + 1] + a[i].z, cnt++, C[a[i].y] = a[i].z; else { B[i] = B[i + 1] - C[a[i].y]; C[a[i].y] = min(C[a[i].y], a[i].z); B[i] += C[a[i].y]; } } else { B[i] = B[i + 1]; } nB[i] = cnt; } long long ans = inf; for (int i = 1; i <= m; i++) { int pos = lower_bound(a + 1, a + 1 + m, node(a[i].t + 1 + k, 0, 0, 0)) - a; if (nA[i] != n || nB[pos] != n) continue; ans = min(ans, A[i] + B[pos]); } if (ans >= inf) printf("-1\n"); else printf("%I64d\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.*; import java.util.*; public class B { static class Flight { int day, cost; public Flight(int day, int cost) { this.day = day; this.cost = cost; } } void submit() { int n = nextInt(); int m = nextInt(); int k = nextInt(); ArrayList<Flight>[] fst = new ArrayList[n]; ArrayList<Flight>[] snd = new ArrayList[n]; for (int i = 0; i < n; i++) { fst[i] = new ArrayList<>(); snd[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int day = nextInt(); int from = nextInt(); int to = nextInt(); int cost = nextInt(); if (to == 0) { fst[from - 1].add(new Flight(day, cost)); } else { snd[to - 1].add(new Flight(day, cost)); } } Comparator<Flight> byDayIncr = new Comparator<Flight>() { @Override public int compare(Flight o1, Flight o2) { if (o1.day != o2.day) { return Integer.compare(o1.day, o2.day); } return Integer.compare(o1.cost, o2.cost); } }; int startDay = Integer.MIN_VALUE; long[] deltaFst = new long[N]; for (ArrayList<Flight> lst : fst) { if (lst.isEmpty()) { out.println(-1); return; } Collections.sort(lst, byDayIncr); int prevDay = -1, prevCost = Integer.MAX_VALUE; for (Flight f : lst) { if (f.cost >= prevCost) { continue; } if (prevDay == -1) { startDay = Math.max(startDay, f.day); deltaFst[f.day] += f.cost; } else { deltaFst[f.day] += f.cost - prevCost; } prevDay = f.day; prevCost = f.cost; } } Comparator<Flight> byDayDecr = new Comparator<Flight>() { @Override public int compare(Flight o1, Flight o2) { if (o1.day != o2.day) { return -Integer.compare(o1.day, o2.day); } return Integer.compare(o1.cost, o2.cost); } }; int endDay = Integer.MAX_VALUE; long[] deltaSnd = new long[N]; for (ArrayList<Flight> lst : snd) { if (lst.isEmpty()) { out.println(-1); return; } Collections.sort(lst, byDayDecr); int prevDay = -1, prevCost = Integer.MAX_VALUE; for (Flight f : lst) { if (f.cost >= prevCost) { continue; } if (prevDay == -1) { endDay = Math.min(endDay, f.day); deltaSnd[f.day] += f.cost; } else { deltaSnd[f.day] += f.cost - prevCost; } prevDay = f.day; prevCost = f.cost; } } for (int i = 1; i < N; i++) { deltaFst[i] += deltaFst[i - 1]; } for (int i = N - 2; i >= 0; i--) { deltaSnd[i] += deltaSnd[i + 1]; } long ans = Long.MAX_VALUE; for (int arrive = 0; arrive < N; arrive++) { int depart = arrive + k + 1; if (arrive < startDay || depart > endDay) { continue; } // System.err.println(arrive + " " + depart); ans = Math.min(ans, deltaFst[arrive] + deltaSnd[depart]); } out.println(ans == Long.MAX_VALUE ? -1 : ans); } static final int N = 1_001_000; void preCalc() { } void stress() { } void test() { } B() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); preCalc(); submit(); //stress(); //test(); out.close(); } static final Random rng = new Random(); static int rand(int l, int r) { return l + rng.nextInt(r - l + 1); } public static void main(String[] args) throws IOException { new B(); } BufferedReader br; PrintWriter out; StringTokenizer st; String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int max_days = 1e6; const long long inf = 1e12; struct Flight { int day, from, to; long long cost; }; Flight flights[100100]; long long min_cost_to[100100]; long long min_cost_from[100100]; long long prefix_cost[max_days + 1]; long long suffix_cost[max_days + 1]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) cin >> flights[i].day >> flights[i].from >> flights[i].to >> flights[i].cost; sort(flights, flights + m, [](Flight& lhs, Flight& rhs) { return lhs.day < rhs.day; }); for (int i = 0; i <= n; i++) min_cost_to[i] = min_cost_from[i] = inf; long long total_cost = n * inf; for (int i = 0; i <= max_days; i++) prefix_cost[i] = suffix_cost[i] = total_cost; for (int i = 0; i < m; i++) if (flights[i].to == 0) { if (flights[i].cost < min_cost_to[flights[i].from]) { total_cost -= min_cost_to[flights[i].from] - flights[i].cost; min_cost_to[flights[i].from] = flights[i].cost; } prefix_cost[flights[i].day] = total_cost; } for (int i = 1; i <= max_days; i++) prefix_cost[i] = min(prefix_cost[i], prefix_cost[i - 1]); total_cost = n * inf; for (int i = m - 1; i >= 0; i--) if (flights[i].from == 0) { if (flights[i].cost < min_cost_from[flights[i].to]) { total_cost -= min_cost_from[flights[i].to] - flights[i].cost; min_cost_from[flights[i].to] = flights[i].cost; } suffix_cost[flights[i].day] = total_cost; } for (int i = max_days - 1; i >= 1; i--) suffix_cost[i] = min(suffix_cost[i], suffix_cost[i + 1]); long long ans = n * inf; for (int i = 1; i + k + 1 <= max_days; i++) ans = min(ans, prefix_cost[i] + suffix_cost[i + k + 1]); cout << (ans < inf ? ans : -1); }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const long long INF = 1e12; const int SIZE = 1e6 + 10; long long BIT[2][SIZE]; void ins(long long bit[], int x, long long v) { for (; x; x -= x & -x) bit[x] += v; } long long qq(long long bit[], int x) { long long res = 0; for (; x < SIZE; x += x & -x) res += bit[x]; return res; } vector<pair<int, int> > data[2][SIZE]; int main() { int N, M, K; scanf("%d%d%d", &N, &M, &K); for (int i = 0; i < (M); ++i) { int d, f; scanf("%d%d", &d, &f); int t, c; scanf("%d%d", &t, &c); if (!f) { data[1][t].push_back(make_pair(d, c)); } else { data[0][f].push_back(make_pair(d, c)); } } for (int i = (1); i < (N + 1); ++i) { if (!((int)(data[0][i]).size()) || !((int)(data[1][i]).size())) return 0 * puts("-1"); for (int j = 0; j < (2); ++j) sort((data[j][i]).begin(), (data[j][i]).end()); int lt = 1; long long mi = INF; for (int j = 0; j < (((int)(data[0][i]).size())); ++j) { ins(BIT[0], lt - 1, -mi); ins(BIT[0], data[0][i][j].first - 1, mi); mi = min(mi, (long long)data[0][i][j].second); lt = data[0][i][j].first; } ins(BIT[0], lt - 1, -mi); ins(BIT[0], SIZE - 1, mi); lt = SIZE - 1; mi = INF; for (int j = ((int)(data[1][i]).size()) - 1; j >= 0; j--) { while (j && data[1][i][j].first == data[1][i][j - 1].first) j--; ins(BIT[1], (SIZE - lt) - 1, -mi); ins(BIT[1], (SIZE - data[1][i][j].first) - 1, mi); mi = min(mi, (long long)data[1][i][j].second); lt = data[1][i][j].first; } ins(BIT[1], (SIZE - lt) - 1, -mi); ins(BIT[1], SIZE - 1, mi); } long long ans = INF; for (int i = 2; i + K < SIZE; i++) { long long v = qq(BIT[0], i - 1) + qq(BIT[1], (SIZE - (i + K))); if (v < ans) { ans = v; } } if (ans >= INF) puts("-1"); else printf("%I64d\n", ans); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; long long fcost[N], forcost[N], rcost[N], revcost[N], fortill[N], reupto[N]; long long finans = 1e18; set<long long> s, s2; int main() { ios::sync_with_stdio(false); cin.tie(0); const long long infinity = 1e14; int n, m, k, i; cin >> n >> m >> k; long long curmin = 0; long long curmin2 = 0; for (i = 0; i < N; i++) { fcost[i] = infinity; forcost[i] = infinity; rcost[i] = infinity; revcost[i] = infinity; } vector<pair<pair<long long, long long>, pair<long long, long long> > > v(m + 1); for (i = 1; i <= m; i++) { cin >> v[i].first.first >> v[i].first.second >> v[i].second.first >> v[i].second.second; } sort(v.begin() + 1, v.end()); for (i = 1; i <= m; i++) { if (v[i].first.second != 0) { if (v[i].second.second < fcost[v[i].first.second]) { if (s.find(v[i].first.second) != s.end()) curmin -= fcost[v[i].first.second]; curmin += v[i].second.second; fcost[v[i].first.second] = v[i].second.second; s.insert(v[i].first.second); if (s.size() == n) forcost[v[i].first.first] = min(forcost[v[i].first.first], curmin); fortill[v[i].first.first] = s.size(); } } } for (i = m; i >= 1; i--) { if (v[i].first.second == 0) { if (v[i].second.second < rcost[v[i].second.first]) { if (s2.find(v[i].second.first) != s2.end()) curmin2 -= rcost[v[i].second.first]; curmin2 += v[i].second.second; rcost[v[i].second.first] = v[i].second.second; s2.insert(v[i].second.first); if (s2.size() == n) revcost[v[i].first.first] = min(revcost[v[i].first.first], curmin2); reupto[v[i].first.first] = s2.size(); } } } for (i = 1; i < N; i++) { if (fortill[i - 1] == n) { forcost[i] = min(forcost[i], forcost[i - 1]); fortill[i] = n; } } for (i = (N - 2); i >= 1; i--) { if (reupto[i + 1] == n) { revcost[i] = min(revcost[i], revcost[i + 1]); reupto[i] = n; } } int lptr = 1, rptr = k + 2; while (rptr < N) { if (fortill[lptr] == n && reupto[rptr] == n) { finans = min(finans, forcost[lptr] + revcost[rptr]); } lptr++; rptr++; } if (finans < 1e18) { cout << finans; } else { cout << -1; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; vector<long long> s1(1e6 + 2, 1LL * (1e12) * n), s2(1e6 + 2, 1LL * (1e12) * n), c1(n + 1, 1e12), c2(n + 1, 1e12); vector<pair<int, long long>> come, go; vector<pair<int, int>> p1, p2; for (int i = 0; i < m; i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (f == 0) { p2.push_back(make_pair(d, go.size())); go.push_back(make_pair(t, c)); } else { p1.push_back(make_pair(d, come.size())); come.push_back(make_pair(f, c)); } } sort(p1.begin(), p1.end()); sort(p2.begin(), p2.end()); int d = 1e6; for (int i = p2.size() - 1; i >= 0; i--) { auto cc = go[p2[i].second]; int dd = p2[i].first; while (d >= dd) s2[d] = s2[d + 1], d--; s2[dd] -= max(c2[cc.first] - cc.second, 0LL); c2[cc.first] = min(c2[cc.first], cc.second); } while (d > 0) s2[d] = s2[d + 1], d--; d++; k++; for (int i = 0; i < p1.size(); i++) { auto cc = come[p1[i].second]; int dd = p1[i].first; while (d <= dd) s1[d] = s1[d - 1], d++; s1[dd] -= max(0LL, c1[cc.first] - cc.second); c1[cc.first] = min(c1[cc.first], cc.second); } while (d <= 1e6) s1[d] = s1[d - 1], d++; long long ans = 1e18; for (int i = 0; i + k < 1e6 + 1; i++) { if (s1[i] <= 1LL * 1e6 * n && s2[i] <= 1LL * 1e6 * n) ans = min(ans, s1[i] + s2[i + k]); } cout << (ans > 1e11 ? -1 : ans) << endl; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int n, m, k; vector<pair<int, int> > in[N + 10], out[N + 10]; long long f1[N + 10], f2[N + 10]; int num1[N + 10], num2[N + 10]; int mi[N + 10], MI[N + 10]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (t == 0) in[d].push_back(make_pair(f, c)); else { out[d].push_back(make_pair(t, c)); } } for (int i = 1; i <= N; i++) { num1[i] = num1[i - 1]; f1[i] = f1[i - 1]; for (int j = 0; j <= (int)in[i].size() - 1; j++) { int from = in[i][j].first, cost = in[i][j].second; if (mi[from] == 0) { mi[from] = cost; num1[i]++; f1[i] += cost; } else { if (mi[from] > cost) { f1[i] -= (mi[from] - cost); mi[from] = cost; } } } } for (int i = N; i >= 1; i--) { f2[i] = f2[i + 1]; num2[i] = num2[i + 1]; for (int j = 0; j <= (int)out[i].size() - 1; j++) { int to = out[i][j].first; int cost = out[i][j].second; if (MI[to] == 0) { MI[to] = cost; f2[i] += cost; num2[i]++; } else { if (MI[to] > cost) { f2[i] -= (MI[to] - cost); MI[to] = cost; } } } } long long ans = -1; for (int i = 1; i + k + 1 <= N; i++) if (num1[i] == n) { if (num2[i + k + 1] == n) { long long temp = f1[i] + f2[i + k + 1]; if (ans == -1) { ans = temp; } else ans = min(ans, temp); } } cout << ans << endl; return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.util.*; import java.io.*; import java.math.BigInteger; public class ProblemA { static int mod = (int) (1e9+7); static InputReader in; static PrintWriter out; static void solve() { in = new InputReader(System.in); out = new PrintWriter(System.out); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int maxn = (int) (1e6 + 2); long ans = Long.MAX_VALUE; ArrayList<Pair>[] start = new ArrayList[maxn]; ArrayList<Pair>[] end = new ArrayList[maxn]; for(int i = 0; i < maxn; i++){ start[i] = new ArrayList<>(); end[i] = new ArrayList<>(); } for(int i = 0; i < m; i++){ int a = in.nextInt(); int x = in.nextInt(); int y = in.nextInt(); long c = in.nextInt(); if(x == 0 && y != 0){ end[a].add(new Pair(y,c)); } if(y == 0 && x != 0){ start[a].add(new Pair(x,c)); } } TreeMap<Long,Integer>[] map = new TreeMap[n + 1]; for(int i = 0; i <= n; i++) map[i] = new TreeMap<>(); long[] svalue = new long[n + 1]; long[] ecnt = new long[n + 1]; long curr = 0; int i = 0; HashSet<Integer> sset = new HashSet<>(); HashSet<Integer> eset = new HashSet<>(); int j = 1; for(Pair p : start[j]){ if(svalue[p.x] > p.c){ curr -= svalue[p.x]; curr += p.c; svalue[p.x] = p.c; } else if(svalue[p.x] == 0){ svalue[p.x] = p.c; curr += p.c; } sset.add(p.x); } for(i = k + 2; i < maxn; i++){ for(Pair p : end[i]){ if(map[p.x].isEmpty()){ curr += p.c; map[p.x].put(p.c, 1); } else{ long last = map[p.x].firstKey(); curr -= last; if(map[p.x].containsKey(p.c)) map[p.x].put(p.c, map[p.x].get(p.c) + 1); else map[p.x].put(p.c, 1); curr += map[p.x].firstKey(); } eset.add(p.x); } } if(sset.size() == n && eset.size() == n) ans = curr; j++; while(j + k < maxn){ for(Pair p : start[j]){ if(svalue[p.x] > p.c){ curr -= svalue[p.x]; curr += p.c; svalue[p.x] = p.c; } else if(svalue[p.x] == 0){ svalue[p.x] = p.c; curr += p.c; } sset.add(p.x); } i = j + k; for(Pair p : end[i]){ if(map[p.x].size() == 1){ curr -= p.c; map[p.x].clear(); eset.remove(p.x); } else{ long last = map[p.x].firstKey(); curr -= last; if(map[p.x].get(p.c) == 1) map[p.x].remove(p.c); else map[p.x].put(p.c, map[p.x].get(p.c) - 1); curr += map[p.x].firstKey(); } } if(sset.size() == n && eset.size() == n) ans = min(ans, curr); if(eset.size() != n) break; j++; } out.println(ans == Long.MAX_VALUE ? -1 : ans); out.close(); } public static void main(String[] args) { new Thread(null,new Runnable() { @Override public void run() { try{ solve(); } catch(Exception e){ e.printStackTrace(); } } },"1",1<<26).start(); } static class Pair implements Comparable<Pair> { int x,y; int i; long c; Pair (int i,int x,int y,long c) { this.x = x; this.y = y; this.i = i; this.c = c; } Pair (int x,long c) { this.x = x; this.c = c; } public int compareTo(Pair o) { return Integer.compare(this.i,o.i); //return 0; } public boolean equals(Object o) { if (o instanceof Pair) { Pair p = (Pair)o; return p.x == x && p.y==y; } return false; } @Override public String toString() { return x + " "+ y + " "+i; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class Merge { public static void sort(int inputArr[]) { int length = inputArr.length; doMergeSort(inputArr,0, length - 1); } private static void doMergeSort(int[] arr,int lowerIndex, int higherIndex) { if (lowerIndex < higherIndex) { int middle = lowerIndex + (higherIndex - lowerIndex) / 2; doMergeSort(arr,lowerIndex, middle); doMergeSort(arr,middle + 1, higherIndex); mergeParts(arr,lowerIndex, middle, higherIndex); } } private static void mergeParts(int[]array,int lowerIndex, int middle, int higherIndex) { int[] temp=new int[higherIndex-lowerIndex+1]; for (int i = lowerIndex; i <= higherIndex; i++) { temp[i-lowerIndex] = array[i]; } int i = lowerIndex; int j = middle + 1; int k = lowerIndex; while (i <= middle && j <= higherIndex) { if (temp[i-lowerIndex] < temp[j-lowerIndex]) { array[k] = temp[i-lowerIndex]; i++; } else { array[k] = temp[j-lowerIndex]; j++; } k++; } while (i <= middle) { array[k] = temp[i-lowerIndex]; k++; i++; } while(j<=higherIndex) { array[k]=temp[j-lowerIndex]; k++; j++; } } } static long add(long a,long b) { long x=(a+b); while(x>=mod) x-=mod; return x; } static long sub(long a,long b) { long x=(a-b); while(x<0) x+=mod; return x; } static long mul(long a,long b) { a%=mod; b%=mod; long x=(a*b); return x%mod; } static boolean isPal(String s) { for(int i=0, j=s.length()-1;i<=j;i++,j--) { if(s.charAt(i)!=s.charAt(j)) return false; } return true; } static String rev(String s) { StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } static long gcd(long x,long y) { if(y==0) return x; else return gcd(y,x%y); } static int gcd(int x,int y) { if(y==0) return x; else return gcd(y,x%y); } static long gcdExtended(long a,long b,long[] x) { if(a==0) { x[0]=0; x[1]=1; return b; } long[] y=new long[2]; long gcd=gcdExtended(b%a, a, y); x[0]=y[1]-(b/a)*y[0]; x[1]=y[0]; return gcd; } static long mulmod(long a,long b,long m) { if (m <= 1000000009) return a * b % m; long res = 0; while (a > 0) { if ((a&1)!=0) { res += b; if (res >= m) res -= m; } a >>= 1; b <<= 1; if (b >= m) b -= m; } return res; } static int abs(int a,int b) { return (int)Math.abs(a-b); } public static long abs(long a,long b) { return (long)Math.abs(a-b); } static int max(int a,int b) { if(a>b) return a; else return b; } static int min(int a,int b) { if(a>b) return b; else return a; } static long max(long a,long b) { if(a>b) return a; else return b; } static long min(long a,long b) { if(a>b) return b; else return a; } static long pow(long n,long p,long m) { long result = 1; if(p==0) return 1; while(p!=0) { if(p%2==1) result *= n; if(result>=m) result%=m; p >>=1; n*=n; if(n>=m) n%=m; } return result; } static long pow(long n,long p) { long result = 1; if(p==0) return 1; while(p!=0) { if(p%2==1) result *= n; p >>=1; n*=n; } return result; } static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; struct node { long long to, c; bool operator<(const node &a) const { return a.c < c; } node(long long _to, long long _c) : to(_to), c(_c) {} node() {} }; priority_queue<node> q[1000005]; vector<node> ft[1000005]; vector<node> fb[1000005]; set<long long> s; long long lans[1000005], rans[1000005]; void init() { for (int i = 0; i < 1000005; i++) { while (!q[i].empty()) { q[i].pop(); } } s.clear(); } int main() { long long n, m, k, d, f, t, c; while (~scanf("%lld %lld %lld", &n, &m, &k)) { init(); for (long long i = 0; i < 1000005; i++) { lans[i] = rans[i] = 1LL << 60; ft[i].clear(); fb[i].clear(); } for (long long i = 1; i <= m; i++) { scanf("%lld %lld %lld %lld", &d, &f, &t, &c); if (t == 0) { ft[d].push_back(node(f, c)); } else { fb[d].push_back(node(t, c)); } } long long sum = 0; for (long long i = 1; i < 1000005; i++) { for (long long j = 0; j < ft[i].size(); j++) { if (q[ft[i][j].to].empty()) { sum += ft[i][j].c; q[ft[i][j].to].push(ft[i][j]); } else { sum -= q[ft[i][j].to].top().c; q[ft[i][j].to].push(ft[i][j]); sum += q[ft[i][j].to].top().c; } s.insert(ft[i][j].to); if (s.size() == n) { lans[i] = min(lans[i], sum); } } if (s.size() == n) { lans[i] = min(lans[i], sum); } } sum = 0; init(); for (long long i = 1000005 - 1; i >= 0; i--) { for (long long j = 0; j < fb[i].size(); j++) { if (q[fb[i][j].to].empty()) { sum += fb[i][j].c; q[fb[i][j].to].push(fb[i][j]); } else { sum -= q[fb[i][j].to].top().c; q[fb[i][j].to].push(fb[i][j]); sum += q[fb[i][j].to].top().c; } s.insert(fb[i][j].to); if (s.size() == n) { rans[i] = min(rans[i], sum); } } if (s.size() == n) { rans[i] = min(rans[i], sum); } } long long ans = 1LL << 60; for (long long i = 1; i < 1000005; i++) { if (i + k < 1000005 && i - 1 > 0) { ans = min(ans, rans[i + k] + lans[i - 1]); } } if (ans == (1LL << 60)) { puts("-1"); } else { printf("%lld\n", ans); } } }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& out, vector<T>& arr) { for (int64_t i = 0; i < (int64_t)arr.size() - 1; ++i) { out << arr[i] << " "; } if (arr.size()) { out << arr.back() << '\n'; } return out; } template <typename T> istream& operator>>(istream& in, vector<T>& arr) { for (auto& i : arr) in >> i; return in; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { out << "{" << p.first << ", " << p.second << "}"; return out; } struct race { int64_t d, f, t, c; friend istream& operator>>(istream& in, race& a) { in >> a.d >> a.f >> a.t >> a.c; return in; } }; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int64_t n, m, k; cin >> n >> m >> k; vector<pair<pair<int64_t, int64_t>, pair<int64_t, int64_t> > > sob; vector<multiset<int64_t> > pri(n), ot(n); vector<int64_t> min_pri(n, 1000000000000), min_ot(n, 1000000000000); int64_t sum = n * 1000000000000; for (int64_t i = 0; i < m; ++i) { int64_t d, f, t, c; cin >> d >> f >> t >> c; if (t == 0) { sob.push_back(make_pair(make_pair(d, 1), make_pair(f - 1, c))); } else { sob.push_back(make_pair(make_pair(d - k, -1), make_pair(t - 1, c))); ot[t - 1].insert(c); min_ot[t - 1] = min(min_ot[t - 1], c); } } for (int64_t j = 0; j < n; ++j) { sum += min_ot[j]; if (min_ot[j] == 1000000000000) { cout << -1 << '\n'; return 0; } } for (int64_t i = 0; i < n; ++i) { ot[i].insert(1000000000000); pri[i].insert(1000000000000); } sort(sob.begin(), sob.end()); int64_t ans = 2000000000000000000; int64_t cnt = 0; for (int64_t i = 0; i < sob.size(); ++i) { int64_t t = sob[i].first.second; int64_t v = sob[i].second.first; int64_t c = sob[i].second.second; if (t == 1) { if (pri[v].size() == 1) { cnt++; } pri[v].insert(c); int64_t nw = *(pri[v].begin()); sum -= min_pri[v] - nw; min_pri[v] = nw; } else { ot[v].erase(ot[v].find(c)); if (ot[v].size() < 2) { break; } int64_t nw = *(ot[v].begin()); sum -= min_ot[v] - nw; min_ot[v] = nw; } if (cnt == n) ans = min(ans, sum); } if (ans <= 2 * 1000000000000) { cout << ans << '\n'; } else { cout << -1 << '\n'; } return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; const int MAXN = 110000; const long long INF = 1E12; multiset<int> s[MAXN]; struct Flight1 { int d, p, c; Flight1(int _d, int _p, int _c) : d(_d), p(_p), c(_c) {} bool operator<(const Flight1& rhs) const { return d < rhs.d; } }; struct Flight2 { int d, p; multiset<int>::iterator pos; Flight2(int _d, int _p, multiset<int>::iterator _pos) : d(_d), p(_p), pos(_pos) {} bool operator<(const Flight2& rhs) const { return d < rhs.d; } }; vector<Flight1> A; vector<Flight2> B; long long c[MAXN], t[MAXN], sum = 0, ans = INF; void update(int p) { long long tmp = t[p] + (s[p].empty() ? INF : *s[p].begin()); sum += tmp - c[p], c[p] = tmp; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (t == 0) A.push_back(Flight1(d, f, c)); else B.push_back(Flight2(d, t, s[t].insert(c))); } sort(A.begin(), A.end()); sort(B.begin(), B.end()); for (int i = 1; i <= n; i++) t[i] = INF, update(i); for (int i = 0, j = 0; i < A.size(); i++) { vector<int> tmp; t[A[i].p] = min(t[A[i].p], 1ll * A[i].c); while (j < B.size() && B[j].d <= A[i].d + k) s[B[j].p].erase(B[j].pos), tmp.push_back(B[j++].p); update(A[i].p); for (int x : tmp) update(x); ans = min(ans, sum); } printf("%lld\n", ans < INF ? ans : -1); return 0; }
CPP
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
import java.io.*; import java.util.*; public class Solution { static MyScanner sc; private static PrintWriter out; static long M = 1000000007; public static void main(String[] s) throws Exception { // sc = new MyScanner(new BufferedReader(new StringReader("2 6 5\n" + // "1 1 0 5000\n" + // "3 2 0 5500\n" + // "2 2 0 6000\n" + // "15 0 2 9000\n" + // "9 0 1 7000\n" + // "8 0 2 6500"))); sc = new MyScanner(System.in); out = new PrintWriter(new OutputStreamWriter(System.out)); long t = System.currentTimeMillis(); solve(); out.flush(); } private static void solve() { int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); int[][] data = new int[m][]; for (int i = 0; i < m; i++) { data[i] = sc.na(4); if (data[i][2] == 0) { data[i][0] += k + 1; } } Arrays.sort(data, Comparator.comparingInt(rt -> rt[0])); int [] st = new int[1000001]; long [] sum = new long[1000001]; boolean [] started = new boolean[n + 1]; int [] vals = new int[n + 1]; int step = 0; for (int i = 0 ; i < m && data[i][0] <= 1000000; i++) { int [] kk = data[i]; if (kk[1] == 0) continue; while(step < kk[0]) { step++; sum[step] = sum[step - 1]; st[step] = st[step - 1]; } if (!started[kk[1]] || kk[3] < vals[kk[1]]) { if (!started[kk[1]]) { st[step]++; } started[kk[1]] = true; sum[step] += kk[3] - vals[kk[1]]; vals[kk[1]] = kk[3]; } } while (step < 1000000) { step++; sum[step] = sum[step - 1]; st[step] = st[step - 1]; } int[] st2 = new int[1000001]; long[] sum2 = new long[1000001]; boolean[] started2 = new boolean[n + 1]; int[] vals2 = new int[n + 1]; step = 1000000; for (int i = m - 1; i>=0; i--) { int[] kk = data[i]; if (kk[2] == 0) continue; while (step > kk[0]) { step--; sum2[step] = sum2[step + 1]; st2[step] = st2[step + 1]; } if (!started2[kk[2]] || kk[3] < vals2[kk[2]]) { if (!started2[kk[2]]) { st2[step]++; } started2[kk[2]] = true; sum2[step] += kk[3] - vals2[kk[2]]; vals2[kk[2]] = kk[3]; } } while (step > 0) { step--; sum2[step] = sum2[step + 1]; st2[step] = st2[step + 1]; } long min = -1; for (int i = 1 ; i <= 1000000; i++) { if (st2[i] == n && st[i] == n) { if (min == -1 || min > sum[i] + sum2[i]) { min = sum[i] + sum2[i]; } } } System.out.println(min); } private static int[][] tree(int n) { int[][] x = new int[2][n - 1]; for (int i = 0; i < n - 1; i++) { x[0][i] = sc.nextInt(); x[1][i] = sc.nextInt(); } return pn(x, n); } private static int[][] pn(int[][] x, int n) { int[] ct = new int[n]; int[][] res = new int[n][]; for (int v : x[0]) { ct[v]++; } for (int v : x[1]) { ct[v]++; } for (int l = 0; l < n; l++) { res[l] = new int[ct[l]]; } for (int i = 0; i < x[0].length; i++) { res[x[0][i]][--ct[x[0][i]]] = x[1][i]; res[x[1][i]][--ct[x[1][i]]] = x[0][i]; } return res; } private static void solveT() { int t = sc.nextInt(); while (t-- > 0) { solve(); } } private static long gcd(long l, long l1) { if (l > l1) return gcd(l1, l); if (l == 0) return l1; return gcd(l1 % l, l); } private static long pow(long a, long b, long m) { if (b == 0) return 1; if (b == 1) return a; long pp = pow(a, b / 2, m); pp *= pp; pp %= m; return (pp * (b % 2 == 0 ? 1 : a)) % m; } static class MyScanner { BufferedReader br; StringTokenizer st; MyScanner(BufferedReader br) { this.br = br; } public MyScanner(InputStream in) { this(new BufferedReader(new InputStreamReader(in))); } void findToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } } String next() { findToken(); return st.nextToken(); } int[] na(int n) { int[] k = new int[n]; for (int i = 0; i < n; i++) { k[i] = sc.nextInt(); } return k; } long[] nl(int n) { long[] k = new long[n]; for (int i = 0; i < n; i++) { k[i] = sc.nextLong(); } return k; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } private static final class STree { long[] val1; int[] from1; int[] to1; long[] sum1; public STree(int c) { int size = Integer.highestOneBit(c - 1); size <<= 1; size = Math.max(size, 2); int rs = size << 1; val1 = new long[rs]; from1 = new int[rs]; sum1 = new long[rs]; to1 = new int[rs]; Arrays.fill(from1, Integer.MAX_VALUE); for (int r = rs - 1; r > 1; r--) { if (r >= size) { from1[r] = r - size; to1[r] = r - size; } from1[r / 2] = Math.min(from1[r / 2], from1[r]); to1[r / 2] = Math.max(to1[r / 2], to1[r]); } } public void add(int from, int to, long val) { add(1, from, to, val); } private void add(int cur, int from, int to, long val) { if (cur >= val1.length) return; if (from <= from1[cur] && to1[cur] <= to) { val1[cur] += val; val1[cur] %= M; addToParent(cur, ((long) to1[cur] - from1[cur] + 1) * val); return; } if (from1[cur] > to || from > to1[cur]) { return; } add((cur << 1) + 1, from, to, val); add(cur << 1, from, to, val); } private void addToParent(int cur, long l) { l %= M; while (cur > 1) { sum1[cur] %= M; sum1[cur >>= 1] += l; sum1[cur] %= M; } } public long sum(int from, int to) { return sum(1, from, to); } private long sum(int cur, int from, int to) { if (cur >= val1.length) return 0; if (from <= from1[cur] && to1[cur] <= to) { return (val1[cur] * (to1[cur] - from1[cur] + 1) + sum1[cur]) % M; } if (from1[cur] > to || from > to1[cur]) { return 0; } int c1 = Math.max(from, from1[cur]); int c2 = Math.min(to, to1[cur]); return ((c2 - c1 + 1) * val1[cur] + sum((cur << 1) + 1, from, to) + sum(cur << 1, from, to)) % M; } } }
JAVA
854_D. Jury Meeting
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process. There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems. You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day. Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days. Input The first line of input contains three integers n, m and k (1 ≤ n ≤ 105, 0 ≤ m ≤ 105, 1 ≤ k ≤ 106). The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 ≤ di ≤ 106, 0 ≤ fi ≤ n, 0 ≤ ti ≤ n, 1 ≤ ci ≤ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost. Output Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities. If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes). Examples Input 2 6 5 1 1 0 5000 3 2 0 5500 2 2 0 6000 15 0 2 9000 9 0 1 7000 8 0 2 6500 Output 24500 Input 2 4 5 1 2 0 5000 2 1 0 4500 2 1 0 3000 8 0 1 6000 Output -1 Note The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more. In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
2
10
#include <bits/stdc++.h> using namespace std; constexpr long long mod = 1000000007; const long long INF = mod * mod; const long double eps = 1e-12; const long double pi = acos(-1.0); long long mod_pow(long long a, long long n, long long m = mod) { a %= m; long long res = 1; while (n) { if (n & 1) res = res * a % m; a = a * a % m; n >>= 1; } return res; } struct modint { long long n; modint() : n(0) { ; } modint(long long m) : n(m) { if (n >= mod) n %= mod; else if (n < 0) n = (n % mod + mod) % mod; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } modint operator+=(modint &a, modint b) { a.n += b.n; if (a.n >= mod) a.n -= mod; return a; } modint operator-=(modint &a, modint b) { a.n -= b.n; if (a.n < 0) a.n += mod; return a; } modint operator*=(modint &a, modint b) { a.n = ((long long)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, int n) { if (n == 0) return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } long long inv(long long a, long long p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } const int max_n = 1 << 18; modint fact[max_n], factinv[max_n]; void init_f() { fact[0] = modint(1); for (int i = 0; i < max_n - 1; i++) { fact[i + 1] = fact[i] * modint(i + 1); } factinv[max_n - 1] = modint(1) / fact[max_n - 1]; for (int i = max_n - 2; i >= 0; i--) { factinv[i] = factinv[i + 1] * modint(i + 1); } } modint comb(int a, int b) { if (a < 0 || b < 0 || a < b) return 0; return fact[a] * factinv[b] * factinv[a - b]; } int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; vector<pair<int, int>> ad[1000001]; vector<pair<int, int>> era[1000001]; void solve() { int n, m, k; cin >> n >> m >> k; long long ans = INF; int pretmp = 0; long long presum = 0; vector<long long> coms(n, INF); vector<priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>>> lefs(n); for (int i = 0; i < m; i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (t == 0) { ad[d].push_back({f - 1, c}); } else { era[d].push_back({t - 1, c}); lefs[t - 1].push({c, d}); } } long long bsum = 0; int tmp = 0; for (int i = 0; i < n; i++) { while (lefs[i].size() && lefs[i].top().second < k) { lefs[i].pop(); } if (lefs[i].size()) { tmp++; bsum += lefs[i].top().first; } } for (int le = 0; le <= 1000000; le++) { if (pretmp == n && tmp == n) { ans = min(ans, presum + bsum); } for (pair<int, int> p : ad[le]) { int id = p.first; if (coms[id] == INF) { presum += p.second; coms[id] = p.second; pretmp++; } else { presum -= coms[id]; coms[id] = min(coms[id], (long long)p.second); presum += coms[id]; } } if (le + k <= 1000000) { for (pair<int, int> p : era[le + k]) { int id = p.first; if (lefs[id].empty()) continue; bsum -= lefs[id].top().first; while (lefs[id].size() && lefs[id].top().second <= le + k) { lefs[id].pop(); } if (lefs[id].empty()) { tmp--; } else { bsum += lefs[id].top().first; } } } } if (ans == INF) ans = -1; cout << ans << "\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0); solve(); char nyaa; cin >> nyaa; return 0; }
CPP