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; int quick_pow(int a, int b, int MOD) { a %= MOD; int res = 1; while (b) { if (b & 1) res = (res * a) % MOD; b /= 2; a = (a * a) % MOD; } return res; } const int maxn = 200000 + 10; const int MOD = 1e9 + 7; struct node { int f, t, c; } s[maxn]; int cmp(const node &x, const node &y) {} vector<node> vec[1000005]; long long in[1000005], out[1000005]; int vis[maxn]; int main() { int i, j, n, m, k; scanf("%d%d%d", &n, &m, &k); int mx = 0; for (i = 1; i <= m; i++) { int d; scanf("%d%d%d%d", &d, &s[i].f, &s[i].t, &s[i].c); vec[d].push_back((node){s[i].f, s[i].t, s[i].c}); mx = max(mx, d); } for (i = 1; i <= n; i++) vis[i] = MOD; for (i = 0; i <= mx + 1; i++) in[i] = out[i] = 100000000000000000LL; int cnt = 0, l = -1, r = -1; long long sum = 0; for (i = 1; i <= mx; i++) { in[i] = in[i - 1]; if (vec[i].size() == 0) { in[i] = in[i - 1]; continue; } for (j = 0; j < vec[i].size(); j++) { if (vec[i][j].t) continue; if (vis[vec[i][j].f] > vec[i][j].c) { if (vis[vec[i][j].f] != MOD) { sum -= vis[vec[i][j].f] - vec[i][j].c; } else { cnt++; sum += vec[i][j].c; } vis[vec[i][j].f] = vec[i][j].c; } if (cnt == n) { if (l == -1) l = i; in[i] = sum; } } } cnt = 0; sum = 0; for (i = 1; i <= n; i++) vis[i] = MOD; for (i = mx; i >= 1; i--) { out[i] = out[i + 1]; if (vec[i].size() == 0) { out[i] = out[i + 1]; continue; } for (j = 0; j < vec[i].size(); j++) { if (vec[i][j].f) continue; if (vis[vec[i][j].t] > vec[i][j].c) { if (vis[vec[i][j].t] != MOD) { sum -= vis[vec[i][j].t] - vec[i][j].c; } else { cnt++; sum += vec[i][j].c; } vis[vec[i][j].t] = vec[i][j].c; } if (cnt == n) { if (r == -1) r = i; out[i] = sum; } } } if (r < 0 || l < 0 || r - l <= k) { puts("-1"); return 0; } long long res = 100000000000000LL; for (i = l; i + k + 1 <= r; i++) { res = min(res, in[i] + out[i + k + 1]); } printf("%I64d\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 w[2050000], a[2050000], w1[2050000], b[2050000]; long long n, m, k; struct node { long long t; long long x; long long y; long long money; } f[2050000]; bool cmp(node x, node y) { if (x.y == 0 && y.y != 0) { return true; } else if (x.y != 0 && y.y == 0) { return false; } else return x.t < y.t; } int main() { long long maxx = 0; scanf("%I64d%I64d%I64d", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%I64d%I64d%I64d%I64d", &f[i].t, &f[i].x, &f[i].y, &f[i].money); } for (long long i = 0; i < 2050000; i++) { w[i] = 100010000000; w1[i] = 100010000000; } maxx = (long long)1 * 100010000000 * n; int len1 = 0; sort(f, f + m, cmp); int i = 0; for (i; i < m; i++) { if (f[i].y != 0) { break; } if (f[i].money <= w[f[i].x]) { long long k = w[f[i].x] - f[i].money; maxx -= k; w[f[i].x] = f[i].money; a[f[i].t] = maxx; len1 = f[i].t; } } maxx = (long long)1 * 100010000000 * n; int sum = 0; int len2 = f[m - 1].t; for (int j = m - 1; j >= i; j--) { if (f[j].money <= w1[f[j].y]) { long long k = w1[f[j].y] - f[j].money; maxx -= k; w1[f[j].y] = f[j].money; b[f[j].t] = maxx; } } a[0] = -1; for (i = 1; i < 2050000; i++) { if (!a[i]) { a[i] = a[i - 1]; } if (a[i] >= 100010000000) { a[i] = -1; } } for (int i = len2; i >= 0; i--) { } for (int i = 0; i < 2050000; i++) { if (a[i - 1] != -1) { a[i] = min(a[i - 1], a[i]); } } b[0] = -1; for (i = len2 - 1; i >= 0; i--) { if (b[i]) { b[i] = min(b[i + 1], b[i]); } else { b[i] = b[i + 1]; } } long long ans = n * 100010000000; for (int i = 1; i + k + 1 <= len2; i++) { if (a[i] != -1 && b[i + k + 1] != -1) { ans = min(ans, a[i] + b[i + k + 1]); } } if (ans >= 100010000000 || ans < 0) { printf("-1"); } else 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; public class BirthdayOdds { static long MAX_VAL = (long)(1e13); public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] line = in.readLine().split("\\s+"); int n = Integer.parseInt(line[0]); int flights = Integer.parseInt(line[1]); int k = Integer.parseInt(line[2]); List<F> go = new ArrayList<>(flights); List<List<F>> back = new ArrayList<>(1000000); for(int i=0; i<1000001; i++) { back.add(new ArrayList<>()); } for(int i=0; i<flights; i++) { line = in.readLine().split("\\s+"); int day = Integer.parseInt(line[0]); int depart = Integer.parseInt(line[1]); int arrival = Integer.parseInt(line[2]); int cost = Integer.parseInt(line[3]); F cur = new F(day, depart, arrival, cost); if(arrival == 0) { go.add(cur); } else { back.get(day).add(cur); } } long[] dp = new long[1000002]; Arrays.fill(dp, MAX_VAL); int[] minCost = new int[n+1]; long curSum = 0; int sz = 0; for(int i=1000000; i>=0; i--) { // always can finish in atleast min cost of i+1 days dp[i] = dp[i+1]; for(F cur : back.get(i)) { if(minCost[cur.arrive] == 0) { minCost[cur.arrive] = cur.cost; curSum += cur.cost; sz++; } else if(minCost[cur.arrive] > cur.cost) { curSum -= minCost[cur.arrive] - cur.cost; minCost[cur.arrive] = cur.cost; } // else cur val is less } if(sz == n) { dp[i] = Math.min(dp[i], curSum); } } Collections.sort(go, new Comparator<F>() { @Override public int compare(F o1, F o2) { return Integer.compare(o1.day, o2.day); } }); Arrays.fill(minCost, 0); curSum = 0; long best = MAX_VAL; sz = 0; for(int i=0; i<go.size(); i++) { F cur = go.get(i); if(cur.day + k + 1 > 1000000) { break; } if(minCost[cur.depart] == 0) { minCost[cur.depart] = cur.cost; curSum += cur.cost; sz++; } else if(minCost[cur.depart] > cur.cost) { curSum -= minCost[cur.depart] - cur.cost; minCost[cur.depart] = cur.cost; } // else cur val is less if(sz == n) { best = Math.min(best, curSum + dp[cur.day + k + 1]); } } System.out.println(best == MAX_VAL ? -1 : best); } } class F { int day; int depart; int arrive; int cost; F(int dd, int ll, int cc, int k) { day = dd; depart = ll; arrive = cc; cost = k; } }
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; inline int min(int _a, int _b) { return _a < _b ? _a : _b; } inline int max(int _a, int _b) { return _a > _b ? _a : _b; } template <class T> inline void rd(T &_a) { int _f = 0, _ch = getchar(); _a = 0; while (_ch < '0' || _ch > '9') { if (_ch == '-') _f = 1; _ch = getchar(); } while (_ch >= '0' && _ch <= '9') { _a = (_a << 1) + (_a << 3) + _ch - '0'; _ch = getchar(); } if (_f) _a = -_a; } const int inf = 0x3f3f3f3f; const double eps = 1e-8; const int N = 1e5 + 5; struct node { int t, u, w; bool operator<(const node &a) const { return t < a.t; } } a[N], b[N]; struct tree { int l, r; long long mn; } t[N << 2]; int v[N], ac, lt = -1, bc; long long lc[N]; void Pu(int rt) { t[rt].mn = min(t[rt << 1].mn, t[rt << 1 | 1].mn); } void Bd(int l, int r, int rt = 1) { t[rt].l = l; t[rt].r = r; if (l == r) { t[rt].mn = lc[l]; return; } int m = l + r >> 1; Bd(l, m, rt << 1); Bd(m + 1, r, rt << 1 | 1); Pu(rt); } long long Qy(int l, int r, int rt = 1) { if (t[rt].l == l && t[rt].r == r) return t[rt].mn; int m = t[rt].l + t[rt].r >> 1; if (r <= m) return Qy(l, r, rt << 1); if (l > m) return Qy(l, r, rt << 1 | 1); return min(Qy(l, m, rt << 1), Qy(m + 1, r, rt << 1 | 1)); } int Bs(int x) { int l = lt, r = ac - 1, res; while (l <= r) { int m = l + r >> 1; if (a[m].t < x) res = m, l = m + 1; else r = m - 1; } return res; } int main() { int n, m, k; long long ans = -1; rd(n); rd(m); rd(k); for (int i = 0, t, u, v, w; i < m; i++) { rd(t); rd(u); rd(v); rd(w); if (v == 0) a[ac++] = (node){t, u, w}; else b[bc++] = (node){t, v, w}; } sort(a, a + ac); sort(b, b + bc); for (int i = 0, cnt = 0; i < ac; i++) { lc[i] = (i ? lc[i - 1] : 0); if (!v[a[i].u]) lc[i] += (v[a[i].u] = a[i].w), cnt++; else if (a[i].w < v[a[i].u]) lc[i] -= v[a[i].u] - a[i].w, v[a[i].u] = a[i].w; if (cnt == n && lt == -1) lt = i; } if (lt == -1) return puts("-1"), 0; Bd(lt, ac - 1); memset(v, 0, sizeof v); for (int i = bc - 1, cnt = 0; i >= 0 && b[i].t > a[lt].t + k; i--) { lc[i] = (i == bc - 1 ? 0 : lc[i + 1]); if (!v[b[i].u]) lc[i] += (v[b[i].u] = b[i].w), cnt++; else if (b[i].w < v[b[i].u]) lc[i] -= v[b[i].u] - b[i].w, v[b[i].u] = b[i].w; if (cnt == n) if (ans == -1) ans = lc[i] + Qy(lt, Bs(b[i].t - k)); else ans = min(ans, lc[i] + Qy(lt, Bs(b[i].t - k))); } printf("%I64d", 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.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collections; import java.util.StringTokenizer; public class Div1_433B { public static void main(String[] args) throws IOException { new Div1_433B().execute(); } void execute() throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer inputData = new StringTokenizer(reader.readLine()); int nJ = Integer.parseInt(inputData.nextToken()); int nF = Integer.parseInt(inputData.nextToken()); int lR = Integer.parseInt(inputData.nextToken()); ArrayList<Flight>[] aFlight = new ArrayList[nJ + 1]; ArrayList<Flight>[] dFlight = new ArrayList[nJ + 1]; for (int i = 1; i <= nJ; i++) { aFlight[i] = new ArrayList<>(); dFlight[i] = new ArrayList<>(); } for (int i = 0; i < nF; i++) { inputData = new StringTokenizer(reader.readLine()); int cT = Integer.parseInt(inputData.nextToken()); int dep = Integer.parseInt(inputData.nextToken()); int arr = Integer.parseInt(inputData.nextToken()); int cC = Integer.parseInt(inputData.nextToken()); if (arr == 0) { aFlight[dep].add(new Flight(cT + 1, cC, 0, dep)); } else { dFlight[arr].add(new Flight(cT - lR + 1, cC, 1, arr)); } } ArrayDeque<Flight>[] aQueue = new ArrayDeque[nJ + 1]; ArrayDeque<Flight>[] dQueue = new ArrayDeque[nJ + 1]; ArrayList<Flight> sFlights = new ArrayList<>(); for (int i = 1; i <= nJ; i++) { ArrayList<Flight> cList = aFlight[i]; Collections.sort(cList); ArrayDeque<Flight> nList = new ArrayDeque<>(); for (int j = 0; j < cList.size(); j++) { if (nList.isEmpty() || nList.peekLast().cost > cList.get(j).cost) { nList.addLast(cList.get(j)); } } aQueue[i] = nList; sFlights.addAll(nList); cList = dFlight[i]; Collections.sort(cList); nList = new ArrayDeque<>(); for (int j = cList.size() - 1; j >= 0; j--) { if (nList.isEmpty() || cList.get(j).cost < nList.peekFirst().cost) { nList.addFirst(cList.get(j)); } } dQueue[i] = nList; sFlights.addAll(nList); } Collections.sort(sFlights); ArrayDeque<Flight> sQueue = new ArrayDeque<>(sFlights); int sTime = 0; for (int i = 1; i <= nJ; i++) { if (aQueue[i].isEmpty()) { printer.println(-1); printer.close(); return; } sTime = Math.max(sTime, aQueue[i].peekFirst().time); } int[] lastA = new int[nJ + 1]; long cCost = 0; for (int i = 1; i <= nJ; i++) { Flight first = aQueue[i].peekFirst(); if (first == null) { printer.println(-1); printer.close(); return; } while (!aQueue[i].isEmpty() && aQueue[i].peekFirst().time <= sTime) { first = aQueue[i].removeFirst(); } aQueue[i].addFirst(first); lastA[i] = first.cost; cCost += first.cost; while (!dQueue[i].isEmpty() && dQueue[i].peekFirst().time <= sTime) { dQueue[i].removeFirst(); } if (dQueue[i].isEmpty()) { printer.println(-1); printer.close(); return; } cCost += dQueue[i].peekFirst().cost; } long mAns = cCost; while (sQueue.peekFirst().time <= sTime) { sQueue.removeFirst(); } while (!sFlights.isEmpty()) { long cTime = sQueue.peekFirst().time; while (sQueue.peekFirst().time == cTime) { Flight nxt = sQueue.removeFirst(); if (nxt.type == 0) { cCost -= lastA[nxt.city]; cCost += nxt.cost; lastA[nxt.city] = nxt.cost; } else { cCost -= nxt.cost; dQueue[nxt.city].removeFirst(); if (dQueue[nxt.city].isEmpty()) { printer.println(mAns); printer.close(); return; } cCost += dQueue[nxt.city].peekFirst().cost; } } mAns = Math.min(mAns, cCost); } printer.println(mAns); printer.close(); } class Flight implements Comparable<Flight> { int time; int cost; int type; int city; Flight(int time, int cost, int type, int city) { this.time = time; this.cost = cost; this.type = type; this.city = city; } public int compareTo(Flight o) { return Integer.compare(time, o.time); } } }
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.math.*; import java.io.*; import java.util.*; public class Main{ static class Node implements Comparable<Node>{ int a; int b; int day; int cost; public Node(int a, int b, int day, int cost){ this.a = a; this.b = b; this.day = day; this.cost = cost; } public int compareTo(Node that){ return this.day - that.day; } } static int maxDay = 1_000_005; public static void main(String[] args ) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] in = br.readLine().split(" "); int n = Integer.parseInt(in[0]); n++; int m = Integer.parseInt(in[1]); int k = Integer.parseInt(in[2]); Node[] arr = new Node[m]; for( int i = 0; i < m; i++){ in = br.readLine().split(" "); int a = Integer.parseInt(in[1]); int b = Integer.parseInt(in[2]); int day = Integer.parseInt(in[0]); int cost = Integer.parseInt(in[3]); arr[i] = new Node( a, b , day , cost); } Arrays.sort(arr); boolean[] used = new boolean[n]; int[] C = new int[n]; int count = 1; long[] minCostLeft = new long[maxDay]; for( int i = 0; i < maxDay; i++){ minCostLeft[i] = Long.MAX_VALUE/2; } long tot = 0; for( int i = 0; i < m ; i++){ Node node = arr[i]; int day = node.day; int from = node.a; int to = node.b; int cost = node.cost; if(from == 0)continue; if(!used[from]){ used[from] = true; tot += cost; C[from] = cost; count++; } else if(cost < C[from]){ tot -= C[from]; tot += cost; C[from] = cost; } if(count == n){ minCostLeft[day] = tot; } } for( int i = 1; i < maxDay; i++){ minCostLeft[i] = Math.min( minCostLeft[i] , minCostLeft[i-1]); //System.out.print("" + minCostLeft[i] + " "); } //System.out.println(); used = new boolean[n]; C = new int[n]; count = 1; long[] minCostRight = new long[maxDay]; for( int i = 0; i < maxDay; i++){ minCostRight[i] = Long.MAX_VALUE/2; } tot = 0; for( int i = m -1; i != -1; i--){ Node node = arr[i]; int day = node.day; int from = node.a; int to = node.b; int cost = node.cost; if(to == 0)continue; if(!used[to]){ used[to] = true; C[to] = cost; tot += cost; count++; } else if( cost < C[to]){ tot -= C[to]; tot += cost; C[to] = cost; } if( count == n){ minCostRight[day] = tot; } } for( int i = maxDay -2; i != 0 ; i--){ minCostRight[i] = Math.min(minCostRight[i], minCostRight[i+1]); //System.out.print(" "+ minCostRight[i] + " " ); } //System.out.println(); long min = Long.MAX_VALUE-1; for( int i = 0; i < maxDay -3-k; i++){ min = Math.min(minCostLeft[i] + minCostRight[i+k+1], min); //System.out.println("" + minCostLeft[i] + " " + minCostRight[i + k + 2]); } if(min >= Long.MAX_VALUE/2 ) min = -1; System.out.println(min); } }
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 = 1e12; const int maxn = 1e6 + 5; int n, m, k; vector<pair<int, int> > st[maxn], fn[maxn]; long long b[maxn], e[maxn]; int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (f) { st[d + 1].push_back({f, c}); } else { fn[d - 1].push_back({t, c}); } } map<int, int> mp; long long total = 0; for (int i = 0; i < maxn; i++) { b[i] = INF; for (int j = 0; j < st[i].size(); j++) { int city = st[i][j].first; int cost = st[i][j].second; if (!mp[city]) { mp[city] = cost; total += cost; } else if (mp[city] > cost) { total -= (mp[city] - cost); mp[city] = cost; } } if (mp.size() == n) { b[i] = total; } } mp.clear(); total = 0; for (int i = maxn - 1; i >= 0; i--) { e[i] = INF; for (int j = 0; j < fn[i].size(); j++) { int city = fn[i][j].first; int cost = fn[i][j].second; if (!mp[city]) { total += cost; mp[city] = cost; } else if (mp[city] > cost) { total -= mp[city] - cost; mp[city] = cost; } } if (mp.size() == n) { e[i] = total; } } long long ans = INF; for (int i = k; i < maxn; i++) { ans = min(ans, e[i] + b[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 Maxn = 1e6 + 10; struct node { long long num, t, c; }; bool cmp1(node a, node b) { return a.t < b.t; } bool cmp2(node a, node b) { return a.t > b.t; } long long l[Maxn] = {}, r[Maxn] = {}, f[Maxn] = {}, vis1[Maxn] = {}, vis2[Maxn] = {}; vector<node> a, b; int main() { long long n, m, k, ft = -1, lt = -1; cin >> n >> m >> k; for (int i = 1; i <= m; i++) { int x, y, z, w; node N; scanf("%d%d%d%d", &x, &y, &z, &w); N.c = w; N.t = x; if (z == 0) { N.num = y; a.push_back(N); } else { N.num = z; b.push_back(N); } } sort(a.begin(), a.end(), cmp1); sort(b.begin(), b.end(), cmp2); long long sum = 0, cnt = 0, nt = -1; for (int i = 0; i < a.size(); i++) { int x = a[i].num; while (nt != -1 && nt < a[i].t) { nt++; l[nt] = sum; } if (!vis1[x]) { vis1[x] = a[i].c; cnt++; sum += a[i].c; } else { if (vis1[x] > a[i].c) { sum += a[i].c - vis1[x]; vis1[x] = a[i].c; l[nt] = sum; } } if (cnt == n && ft == -1) { ft = a[i].t; nt = ft; l[ft] = sum; } } if (nt != -1) for (int i = nt + 1; i <= 1e6; i++) l[i] = l[i - 1]; sum = 0, cnt = 0, nt = -1; for (int i = 0; i < b.size(); i++) { int x = b[i].num; while (nt != -1 && nt > b[i].t) { nt--; r[nt] = sum; } if (!vis2[x]) { vis2[x] = b[i].c; cnt++; sum += b[i].c; } else { if (vis2[x] > b[i].c) { sum += b[i].c - vis2[x]; vis2[x] = b[i].c; r[nt] = sum; } } if (cnt == n && lt == -1) { nt = b[i].t; lt = nt; r[lt] = sum; } } if (nt != -1) for (int i = nt - 1; i >= 0; i--) r[i] = r[i + 1]; long long ans = 1e15; if (ft != -1 && lt != -1) for (int i = ft; i + k + 1 <= lt; i++) { ans = min(ans, l[i] + r[i + k + 1]); } if (ans == 1e15) 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 long long inf = (long long)1 << 61; struct data { int d, v, c; } a[110001], b[110001]; struct stcmp1 { bool operator()(int x, int y) { return a[x].c < a[y].c || a[x].c == a[y].c && x < y; } }; struct stcmp2 { bool operator()(int x, int y) { return b[x].c < b[y].c || b[x].c == b[y].c && x < y; } }; set<int, stcmp1> sa[110001]; set<int, stcmp2> sb[110001]; long long ans, now; int i, j, k, n, m, w, t1, t2, na, nb, v[110001], pa, pb, tmp; bool cmp(data x, data y) { return x.d < y.d; } int main() { scanf("%d%d%d", &n, &m, &w); na = nb = 0; for (i = 1; i <= m; i++) { scanf("%d%d%d%d", &j, &t1, &t2, &k); if (!t2) a[++na].d = j, a[na].v = t1, a[na].c = k; else b[++nb].d = j, b[nb].v = t2, b[nb].c = k; } if (na < n || nb < n) { puts("-1"); return 0; } sort(a + 1, a + 1 + na, cmp); sort(b + 1, b + 1 + nb, cmp); memset(v, 0, sizeof(v)); for (k = 0, i = 1; i <= na; i++) { if (!v[a[i].v]) v[a[i].v] = 1, k++; if (k == n) { t1 = a[i].d + 1; break; } } if (k < n) { puts("-1"); return 0; } memset(v, 0, sizeof(v)); for (k = 0, i = nb; i; i--) { if (!v[b[i].v]) v[b[i].v] = 1, k++; if (k == n) { t2 = b[i].d - w; break; } } if (k < n || t2 < t1) { puts("-1"); return 0; } for (i = 1; i <= n; i++) sa[i].clear(), sb[i].clear(); for (pa = 1; pa <= na && a[pa].d < t1; pa++) sa[a[pa].v].insert(pa); for (pb = nb; pb && b[pb].d >= t1 + w; pb--) sb[b[pb].v].insert(pb); for (now = 0, i = 1; i <= n; i++) now += a[*sa[i].begin()].c + b[*sb[i].begin()].c; for (ans = now, pb++, i = t1; i <= t2; i++) { for (; pa <= na && a[pa].d < i; pa++) { j = a[pa].v; k = a[*sa[j].begin()].c; if (a[pa].c < k) now -= k - a[pa].c; sa[j].insert(pa); } for (; pb <= nb && b[pb].d < i + w; pb++) { j = b[pb].v; k = b[*sb[j].begin()].c; sb[j].erase(pb); tmp = b[*sb[j].begin()].c; now += tmp - k; } ans = min(ans, now); } 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 long long INF = 1ll * 1e18; const int N = 1e5 + 5; struct node { int d, u, v, c; bool operator<(const node& t) const { return d < t.d; } } e[N]; int n, m, k, maxn, dis[N], d[N]; long long a[N * 20], b[N * 20]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } void solve() { long long now = INF; int crt = n; for (int i = 1; i <= m; i++) { int T = e[i].d; if (e[i].u) { if (!dis[e[i].u]) { dis[e[i].u] = e[i].c; if (!(--crt)) { now = 0; for (int i = 1; i <= n; i++) now += dis[i]; a[T] = now; } } else if (dis[e[i].u] > e[i].c) { if (!crt) { now -= dis[e[i].u]; dis[e[i].u] = e[i].c; now += e[i].c; a[T] = now; } else dis[e[i].u] = e[i].c; } } } now = INF, crt = n; for (int i = m; i >= 1; i--) { int p = e[i].d; if (e[i].v != 0) { if (d[e[i].v] == 0) { d[e[i].v] = e[i].c; if ((--crt) == 0) { now = 0; for (int j = 1; j <= n; j++) now += d[j]; b[p] = now; } } else if (e[i].c < d[e[i].v]) { if (crt == 0) { now -= d[e[i].v]; d[e[i].v] = e[i].c; now += e[i].c; b[p] = now; } else d[e[i].v] = e[i].c; ; } } } long long ans = INF; for (int i = maxn; i >= 1; i--) if (!b[i]) b[i] = b[i + 1]; else if (b[i + 1]) b[i] = min(b[i], b[i + 1]); for (int i = 1; i <= maxn; i++) if (!a[i]) a[i] = a[i - 1]; else if (a[i - 1]) a[i] = min(a[i], a[i - 1]); for (int i = 1; i <= maxn - k - 1; i++) if (a[i] && b[i + k + 1]) ans = min(ans, a[i] + b[i + k + 1]); if (ans != INF) printf("%lld\n", ans); else printf("-1\n"); } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { e[i].d = read(), e[i].u = read(), e[i].v = read(), e[i].c = read(); maxn = max(maxn, e[i].d); } sort(e + 1, e + m + 1); 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; const long long LLINF = 8e18; const int MOD = 1e9 + 7; const int INF = 2e9; const int N = 1e5; struct data { int day, from, to, cost; } a[N]; int n, m, k, cnt, j, was, will; int mn[N]; long long tmp, ans = LLINF; set<pair<int, int>> s[N]; bool cmp(data a, data b) { return (a.day < b.day); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> a[i].day >> a[i].from >> a[i].to >> a[i].cost; if (a[i].from == 0) s[a[i].to - 1].insert(make_pair(a[i].cost, a[i].day)); } for (int i = 0; i < n; i++) { if (s[i].size() == 0) { cout << -1; return 0; } tmp += (*s[i].begin()).first; } sort(a, a + m, cmp); for (int i = 0; i < m; i++) { if (a[i].to == 0) { was = mn[a[i].from - 1]; if (was == 0) { cnt++; mn[a[i].from - 1] = a[i].cost; tmp += a[i].cost; } else { will = mn[a[i].from - 1] = min(mn[a[i].from - 1], a[i].cost); tmp -= was - will; } } while (a[j].day - a[i].day < k + 1) { if (a[j].from == 0) { was = (*s[a[j].to - 1].begin()).first; s[a[j].to - 1].erase(make_pair(a[j].cost, a[j].day)); if (s[a[j].to - 1].size() == 0) { if (ans == LLINF) cout << -1; else cout << ans; return 0; } else { will = (*s[a[j].to - 1].begin()).first; tmp -= was - will; } } j++; } if (cnt == n) ans = min(ans, tmp); } 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 <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; } inline int read() { register int c = getchar(), sum(0), fg(1); while (c < '0' || c > '9') { if (c == '-') fg = -1; c = getchar(); } while (c >= '0' && c <= '9') sum = sum * 10 + c - '0', c = getchar(); return sum * fg; } const int oo = 0x3f3f3f3f; const int maxn = 100000; struct flight { long long d, first, w; inline bool operator<(const flight &adj) const { return d < adj.d; } } a[maxn + 5], b[maxn + 5]; int L1 = 0, L2 = 0; int N, M, k; long long prefix[maxn + 5], suffix[maxn + 5], Min[maxn + 5]; int main() { N = read(), M = read(), k = read(); for (int i = (1), i_end_ = (M); i <= i_end_; ++i) { int d, first, second, w; d = read(), first = read(), second = read(), w = read(); if (second == 0) a[++L1].d = d, a[L1].first = first, a[L1].w = w; if (first == 0) b[++L2].d = d, b[L2].first = second, b[L2].w = w; } sort(a + 1, a + L1 + 1); sort(b + 1, b + L2 + 1); long long sum = 0, cnt = 0; memset(Min, 0, sizeof Min); for (int i = (1), i_end_ = (L1); i <= i_end_; ++i) { sum -= Min[a[i].first]; if (!Min[a[i].first]) ++cnt, Min[a[i].first] = a[i].w; else chkmin(Min[a[i].first], a[i].w); sum += Min[a[i].first]; prefix[i] = cnt == N ? sum : LLONG_MAX; } sum = cnt = 0, memset(Min, 0, sizeof Min); for (int i = L2; i; --i) { sum -= Min[b[i].first]; if (!Min[b[i].first]) ++cnt, Min[b[i].first] = b[i].w; else chkmin(Min[b[i].first], b[i].w); sum += Min[b[i].first]; suffix[i] = cnt == N ? sum : LLONG_MAX; } int r = 0; long long ans = LLONG_MAX; for (int l = (1), l_end_ = (L1); l <= l_end_; ++l) { while (r <= L2 && a[l].d + k >= b[r].d) ++r; if (r > L2) break; if (prefix[l] < LLONG_MAX && suffix[r] < LLONG_MAX) chkmin(ans, prefix[l] + suffix[r]); } printf("%lld\n", ans >= LLONG_MAX ? -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; struct info { int d, f, t, cst; } inp[100006]; int forw[1000005]; int forw2[1000005]; long long cforw[1000005]; long long cforw2[1000005]; int ok[1000006]; int ok2[1000006]; bool cmp(info a, info b) { return a.d < b.d; } void process1(int prev, int day) { while (prev < day) { cforw[prev + 1] = cforw[prev]; forw[prev + 1] = forw[prev]; prev++; } } void process2(int prev, int day) { while (prev > day) { cforw2[prev - 1] = cforw2[prev]; forw2[prev - 1] = forw2[prev]; prev--; } } 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", &inp[i].d, &inp[i].f, &inp[i].t, &inp[i].cst); } sort(inp + 1, inp + m + 1, cmp); int prev = 0; for (int i = 1; i <= m; i++) { int day = inp[i].d; process1(prev, day); prev = day; if (inp[i].f != 0) { int pos = inp[i].f; if (ok[pos] == 0) { ok[pos] = inp[i].cst; cforw[day] += inp[i].cst; forw[day]++; } else if (ok[pos] > inp[i].cst) { cforw[day] += (-ok[pos] + inp[i].cst); ok[pos] = inp[i].cst; } } } process1(prev, 1000000); prev = n + 1; for (int i = m; i >= 1; i--) { if (inp[i].f != 0) continue; int pos = inp[i].t; int day = inp[i].d; process2(prev, day); prev = day; if (ok2[pos] == 0) { ok2[pos] = inp[i].cst; cforw2[day] += inp[i].cst; forw2[day]++; } else if (ok2[pos] > inp[i].cst) { cforw2[day] += (-ok2[pos] + inp[i].cst); ok2[pos] = inp[i].cst; } } process2(prev, 1); long long ans = -1; for (int i = 1; i <= 1000000 - k - 1; i++) { int t = i + k + 1; if (forw[i] != n || (forw2[t] != n)) continue; long long tot = cforw[i] + cforw2[t]; if (ans == -1) ans = tot; else ans = min(ans, tot); } 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; template <class T> inline void gmin(T &x, const T &y) { if (x > y) x = y; } template <class T> inline void gmax(T &x, const T &y) { if (x < y) x = y; } const int BufferSize = 1 << 16; char buffer[BufferSize], *Bufferhead, *Buffertail; bool Terminal; inline char Getchar() { if (Bufferhead == Buffertail) { int l = fread(buffer, 1, BufferSize, stdin); if (!l) { Terminal = 1; return 0; } Buffertail = (Bufferhead = buffer) + l; } return *Bufferhead++; } template <class T> inline bool read(T &x) { x = 0; char c = Getchar(), rev = 0; while (c < '0' || c > '9') { c = Getchar(); rev |= c == '-'; if (Terminal) return 0; } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = Getchar(); if (c == '.') { c = Getchar(); double t = 0.1; while (c >= '0' && c <= '9') x = x + (c - '0') * t, c = Getchar(), t = t / 10; } x = rev ? -x : x; return 1; } template <class T1, class T2> inline bool read(T1 &x, T2 &y) { return read(x) & read(y); } template <class T1, class T2, class T3> inline bool read(T1 &x, T2 &y, T3 &z) { return read(x) & read(y) & read(z); } template <class T1, class T2, class T3, class T4> inline bool read(T1 &x, T2 &y, T3 &z, T4 &w) { return read(x) & read(y) & read(z) & read(w); } inline bool reads(char *x) { char c = Getchar(); while (c < 33 || c > 126) { c = Getchar(); if (Terminal) return 0; } while (c >= 33 && c <= 126) (*x++) = c, c = Getchar(); *x = 0; return 1; } template <class T> inline void print(T x, const char c = '\n') { if (!x) { putchar('0'); putchar(c); return; } if (x < 0) putchar('-'), x = -x; int m = 0, a[20]; while (x) a[m++] = x % 10, x /= 10; while (m--) putchar(a[m] + '0'); putchar(c); } const long long inf = 1ll << 42; const int N = 1000005, M = 100005, mod = 1e9 + 7, day = 1e6; template <class T, class S> inline void ch(T &x, const S y) { x = (x + y) % mod; } inline int exp(int x, int y, const int mod = ::mod) { int ans = 1; while (y) { if (y & 1) ans = (long long)ans * x % mod; x = (long long)x * x % mod; y >>= 1; } return ans; } int n, m, k; long long l[N], r[N], now, c[N]; vector<pair<int, int> > g[N], f[N]; int main() { read(n, m, k); for (int i = 1; i <= m; i++) { int d, s, t, c; read(d, s, t, c); if (s) g[d].push_back(make_pair(s, c)); else f[d].push_back(make_pair(t, c)); } now = 0; for (int i = 1; i <= n; i++) c[i] = inf, now += c[i]; for (int i = 1; i <= day; i++) { for (auto j : g[i]) if (j.second < c[j.first]) now -= c[j.first] - j.second, c[j.first] = j.second; l[i] = now; } now = 0; for (int i = 1; i <= n; i++) c[i] = inf, now += c[i]; for (int i = day; i; i--) { for (auto j : f[i]) if (j.second < c[j.first]) now -= c[j.first] - j.second, c[j.first] = j.second; r[i] = now; } long long ans = inf; for (int i = 1; i <= day; i++) { if (i + k + 1 > day) continue; gmin(ans, l[i] + r[i + k + 1]); } print(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.util.*; import java.io.*; public class b implements Runnable { PrintWriter out; int n, m, k; long[] costs, retCosts; TreeMap<Long, Integer>[] rets; long oo = 1000000000000L; public void main() throws Throwable { FastScanner in = new FastScanner(System.in); out = new PrintWriter(System.out); n = in.nextInt(); m = in.nextInt(); k = in.nextInt(); costs = new long[n]; retCosts = new long[n]; Arrays.fill(costs, oo); Arrays.fill(retCosts, oo); rets = new TreeMap[n]; for (int i = 0; i < n; i++) { rets[i] = new TreeMap<>(); rets[i].put(oo, 1); } long cost = oo * n * 2; Flight[] fs = new Flight[m]; for (int i = 0; i < m; i++) { Flight f = fs[i] = new Flight(in.nextInt(), in.nextInt(), in.nextInt(), in.nextLong()); if (f.a == 0) { if (!rets[f.b - 1].containsKey(f.cost)) rets[f.b - 1].put(f.cost, 1); else rets[f.b - 1].put(f.cost, rets[f.b - 1].get(f.cost) + 1); f.t -= k; } } Arrays.sort(fs); for (int i = 0; i < n; i++) { cost -= retCosts[i]; retCosts[i] = rets[i].firstKey(); cost += retCosts[i]; } long ans = oo * n; for (Flight f : fs) { //System.out.println(cost + " " + Arrays.toString(costs) + " " + Arrays.toString(retCosts)); if (f.a == 0) { int count = rets[f.b - 1].get(f.cost); if (count == 1) { rets[f.b - 1].remove(f.cost); } else { rets[f.b - 1].put(f.cost, count - 1); } cost -= retCosts[f.b - 1]; long tmp = rets[f.b - 1].firstKey(); cost += tmp; retCosts[f.b - 1] = tmp; continue; } int i = f.a - 1; cost -= costs[i]; costs[i] = Math.min(costs[i], f.cost); cost += costs[i]; ans = Math.min(ans, cost); } if (ans >= oo) { out.println(-1); } else { out.println(ans); } out.close(); } static class Flight implements Comparable<Flight> { int t, a, b; long cost; public Flight(int tt, int aa, int bb, long costt) { t = tt; a = aa; b = bb; cost = costt; } public int compareTo(Flight f) { if (t == f.t) { if (a == f.a) return 0; if (a == 0) return -1; if (f.a == 0) return 1; return 0; } return Integer.compare(t, f.t); } } public static void main(String[] args) throws Exception { new Thread(null, new b(), "b", 1L << 28).start(); } public void run() { try { main(); } catch (Throwable t) { t.printStackTrace(); System.exit(-1); } } static void sort(int[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 1; i < n; i++) { int j = rand.nextInt(i); int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static void sort(long[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 1; i < n; i++) { int j = rand.nextInt(i); long tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static void sort(double[] arr) { Random rand = new Random(); int n = arr.length; for (int i = 1; i < n; i++) { int j = rand.nextInt(i); double tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } Arrays.sort(arr); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(InputStream i) { br = new BufferedReader(new InputStreamReader(i)); st = new StringTokenizer(""); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public int[] nextIntArr(int n) throws Exception { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } public double[] nextDoubleArr(int n) throws Exception { double[] arr = new double[n]; for (int i = 0; i < n; i++) arr[i] = nextDouble(); return arr; } public long[] nextLongArr(int n) throws Exception { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } public int[] nextOffsetIntArr(int n) throws Exception { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt() - 1; return arr; } public int[][] nextIntArr(int n, int m) throws Exception { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = nextInt(); return arr; } public double[][] nextDoubleArr(int n, int m) throws Exception { double[][] arr = new double[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = nextDouble(); return arr; } public long[][] nextLongArr(int n, int m) throws Exception { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) arr[i][j] = nextLong(); return arr; } } }
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 unsigned long long N = 2000005; const unsigned long long M = 10921000; const unsigned long long INF = (1LL << 50); const unsigned long long MOD = 1e9 + 7; const double eps = 1e-4; unsigned long long n, m, k; struct nd { unsigned long long d; unsigned long long u, v; unsigned long long c; } s[N]; unsigned long long dp[N][3]; struct md { unsigned long long t[N]; void ini() { memset(t, 0, sizeof t); } void add(int p, unsigned long long x) { for (int i = p; i < N; i += i & (-i)) t[i] += x; } unsigned long long sum(unsigned long long p) { unsigned long long ans = 0; for (int i = p; i > 0; i -= i & (-i)) ans += t[i]; return ans; } } tr, tp; bool cmp(nd a, nd b) { return a.d < b.d; } int main() { while (~scanf("%I64d%I64d%I64d", &n, &m, &k)) { memset(dp, 0, sizeof dp); tr.ini(); tp.ini(); for (unsigned long long i = 0; i < N / 2; i++) dp[i][0] = dp[i][1] = INF; unsigned long long ma = 0; for (unsigned long long i = 0; i < m; i++) { unsigned long long a, b, c, d; scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &d); s[i].d = a; s[i].u = b; s[i].v = c; s[i].c = d; ma = max(ma, a); } sort(s, s + m, cmp); for (unsigned long long i = 0; i < m; i++) { if (s[i].v == 0) { unsigned long long day = s[i].d; unsigned long long p = s[i].u; unsigned long long cost = s[i].c; unsigned long long tmp = tr.sum(p) - tr.sum(p - 1); if (tmp == 0 || tmp > cost) { tr.add(p, -tmp); tr.add(p, cost); } if (tp.sum(p) - tp.sum(p - 1) == 0) tp.add(p, 1); if (tp.sum(n) == n) dp[day][0] = min(dp[day][0], tr.sum(n)); } } tr.ini(); tp.ini(); for (int i = m - 1; i >= 0; i--) { if (s[i].u == 0) { unsigned long long day = s[i].d; unsigned long long p = s[i].v; unsigned long long cost = s[i].c; unsigned long long tmp = tr.sum(p) - tr.sum(p - 1); if (tmp == 0 || tmp > cost) { tr.add(p, -tmp); tr.add(p, cost); } if (tp.sum(p) - tp.sum(p - 1) == 0) tp.add(p, 1); if (tp.sum(n) == n) dp[day][1] = min(dp[day][1], tr.sum(n)); } } for (int i = 1; i <= ma; i++) { dp[i][0] = min(dp[i - 1][0], dp[i][0]); } for (int i = ma - 1; i > 0; i--) { dp[i][1] = min(dp[i + 1][1], dp[i][1]); } unsigned long long ans = INF; for (int i = 1; i + k + 1 <= ma; i++) { ans = min(ans, dp[i][0] + dp[i + k + 1][1]); } if (ans >= INF) ans = -1; 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 maxn = 1e6 + 1; const long long inf = 1e18; int n, m, k; struct Flight { int d, s, t, c; } f[maxn]; long long mn[maxn], pre[maxn], s; vector<int> sw[maxn]; int cnt; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> f[i].d >> f[i].s >> f[i].t >> f[i].c; sw[f[i].d].push_back(i); } fill(mn + 1, mn + n + 1, inf); pre[0] = inf; cnt = 0; for (int i = 1; i < maxn; i++) { for (auto qi : sw[i]) if (f[qi].t == 0 && f[qi].c < mn[f[qi].s]) { if (mn[f[qi].s] == inf) cnt++; else s -= mn[f[qi].s]; s += (mn[f[qi].s] = f[qi].c); } pre[i] = cnt == n ? s : inf; } cerr << pre[2] << '\n'; s = cnt = 0; fill(mn + 1, mn + n + 1, inf); long long ans = inf, cur = inf; for (int i = maxn - 1; i >= k + 1; i--) { for (auto qi : sw[i]) if (f[qi].s == 0 && f[qi].c < mn[f[qi].t]) { if (mn[f[qi].t] == inf) cnt++; else s -= mn[f[qi].t]; s += (mn[f[qi].t] = f[qi].c); } cur = min(cur, cnt == n ? s : inf); ans = min(ans, cur + pre[i - k - 1]); } if (ans == inf) cout << -1 << '\n'; 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; int n, m, k, t, x, y, z, l, r, cnt1, cnt2; struct edgm { int next, to, v; } e1[100100], e2[100100]; int head1[1000100], head2[1000100], c1[100100], c2[100100]; long long a[1000100], b[1000100], ans = -1; void add1() { e1[++cnt1].next = head1[t]; e1[cnt1].to = x; e1[cnt1].v = z; head1[t] = cnt1; } void add2() { e2[++cnt2].next = head2[t]; e2[cnt2].to = y; e2[cnt2].v = z; head2[t] = cnt2; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%d", &t, &x, &y, &z); if (x) add1(); else add2(); } cnt1 = cnt2 = 0; for (int i = 1; i <= 1000000; i++) { a[i] = a[i - 1]; for (int j = head1[i]; j; j = e1[j].next) { x = e1[j].to; y = e1[j].v; if (!c1[x]) { cnt1++; c1[x] = y; a[i] += c1[x]; } else if (c1[x] > y) { a[i] += y; a[i] -= c1[x], c1[x] = y; } } if (!l && cnt1 == n) l = i; } for (int i = 1000000; i >= 1; i--) { b[i] = b[i + 1]; for (int j = head2[i]; j; j = e2[j].next) { x = e2[j].to; y = e2[j].v; if (!c2[x]) { cnt2++; c2[x] = y; b[i] += c2[x]; } else if (c2[x] > y) { b[i] += y; b[i] -= c2[x], c2[x] = y; } } if (!r && cnt2 == n) r = i; } if (l == 0 || r == 0) { printf("-1\n"); return 0; } for (int i = l; i + k + 1 <= r; i++) { if (ans == -1) ans = a[i] + b[i + k + 1]; else ans = min(ans, a[i] + b[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 = 100100; const int maxm = maxn * 30; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; struct node { int day, from, to, c; bool operator<(const node& p) const { return day < p.day; } } use[maxn]; bool lok[maxn], rok[maxn]; long long l[maxn], r[maxn]; int n, m, k; int co[maxn]; void init() { long long now = 0; int cnt = 0; memset(co, -1, sizeof(co)); for (int i = 1; i <= m; i++) { if (use[i].to == 0) { int f = use[i].from; if (co[f] == -1) { now += use[i].c; co[f] = use[i].c; cnt++; } else { if (use[i].c < co[f]) { now -= co[f] - use[i].c; co[f] = use[i].c; } } } if (cnt >= n) { lok[i] = 1; l[i] = now; } } memset(co, -1, sizeof(co)); now = 0, cnt = 0; for (int i = m; i > 0; i--) { if (use[i].from == 0) { int t = use[i].to; if (co[t] == -1) { now += use[i].c; co[t] = use[i].c; cnt++; } else { if (use[i].c < co[t]) { now -= co[t] - use[i].c; co[t] = use[i].c; } } } if (cnt >= n) { rok[i] = 1; r[i] = now; } } } int get(int x) { node tmp; tmp.day = x; return lower_bound(use + 1, use + 1 + m, tmp) - use; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; long long ans = INF; for (int i = 1; i <= m; i++) { cin >> use[i].day >> use[i].from >> use[i].to >> use[i].c; if (use[i].to == 0) use[i].day++; } sort(use + 1, use + m + 1); init(); for (int i = 1; i <= m; i++) { if (lok[i]) { int d = use[i].day + k; int s = get(d); if (s > m) break; if (rok[s]) { ans = min(ans, l[i] + r[s]); } } } if (ans == INF) 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 long long INF = 1LL << 60; using P = pair<long long, long long>; template <class T> struct SegmentTree { using F = function<T(T, T)>; F f; T inf; long long n; vector<T> node; SegmentTree() {} SegmentTree(vector<T> v, T inf, F f) : inf(inf), f(f) { n = 1; while (n < v.size()) n *= 2; node.resize(2 * n - 1, inf); for (long long i = 0; i < v.size(); i++) node[i + n - 1] = v[i]; for (long long i = n - 2; i >= 0; i--) node[i] = f(node[2 * i + 1], node[2 * i + 2]); } void update(long long k, T val) { k += n - 1; node[k] = val; while (k > 0) { k = (k - 1) / 2; node[k] = f(node[2 * k + 1], node[2 * k + 2]); } } void add(long long k, T val) { k += n - 1; node[k] += val; while (k > 0) { k = (k - 1) / 2; node[k] = f(node[2 * k + 1], node[2 * k + 2]); } } T operator[](long long x) { return getval(x, x + 1); } T getval(long long a, long long b) { return getval(a, b, 0, 0, n); } T getval(long long a, long long b, long long k, long long l, long long r) { if (r <= a || b <= l) return inf; if (a <= l && r <= b) return node[k]; T vl = getval(a, b, 2 * k + 1, l, (l + r) / 2); T vr = getval(a, b, 2 * k + 2, (l + r) / 2, r); return f(vl, vr); } void print(long long n) { for (long long i = 0; i < n; i++) { cout << getval(i, i + 1) << " "; } cout << endl; } }; signed main() { long long n, m, k; cin >> n >> m >> k; vector<long long> d(m), u(m), v(m), c(m); vector<P> g(m); for (long long i = 0; i < m; i++) { cin >> d[i] >> u[i] >> v[i] >> c[i]; g[i] = {d[i], i}; } sort(g.begin(), g.end()); SegmentTree<long long> people( vector<long long>(n, 0), 0, [&](long long x, long long y) { return (x + y); }); SegmentTree<long long> cost( vector<long long>(n, 0), 0, [&](long long x, long long y) { return (x + y); }); vector<long long> go(2e6, INF); for (long long i = 0; i < m; i++) { long long day = g[i].first, idx = g[i].second; if (v[idx] != 0) continue; people.update(u[idx] - 1, 1); long long pre = cost[u[idx] - 1]; if (pre == 0 || pre > c[idx]) cost.update(u[idx] - 1, c[idx]); go[day] = (people.getval(0, n) == n ? cost.getval(0, n) : INF); } for (long long i = 0; i < 2e6 - 1; i++) { go[i + 1] = min(go[i + 1], go[i]); } sort(g.rbegin(), g.rend()); SegmentTree<long long> people2( vector<long long>(n, 0), 0, [&](long long x, long long y) { return (x + y); }); SegmentTree<long long> cost2( vector<long long>(n, 0), 0, [&](long long x, long long y) { return (x + y); }); vector<long long> back(2e6, INF); for (long long i = 0; i < m; i++) { long long day = g[i].first, idx = g[i].second; if (u[idx] != 0) continue; people2.update(v[idx] - 1, 1); long long pre = cost2[v[idx] - 1]; if (pre == 0 || pre > c[idx]) cost2.update(v[idx] - 1, c[idx]); back[day] = (people2.getval(0, n) == n ? cost2.getval(0, n) : INF); } for (long long i = 2e6 - 2; i >= 0; i--) { back[i] = min(back[i + 1], back[i]); } long long ans = INF; for (long long i = 0; i < 1e6; i++) { ans = min(ans, go[i] + back[i + k + 1]); } if (ans >= INF) 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; struct moja { long long d, x, y, c; } r[100005]; int cmp(struct moja qq, struct moja ww) { if (qq.d != ww.d) { return qq.d < ww.d; } } long long q, i, dosao[100005], otisao[100005], usao, izasao, m, n, k, md[100005], mi[100005], sol, dsol[100005], isol[100005], gusao, gizasao, j; int main() { usao = 10000001; scanf("%I64d%I64d%I64d", &n, &m, &k); gusao = m + 1; for (i = 1; i <= m; i++) { scanf("%I64d%I64d%I64d%I64d", &r[i].d, &r[i].x, &r[i].y, &r[i].c); } sort(r + 1, r + m + 1, cmp); q = 0; for (i = 1; i <= m; i++) { if (r[i].x != 0) { dosao[r[i].x]++; if (dosao[r[i].x] == 1) { q++; if (q == n) { usao = r[i].d; gusao = i; } } } } q = 0; for (i = m; i >= 1; i--) { if (r[i].x == 0) { otisao[r[i].y]++; if (otisao[r[i].y] == 1) { q++; if (q == n) { izasao = r[i].d; gizasao = i; } } } } for (i = 1; i <= n; i++) { md[i] = 1000001; mi[i] = 1000001; } dsol[0] = md[1] * n; for (i = 1; i <= m; i++) { dsol[i] = dsol[i - 1]; if (r[i].x != 0) { if (r[i].c < md[r[i].x]) { dsol[i] = dsol[i] - (md[r[i].x] - r[i].c); md[r[i].x] = r[i].c; } } } isol[m + 1] = mi[1] * n; for (i = m; i >= 1; i--) { isol[i] = isol[i + 1]; if (r[i].x == 0) { if (r[i].c < mi[r[i].y]) { isol[i] = isol[i] - (mi[r[i].y] - r[i].c); mi[r[i].y] = r[i].c; } } } sol = dsol[0] + isol[m + 1]; j = gusao; for (i = gusao; i <= gizasao; i++) { while (1) { if (r[j].d - r[i].d > k) break; j++; if (j > gizasao) break; } if (j > gizasao) break; if (dsol[i] + isol[j] < sol) sol = dsol[i] + isol[j]; } if (sol < dsol[0] + isol[m + 1]) printf("%I64d", sol); else printf("-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 int MAXN = 1e6 + 5, MAX = 1e6; vector<pair<int, int> > a[MAXN], b[MAXN]; long long cost[MAXN], prefc[MAXN], sufc[MAXN]; int preft[MAXN], suft[MAXN], N, M, K; 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); if (!t) a[d].push_back({f, c}); else b[d].push_back({t, c}); } for (int i = 1; i <= MAX; ++i) { prefc[i] = prefc[i - 1], preft[i] = preft[i - 1]; for (pair<int, int> it : a[i]) { int city = it.first, w = it.second; if (!cost[city]) preft[i]++, prefc[i] += w, cost[city] = w; else if (cost[city] > w) { prefc[i] = prefc[i] - cost[city] + w; cost[city] = w; } } } memset(cost, 0, sizeof(cost)); for (int i = MAX; i >= 1; --i) { sufc[i] = sufc[i + 1], suft[i] = suft[i + 1]; for (pair<int, int> it : b[i]) { int city = it.first, w = it.second; if (!cost[city]) suft[i]++, sufc[i] += w, cost[city] = w; else if (cost[city] > w) { sufc[i] = sufc[i] - cost[city] + w; cost[city] = w; } } } long long res = 1000000000000000000LL; for (int i = 1; i + K + 1 <= MAX; ++i) if (preft[i] == N && suft[i + K + 1] == N) res = min(res, prefc[i] + sufc[i + K + 1]); printf("%lld\n", res == 1000000000000000000LL ? -1 : 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> #pragma comment(linker, "/STACK:268435456") using namespace std; template <typename T> inline T abs(T a) { return ((a < 0) ? -a : a); } template <typename T> inline T sqr(T a) { return a * a; } template <class T> T gcd(T a, T b) { return a ? gcd(b % a, a) : b; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> T sign(T a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; const int dxK[] = {-1, -1, 0, 1, 1, 1, 0, -1}; const int dyK[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dxKn[] = {-2, -1, 1, 2, 2, 1, -1, -2}; const int dyKn[] = {1, 2, 2, 1, -1, -2, -2, -1}; const int N = int(1000 * 1000) + 5; const int M = int(10000) + 9; const int LOGN = 21; const int SQN = 350; const int MOD = int(1e9) + 7; const int INF = int(1e9) + 100; const long long INF64 = 2e18; const double PI = double(3.1415926535897932384626433832795); const double e = double(2.7182818284590452353602874713527); const double EPS = 1e-9; struct wtf { int v, to, c; wtf() {} wtf(int v, int to, int c) : v(v), to(to), c(c) {} }; int n, m, k; vector<wtf> a[N]; multiset<int> qin[N], qout[N]; int cntin, cntout; long long sum; void addin(int t) { if (t >= N) return; for (int i = 0; i < (int)((int)(a[t].size())); ++i) { wtf cur = a[t][i]; if (cur.to != -1) continue; int v = cur.v; if ((int)(qin[v].size()) == 0) { ++cntin; sum += cur.c; qin[v].insert(cur.c); } else { sum -= *qin[v].begin(); qin[v].insert(cur.c); sum += *qin[v].begin(); } } } void addout(int t) { if (t >= N) return; for (int i = 0; i < (int)((int)(a[t].size())); ++i) { wtf cur = a[t][i]; if (cur.v != -1) continue; int v = cur.to; if ((int)(qout[v].size()) == 0) { ++cntout; sum += cur.c; qout[v].insert(cur.c); } else { sum -= *qout[v].begin(); qout[v].insert(cur.c); sum += *qout[v].begin(); } } } void delout(int t) { if (t >= N) return; for (int i = 0; i < (int)((int)(a[t].size())); ++i) { wtf cur = a[t][i]; if (cur.v != -1) continue; int v = cur.to; if ((int)(qout[v].size()) == 1) { assert(qout[v].count(cur.c) != 0); --cntout; sum -= cur.c; qout[v].erase(cur.c); } else { sum -= *qout[v].begin(); qout[v].erase(qout[v].find(cur.c)); sum += *qout[v].begin(); } } } void solve() { cin >> n >> m >> k; for (int i = 0; i < (int)(m); ++i) { int d, v, to, c; scanf("%d %d %d %d", &d, &v, &to, &c); --v, --to; a[d].push_back(wtf(v, to, c)); } long long res = INF64; addin(0); for (int i = k + 1; i < N; ++i) addout(i); for (int i = 1; i < N; ++i) { if (cntin == cntout && cntin == n) res = min(res, sum); addin(i); delout(i + k); } if (res == INF64) res = -1; cout << res << endl; } int main() { srand(time(NULL)); cout << setprecision(25) << fixed; cerr << setprecision(25) << fixed; solve(); 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 pre[1000010]; long long suf[1000010]; int cost[100010]; int see[100010]; vector<int> f[1000010], t[1000010], c[1000010]; const int maxn = 1000000; const int inf = 1e9; const long long INF = 1e16; int main(int argc, char *argv[]) { 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); f[D].push_back(F); t[D].push_back(T); c[D].push_back(C); } long long sum = 0; int tot = 0; for (int i = 1; i <= n; i++) { see[i] = 0; cost[i] = inf; sum += inf; } for (int i = 1; i <= maxn; i++) { for (size_t j = 0; j < f[i].size(); j++) { int F = f[i][j]; int T = t[i][j]; int C = c[i][j]; if (F == 0) continue; if (see[F] == 0) ++tot; see[F] = 1; sum -= cost[F]; cost[F] = min(cost[F], C); sum += cost[F]; } if (tot < n) pre[i] = -1; else pre[i] = sum; } sum = 0; tot = 0; for (int i = 1; i <= n; i++) { see[i] = 0; cost[i] = inf; sum += inf; } for (int i = maxn; i >= 1; i--) { for (size_t j = 0; j < f[i].size(); j++) { int F = f[i][j]; int T = t[i][j]; int C = c[i][j]; if (T == 0) continue; if (see[T] == 0) ++tot; see[T] = 1; sum -= cost[T]; cost[T] = min(cost[T], C); sum += cost[T]; } if (tot < n) suf[i] = -1; else suf[i] = sum; } long long ans = INF; for (int i = 1; i <= maxn; i++) { if (i + k + 1 > maxn) continue; if (pre[i] == -1 || suf[i + k + 1] == -1) continue; ans = min(ans, pre[i] + suf[i + k + 1]); } if (ans == INF) 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
import java.io.*; import java.util.*; public class Main { private InputStream is; private PrintWriter out; int time = 0, DP[], start[], end[], dist[], black[], MOD = (int) (1e9 + 7), arr[], weight[][], x[], y[], parent[]; int MAX = 1000000, N, K; long red[]; ArrayList<Character>[] amp; ArrayList<Pair>[][] pmp; boolean b[], boo[][]; Pair prr[]; HashMap<Pair, Integer> hm = new HashMap(); long Dp[][][][] = new long[110][110][12][12]; void soln() { is = System.in; out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); // out.close(); out.flush(); // tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { // new CODEFORCES().soln(); } catch (Exception e) { System.out.println(e); } } }, "1", 1 << 26).start(); new Main().soln(); } int ans = 0, cost = 0, D[][]; char ch[], ch1[]; int hash = 29; TreeSet<Cell> ts; int x2, y2; HashSet<Integer>[] hs = (HashSet<Integer>[]) new HashSet[110]; void solve() { int n = ni(), m = ni(), k = ni(); int arr[][] = new int[MAX][4]; int min[] = new int[MAX+10]; Arrays.fill(min, 10000000); int max[] = new int[MAX+10]; for(int i = 0; i< m; i++){ for(int j = 0; j<4;j++) arr[i][j] = ni(); if(arr[i][1]!=0) min[arr[i][1]] = min(min[arr[i][1]],arr[i][0]); else max[arr[i][2]] = max(max[arr[i][2]],arr[i][0]); } ArrayList<Flight>[] amp = new ArrayList[MAX+10]; for(int i = 0; i<MAX+10;i++) amp[i] = new ArrayList<Flight>(); TreeSet<Flight>[] ts = new TreeSet[MAX+10]; for(int i = 0; i<n+10;i++) ts[i] = new TreeSet<Flight>(); for(int i = 0; i< m;i++){ amp[arr[i][0]].add(new Flight(arr[i][1],arr[i][2],arr[i][3])); } int mf = 0, ma = 1000000000; for(int i = 1; i<=n;i++){ //System.out.println(min[i]+" "+max[i]); mf = max(mf,min[i]); ma = min(ma,max[i]); } //System.out.println(ma+" "+mf); if((ma-mf-1)<k){ System.out.println(-1); } else{ long cost = 0, fa = Long.MAX_VALUE; int y[] = new int[MAX+10]; Arrays.fill(y, 1000000000); int z[] = new int[MAX+10]; Arrays.fill(z, 1000000000); for(int i = 0; i<m;i++){ if(arr[i][0]<=mf && arr[i][1]!=0){ y[arr[i][1]] = min(y[arr[i][1]],arr[i][3]); //System.out.println(y[arr[i][2]]); } else if(arr[i][0]>(mf+k) && arr[i][1]==0){ z[arr[i][2]] = min(z[arr[i][2]],arr[i][3]); ts[arr[i][2]].add(new Flight(arr[i][1],arr[i][2],arr[i][3])); } } for(int i = 1; i<=n;i++){ //System.out.println(y[i]+" "+z[i]); cost += (y[i]+z[i]); } fa = cost; //System.out.println(cost); for(int j = mf+1;j<=(ma-k-1);j++){ for(int l = 0; l<amp[j].size();l++){ Flight f = amp[j].get(l); int a1 = f.f, a3 = f.c; if(a1!=0){ cost-=y[a1]; y[a1] = min(y[a1],a3); cost+=y[a1]; } } for(int l = 0; l<amp[j+k].size();l++){ Flight f = amp[j+k].get(l); //System.out.println(j+k+1); int a1 = f.f, a2 = f.s; if(a1==0){ cost-=z[a2]; ts[a2].remove(f); z[a2] = ts[a2].first().c; cost+=z[a2]; } } fa = Math.min(fa, cost); //System.out.println(j+" "+fa); } System.out.println(fa); } } private class Flight implements Comparable<Flight>{ int f, s,c; Flight(int f, int s, int c){ this.f = f; this.s = s; this.c = c; } @Override public int compareTo(Flight other) { // TODO Auto-generated method stub return (Long.compare(c, other.c)); } public String toString() { return this.f + " " + this.s +" "+this.c; } } void getFactors(int n,int x){ for(int i = 1;i*i<=n;i++){ if(n%i==0){ hs[x].add(i); hs[x].add(n/i); } } } void recur(int a, int b, int k){ if(k==0){ if(a==x2 && b==y2) ans++; return; } recur(a,b+1,k-1); recur(a,b-1,k-1); recur(a+1,b,k-1); recur(a-1,b,k-1); } int min(int a, int b) { return (a<b)?a:b; } private class Cell implements Comparable<Cell> { int u, v, s; public Cell(int u, int v, int s) { this.u = u; this.v = v; this.s = s; } public int hashCode() { return Objects.hash(); } public int compareTo(Cell other) { return (Long.compare(s, other.s) != 0 ? (Long.compare(s, other.s)):(Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u))) &((Long.compare(s, other.s) != 0 ? (Long.compare(s, other.s)):(Long.compare(u, other.v)!=0?Long.compare(u, other.v):Long.compare(v, other.u)))); } public String toString() { return this.u + " " + this.v; } } class Pair implements Comparable<Pair> { int u, v, i; Pair(int u, int v) { this.u = u; this.v = v; } Pair(int u, int v, int i) { this.u = u; this.v = v; this.i = i; } public int hashCode() { return Objects.hash(); } public boolean equals(Object o) { Pair other = (Pair) o; return ((u == other.u && v == other.v)); } public int compareTo(Pair other) { // return Integer.compare(val, other.val); return Long.compare(u, other.u) != 0 ? (Long.compare(u, other.u)): (Long.compare(v, other.v));//((Long.compare(u, other.u) != 0 ? (Long.compare(u, other.u)): (Long.compare(v, other.v))) //&(Long.compare(u, other.v) != 0 ? (Long.compare(u, other.v)): (Long.compare(v, other.u)))); } public String toString() { return this.u + " " + this.v; } } int max(int a, int b) { if (a > b) return a; return b; } /*public static class FenwickTree { int[] array; // 1-indexed array, In this array We save cumulative // information to perform efficient range queries and // updates public FenwickTree(int size) { array = new int[size + 1]; } public int rsq(int ind) { assert ind > 0; int sum = 0; while (ind > 0) { sum += array[ind]; // Extracting the portion up to the first significant one of the // binary representation of 'ind' and decrementing ind by that // number ind -= ind & (-ind); } return sum; } public int rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, int value) { assert ind > 0; while (ind < array.length) { array[ind] += value; // Extracting the portion up to the first significant one of the // binary representation of 'ind' and incrementing ind by that // number ind += ind & (-ind); } } public int size() { return array.length - 1; } } */ void buildGraph(int n) { for (int i = 0; i < n; i++) { int x1 = ni() - 1, y1 = ni() - 1, z = ni(); ts.add(new Cell(x1,y1,z)); hm.put(new Pair(x1,y1), z); ans += z; } } long modInverse(long a, long mOD2) { return power(a, mOD2 - 2, mOD2); } long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } public long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static class ST1 { int arr[], st[], size; ST1(int a[]) { arr = a.clone(); size = 10 * arr.length; st = new int[size]; build(0, arr.length - 1, 1); } void build(int ss, int se, int si) { if (ss == se) { st[si] = arr[ss]; return; } int mid = (ss + se) / 2; int val = 2 * si; build(ss, mid, val); build(mid + 1, se, val + 1); st[si] = Math.min(st[val], st[val + 1]); } int get(int ss, int se, int l, int r, int si) { if (l > se || r < ss || l > r) return Integer.MAX_VALUE; if (l <= ss && r >= se) return st[si]; int mid = (ss + se) / 2; int val = 2 * si; return Math.min(get(ss, mid, l, r, val), get(mid + 1, se, l, r, val + 1)); } } static class ST { int arr[], lazy[], n; ST(int a) { n = a; arr = new int[10 * n]; lazy = new int[10 * n]; } void up(int l, int r, int val) { update(0, n - 1, 0, l, r, val); } void update(int l, int r, int c, int x, int y, int val) { if (lazy[c] != 0) { lazy[2 * c + 1] += lazy[c]; lazy[2 * c + 2] += lazy[c]; if (l == r) arr[c] += lazy[c]; lazy[c] = 0; } if (l > r || x > y || l > y || x > r) return; if (x <= l && y >= r) { lazy[c] += val; return; } int mid = l + r >> 1; update(l, mid, 2 * c + 1, x, y, val); update(mid + 1, r, 2 * c + 2, x, y, val); arr[c] = Math.max(arr[2 * c + 1], arr[2 * c + 2]); } int an(int ind) { return ans(0, n - 1, 0, ind); } int ans(int l, int r, int c, int ind) { if (lazy[c] != 0) { lazy[2 * c + 1] += lazy[c]; lazy[2 * c + 2] += lazy[c]; if (l == r) arr[c] += lazy[c]; lazy[c] = 0; } if (l == r) return arr[c]; int mid = l + r >> 1; if (mid >= ind) return ans(l, mid, 2 * c + 1, ind); return ans(mid + 1, r, 2 * c + 2, ind); } } public static int[] shuffle(int[] a, Random gen) { for (int i = 0, n = a.length; i < n; i++) { int ind = gen.nextInt(n - i) + i; int d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } long power(long x, long y, int mod) { long ans = 1; while (y > 0) { if (y % 2 == 0) { x = (x * x) % mod; y /= 2; } else { ans = (x * ans) % mod; y--; } } return ans; } // To Get Input // Some Buffer Methods 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 = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(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 (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(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 && !(isSpaceChar(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 ticket { int day, start, end; long long cost; ticket(int day = 0, int start = 0, int end = 0, long long cost = 0) : day(day), start(start), end(end), cost(cost) {} bool operator<(ticket other) const { return day < other.day; } }; struct minimun_queue { stack<pair<int, int> > s1, s2; void push(int value) { int minimun = s1.empty() ? value : min(value, s1.top().second); s1.push(pair<int, int>(value, minimun)); } int findMinimun() { if (s1.empty() && s2.empty()) return 0; else { if (s1.empty() || s2.empty()) { return s1.empty() ? s2.top().second : s1.top().second; } else { return min(s1.top().second, s2.top().second); } } } bool pop() { if (s2.empty()) { while (!s1.empty()) { int element = s1.top().first; s1.pop(); int minimun = s2.empty() ? element : min(element, s2.top().second); s2.push(pair<int, int>(element, minimun)); } } if (s2.empty()) return false; s2.pop(); return true; } int size() { return s1.size() + s2.size(); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k, d, f, t, c; cin >> n >> m >> k; ticket v[m]; for (int i = 0; i < m; i++) { cin >> d >> f >> t >> c; v[i] = ticket(d, f, t, c); } sort(v, v + m); int len = 0, len1 = 0, pos = 0, pos1; long long ans = 1e18, sum = 0; minimun_queue q[2][n + 1]; int day = 1; for (; day <= 1000000 && len < n; day++) { while (pos < m && v[pos].day <= day) { if (v[pos].end == 0) { q[0][v[pos].start].push(v[pos].cost); if (q[0][v[pos].start].size() == 1) len++; } pos++; } if (len == n) break; } pos1 = pos; while (pos1 < m && v[pos1].day <= day + k) { pos1++; } for (int i = pos1; i < m; i++) { if (v[i].start == 0) { q[1][v[i].end].push(v[i].cost); if (q[1][v[i].end].size() == 1) len1++; } } for (int i = 1; i <= n; i++) { sum += q[0][i].findMinimun() + q[1][i].findMinimun(); } for (; day <= 1000000 && len == n && len1 == n && pos < m && pos1 < m; day++) { while (pos < m && v[pos].day <= day) { if (v[pos].end == 0) { sum -= q[0][v[pos].start].findMinimun(); q[0][v[pos].start].push(v[pos].cost); sum += q[0][v[pos].start].findMinimun(); } pos++; } while (pos1 < m && v[pos1].day <= day + k) { if (v[pos1].start == 0) { sum -= q[1][v[pos1].end].findMinimun(); q[1][v[pos1].end].pop(); sum += q[1][v[pos1].end].findMinimun(); if (q[1][v[pos1].end].size() == 0) len1--; } pos1++; } if (len == n && len1 == n && sum < ans) { ans = sum; } } if (ans != 1e18) 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 = 1e5 + 10; const int D = 1e6; const long long INF = 1e18; int n, m, k; priority_queue<pair<int, int> > Q[maxn]; vector<pair<int, int> > in[D + 10]; vector<int> out[D + 10]; long long cost[maxn]; long long ans = INF; void solve() { int count = 0; long long sum = 0; for (int x = 1; x <= n; ++x) { while (!Q[x].empty() && Q[x].top().second < k) Q[x].pop(); if (Q[x].empty()) return; sum -= Q[x].top().first; } for (int d = 1; d <= D; ++d) { for (auto i : in[d - 1]) { int c = i.first; int id = i.second; if (!cost[id] || c < cost[id]) { if (!cost[id]) ++count; sum -= cost[id]; sum += c; cost[id] = c; } } if (d + k - 1 > D) break; for (auto x : out[d + k - 1]) { sum += Q[x].top().first; while (!Q[x].empty() && Q[x].top().second <= d + k - 1) Q[x].pop(); if (Q[x].empty()) return; sum -= Q[x].top().first; } if (count == n) ans = min(ans, sum); } } 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); if (!t) in[d].push_back({c, f}); else { Q[t].push({-c, d}); out[d].push_back(t); } } solve(); if (ans == INF) 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
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.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; 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; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, FastScanner in, PrintWriter out) { final int N = 1000010; final long infinity = (long) 1e15; int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int[] firstFlight = new int[N]; Arrays.fill(firstFlight, -1); int[] flightFrom = new int[m]; int[] flightTo = new int[m]; int[] flightNext = new int[m]; int[] flightCost = new int[m]; for (int i = 0; i < m; i++) { int d = in.nextInt() - 1; flightFrom[i] = in.nextInt(); flightTo[i] = in.nextInt(); flightCost[i] = in.nextInt(); flightNext[i] = firstFlight[d]; firstFlight[d] = i; } int l = -1; int r = -1; long[] costFrom = new long[N]; long[] costTo = new long[N]; int[] minSoFar = new int[n]; for (int step = 0; step < 2; step++) { int numBad = n; Arrays.fill(minSoFar, -1); long sum = 0; for (int dummyD = 0; dummyD < N; dummyD++) { int d = dummyD; if (step == 1) { d = N - dummyD - 1; } for (int f = firstFlight[d]; f >= 0; f = flightNext[f]) { if (step == 0 && flightTo[f] != 0) { continue; } if (step == 1 && flightFrom[f] != 0) { continue; } int city = flightTo[f] + flightFrom[f] - 1; if (minSoFar[city] < 0) { --numBad; } if (minSoFar[city] < 0) { sum += flightCost[f]; minSoFar[city] = flightCost[f]; } if (minSoFar[city] > flightCost[f]) { sum -= minSoFar[city]; minSoFar[city] = flightCost[f]; sum += minSoFar[city]; } } if (numBad == 0) { if (step == 0 && l < 0) { l = d; } if (step == 1 && r < 0) { r = d; } } if (step == 0) { costFrom[d] = sum; } if (step == 1) { costTo[d] = sum; } } } if (l < 0 || r < 0) { out.println(-1); return; } long[] bestCost1 = new long[N]; Arrays.fill(bestCost1, infinity); for (int i = l; i < N; i++) { bestCost1[i] = costFrom[i]; if (i > 0) { bestCost1[i] = Math.min(bestCost1[i], bestCost1[i - 1]); } } long[] bestCost2 = new long[N]; Arrays.fill(bestCost2, infinity); for (int i = r; i >= 0; i--) { bestCost2[i] = costTo[i]; if (i + 1 < N) { bestCost2[i] = Math.min(bestCost2[i], bestCost2[i + 1]); } } long ans = infinity; for (int i = 0; i + k + 1 < N; i++) { ans = Math.min(ans, bestCost1[i] + bestCost2[i + k + 1]); } if (ans >= infinity) { out.println(-1); } else { out.println(ans); } } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(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 maxn = 1000005; struct node { int d, s, t; long long c; } a[maxn]; long long x[maxn], y[maxn], cost[maxn]; int cmp(node a, node b) { return a.d < b.d; } int main() { memset(cost, -1, sizeof(cost)); int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%lld", &a[i].d, &a[i].s, &a[i].t, &a[i].c); } sort(a + 1, a + m + 1, cmp); int maxd = a[m].d, num = 0, now = 1; long long sum = 0; for (int i = 1; i <= maxd; i++) { while (a[now].d == i && now <= m) { if (a[now].s == 0) { now++; continue; } if (cost[a[now].s] == -1) { num++; cost[a[now].s] = a[now].c; sum += 1ll * a[now].c; } else if (cost[a[now].s] > a[now].c) { sum -= 1ll * (cost[a[now].s] - a[now].c); cost[a[now].s] = a[now].c; } now++; } if (num < n) x[i] = -1; else x[i] = sum; } memset(cost, -1, sizeof(cost)); num = 0, now = m, sum = 0; for (int i = maxd; i >= 1; i--) { while (a[now].d == i && now > 0) { if (a[now].t == 0) { now--; continue; } if (cost[a[now].t] == -1) { num++; cost[a[now].t] = 1ll * a[now].c; sum += 1ll * a[now].c; } else if (cost[a[now].t] > a[now].c) { sum -= 1ll * (cost[a[now].t] - 1ll * a[now].c); cost[a[now].t] = a[now].c; } now--; } if (num < n) y[i] = -1; else y[i] = sum; } long long ans = -1; for (int i = 1; i <= maxd - k - 1; i++) { if (x[i] == -1 || y[i + k + 1] == -1) continue; if (ans == -1 || x[i] + y[i + k + 1] < ans) ans = x[i] + y[i + k + 1]; } 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.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn Agrawal coderbond007 PLEASE!! PLEASE!! HACK MY SOLUTION!! */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public static final int DAYS = 1000000; public static final long INFTY = 1000_000_000_000L; public void solve(int testNumber, FastReader in, PrintWriter out) { int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int[] d = new int[m]; int[] f = new int[m]; int[] t = new int[m]; int[] c = new int[m]; in.nextIntArrays(d, f, t, c); MiscUtils.decreaseByOne(d, f, t); int[] first = ArrayUtils.createArray(DAYS, -1); int[] next = new int[m]; for (int i = 0; i < m; i++) { if (t[i] == -1) { next[i] = first[d[i]]; first[d[i]] = i; } } long[] inbound = new long[DAYS]; long[] current = ArrayUtils.createArray(n, INFTY); long result = INFTY * n; for (int i = 0; i < DAYS; i++) { for (int j = first[i]; j != -1; j = next[j]) { if (c[j] < current[f[j]]) { result -= current[f[j]]; current[f[j]] = c[j]; result += c[j]; } } inbound[i] = result; } ArrayUtils.fill(first, -1); for (int i = 0; i < m; i++) { if (f[i] == -1) { next[i] = first[d[i]]; first[d[i]] = i; } } ArrayUtils.fill(current, INFTY); result = INFTY * n; long[] outbound = new long[DAYS]; for (int i = DAYS - 1; i >= 0; i--) { for (int j = first[i]; j != -1; j = next[j]) { if (c[j] < current[t[j]]) { result -= current[t[j]]; current[t[j]] = c[j]; result += c[j]; } } outbound[i] = result; } long answer = Long.MAX_VALUE; for (int i = k + 1; i < DAYS; i++) { answer = Math.min(answer, inbound[i - k - 1] + outbound[i]); } if (answer >= INFTY) { answer = -1; } out.println(answer); } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; private FastReader.SpaceCharFilter filter; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public void nextIntArrays(int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = nextInt(); } } } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class ArrayUtils { public static void fill(long[] array, long value) { Arrays.fill(array, value); } public static void fill(int[] array, int value) { Arrays.fill(array, value); } public static int[] createArray(int count, int value) { int[] array = new int[count]; Arrays.fill(array, value); return array; } public static long[] createArray(int count, long value) { long[] array = new long[count]; Arrays.fill(array, value); return array; } } static class MiscUtils { public static void decreaseByOne(int[]... arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) { array[i]--; } } } } }
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 maxn = 1e5 + 5; struct node { long long a, b, c, d; }; node rec[maxn]; bool cmp(node x, node y) { return x.a < y.a; } struct node2 { long long num, cost; }; priority_queue<node2> q[maxn]; bool operator<(const node2 &x, const node2 &y) { return x.cost > y.cost; } long long vis[maxn], n, m, k; long long cost[maxn]; int main() { node2 flight; memset(vis, 0, sizeof(vis)); long long ans = 0x3f3f3f3f3f3f3f3f, tmp = 0; scanf("%I64d%I64d%I64d", &n, &m, &k); for (long long i = 1; i <= m; i++) scanf("%I64d%I64d%I64d%I64d", &rec[i].a, &rec[i].b, &rec[i].c, &rec[i].d); sort(rec + 1, rec + m + 1, cmp); long long i, j, tot1 = 0, tot2 = 0; for (long long k = 1; k <= m; k++) if (rec[k].b == 0) { flight.num = rec[k].a; flight.cost = rec[k].d; q[rec[k].c].push(flight); if (vis[rec[k].c] == 0) { tot2++; vis[rec[k].c] = 1; } } if (tot2 == n) for (long long k = 1; k <= n; k++) { tmp += q[k].top().cost; } for (i = 1, j = 1; i <= m; i++) { while (rec[j].a - rec[i].a < k + 1 && j <= m) { if (rec[j].b == 0) { long long x = rec[j].c; while (q[x].size() != 0 && q[x].top().num < rec[i].a + k + 1) { tmp -= q[x].top().cost; q[x].pop(); if (q[x].size() != 0) tmp += q[x].top().cost; } if (q[x].size() == 0) { tot2--; break; } } j++; } if (j > m || tot2 != n) break; if (rec[i].c == 0) { long long x = rec[i].b; if (cost[x] == 0) { cost[x] = rec[i].d; tot1++; tmp += cost[x]; } else if (rec[i].d < cost[x]) { tmp += rec[i].d - cost[x]; cost[x] = rec[i].d; } } if (tot1 == n && tot2 == n) ans = min(ans, tmp); } if (ans == 0x3f3f3f3f3f3f3f3f) 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
#include <bits/stdc++.h> using namespace std; struct node { int to, next, w; } ea[100010], eb[100010]; int n, m, k, t, x, y, z, st, ed, cnta, cntb, heada[1000010], headb[1000010], disa[100010], disb[100010]; long long ans = -1, a[1000010], b[1000010]; void adda() { ea[++cnta] = {x, heada[t], z}, heada[t] = cnta; } void addb() { eb[++cntb] = {y, headb[t], z}, headb[t] = cntb; } int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> t >> x >> y >> z; if (!x) addb(); else adda(); } cnta = cntb = 0; for (int i = 1; i <= 1000000; i++) { a[i] = a[i - 1]; for (int j = heada[i]; j; j = ea[j].next) { t = ea[j].to; if (!disa[t]) cnta++, disa[t] = ea[j].w, a[i] += disa[t]; else if (disa[t] > ea[j].w) a[i] += ea[j].w - disa[t], disa[t] = ea[j].w; } if (!st && cnta == n) st = i; } for (int i = 1000000; i >= 1; i--) { b[i] = b[i + 1]; for (int j = headb[i]; j; j = eb[j].next) { t = eb[j].to; if (!disb[t]) cntb++, disb[t] = eb[j].w, b[i] += disb[t]; else if (disb[t] > eb[j].w) b[i] += eb[j].w - disb[t], disb[t] = eb[j].w; } if (!ed && cntb == n) ed = i; } if (st == 0 || ed == 0) { cout << -1 << endl; return 0; } for (int i = 1; i <= 1000000; i++) { if (i + k + 1 > ed) break; if (i < st) continue; if (ans == -1) ans = a[i] + b[i + k + 1]; else ans = min(ans, a[i] + b[i + k + 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; template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> T lcm(T a, T b) { return a * b / gcd(a, b); } void printDouble(double x, int n) { cout << fixed << setprecision(n); cout << x << endl; } const int MAXN = 5e6 + 5; const int mod = 1e9 + 7; long long product() { return 1; } long long sum() { return 0; } template <typename T, typename... Args> T product(T a, Args... args) { return (a * product(args...)) % mod; } template <typename T, typename... Args> T sum(T a, Args... args) { return (a + sum(args...)) % mod; } struct flight { int city, day, cost; flight(int city, int day, int cost) : city(city), day(day), cost(cost) {} }; int city, nflight, k, x, y, z, t, firstDayGo(0), firstDayReturn(INT_MAX), lastDayGo(0), lastDayReturn(0), maxDay(0); vector<vector<flight> > v(2, vector<flight>()); vector<vector<long long> > f(2, vector<long long>(1e6 + 5, -1)); bool cmp(flight a, flight b) { return a.day < b.day; } void cal(int type) { vector<bool> check(city + 1, false); vector<long long> curMin(city + 1, LONG_LONG_MAX); bool first = true; long long cnt = city, cur = 0; for (auto i : v[type]) { if (!check[i.city]) cnt--, check[i.city] = true; if (cnt > 0) curMin[i.city] = min(curMin[i.city], i.cost * 1ll); if (!cnt && first) { if (type) lastDayReturn = i.day; else firstDayGo = i.day; f[type][i.day] = 0; first = false; curMin[i.city] = min(curMin[i.city], i.cost * 1ll); for (int j = 1; j <= city; j++) cur = f[type][i.day] += curMin[j]; continue; } if (!cnt && !first) { if (i.cost < curMin[i.city]) f[type][i.day] = cur + i.cost - curMin[i.city], curMin[i.city] = i.cost, cur = f[type][i.day]; else f[type][i.day] = cur; } } } int main() { std::ios::sync_with_stdio(false); cin >> city >> nflight >> k; for (int i = 1; i <= nflight; i++) { cin >> x >> y >> z >> t; maxDay = max(maxDay, x); v[y == 0].push_back(flight(y + z, x, t)); if (y == 0) firstDayReturn = min(firstDayReturn, x); if (y != 0) lastDayGo = max(lastDayGo, x); } sort(v[0].begin(), v[0].end(), cmp); sort(v[1].rbegin(), v[1].rend(), cmp); cal(0); cal(1); if (lastDayReturn == 0 || firstDayGo == 0) { cout << -1; return 0; } for (int i = lastDayReturn; i >= firstDayGo + 1; i--) if (f[1][i] < 0) f[1][i] = f[1][i + 1]; for (int i = firstDayGo; i <= maxDay; i++) if (f[0][i] < 0) f[0][i] = f[0][i - 1]; long long res = LONG_LONG_MAX; for (int i = firstDayGo; i <= maxDay; i++) { int endDay = i + k; if (endDay < lastDayReturn) res = min(res, f[0][i] + f[1][endDay + 1]); } if (res != LONG_LONG_MAX) cout << res; else cout << -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
import java.util.*; import java.io.*; import java.lang.Math.*; public class MainA { public static int mod = 20000; public static long[] val; public static long[] arr; static int max = (int) 1e9 + 7; public static void main(String[] args) { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=in.nextInt(); int m=in.nextInt(); int k=in.nextInt(); int first_coming_from[]=new int[n+1];Arrays.fill(first_coming_from,Integer.MAX_VALUE); int last_going_to[]=new int[n+1]; int len=(int)1e6+1; ArrayList<Pairs> arrival[]=new ArrayList[len]; ArrayList<Pairs> depart[]=new ArrayList[len]; for(int i=0;i<len;i++) { arrival[i]=new ArrayList(); depart[i]=new ArrayList(); } int lastday=-1; for(int i=0;i<m;i++) { int day=in.nextInt(); int dep=in.nextInt(); int arr=in.nextInt(); int cost=in.nextInt(); lastday=Math.max(day, lastday); if(dep==0) { depart[day].add(new Pairs(arr,cost)); if(last_going_to[arr]<day) last_going_to[arr]=day; } else { arrival[day].add(new Pairs(dep,cost)); if(first_coming_from[dep]>day) { first_coming_from[dep]=day; } } } int min=Integer.MAX_VALUE; int max=Integer.MIN_VALUE; for(int i=1;i<=n;i++) { if(last_going_to[i]==0 || first_coming_from[i]==0) { out.println(-1);out.close();return; } else { min=Math.min(min, last_going_to[i]); max=Math.max(max, first_coming_from[i]); } } for(int i=1;i<=n;i++) { if(min-first_coming_from[i]-1<k) { out.println(-1);out.close();return; } } //out.println(max+" "+min); long inflights[]=new long[len]; int set_in[]=new int[n+1]; Arrays.fill(set_in,Integer.MAX_VALUE); for(int i=1;i<min-k;i++) { inflights[i]=inflights[i-1]; for(int j=0;j<arrival[i].size();j++) { if(set_in[arrival[i].get(j).x]>arrival[i].get(j).y) { if(set_in[arrival[i].get(j).x]==Integer.MAX_VALUE) inflights[i]+=(long)arrival[i].get(j).y; else inflights[i]+=-set_in[arrival[i].get(j).x]+(long)arrival[i].get(j).y; set_in[arrival[i].get(j).x]=arrival[i].get(j).y; } } } long outflights[]=new long[len]; int set_out[]=new int[n+1]; Arrays.fill(set_out,Integer.MAX_VALUE); for(int i=lastday;i>=max+k+1;i--) { outflights[i]=outflights[i+1]; for(int j=0;j<depart[i].size();j++) { if(set_out[depart[i].get(j).x]>depart[i].get(j).y) { if(set_out[depart[i].get(j).x]==Integer.MAX_VALUE) outflights[i]+=(long)depart[i].get(j).y; else outflights[i]+=-set_out[depart[i].get(j).x]+(long)depart[i].get(j).y; set_out[depart[i].get(j).x]=depart[i].get(j).y; } } } /*for(int i=lastday;i>=max+k+1;i--) { out.print(outflights[i]+" "); for(int j=1;j<=n;j++) { out.print(set_out[j]+" "); } out.println(); }*/ long ans=Long.MAX_VALUE; for(int i=max+1;i<=min-k;i++) { ans=Math.min(ans, inflights[i-1]+outflights[i+k]); } out.println(ans); out.close(); } static class Pairs implements Comparable<Pairs> { int x; int y; Pairs(int a, int b) { x = a; y = b; } @Override public int compareTo(Pairs o) { // TODO Auto-generated method stub if (x == o.x) return Integer.compare(y, o.y); else return Long.compare(-x, -o.x); } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public 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; } public static String rev(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return sb.toString(); } public static long gcd(long x, long y) { if (y == 0) return x; if (x % y == 0) return y; else return gcd(y, x % y); } public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); } public 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; } public static long max(long a, long b) { if (a > b) return a; else return b; } public static long min(long a, long b) { if (a > b) return b; else return a; } public static long[][] mul(long a[][], long b[][]) { long c[][] = new long[2][2]; c[0][0] = a[0][0] * b[0][0] + a[0][1] * b[1][0]; c[0][1] = a[0][0] * b[0][1] + a[0][1] * b[1][1]; c[1][0] = a[1][0] * b[0][0] + a[1][1] * b[1][0]; c[1][1] = a[1][0] * b[0][1] + a[1][1] * b[1][1]; return c; } public static long[][] pow(long n[][], long p, long m) { long result[][] = new long[2][2]; result[0][0] = 1; result[0][1] = 0; result[1][0] = 0; result[1][1] = 1; if (p == 0) return result; if (p == 1) return n; while (p != 0) { if (p % 2 == 1) result = mul(result, n); //System.out.println(result[0][0]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (result[i][j] >= m) result[i][j] %= m; } } p >>= 1; n = mul(n, n); // System.out.println(1+" "+n[0][0]+" "+n[0][1]+" "+n[1][0]+" "+n[1][1]); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (n[i][j] >= m) n[i][j] %= m; } } } return result; } public static long pow(long n, long p) { long result = 1; if (p == 0) return 1; if (p == 1) return n; while (p != 0) { if (p % 2 == 1) result *= n; p >>= 1; n *= n; } return result; } 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] = nextInt(); } 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 Flight { int day, depCity, arrCity, cost; }; int bestArrCost[100002LL], bestDepCost[100002LL]; vector<pair<long long, int>> bestDep; vector<Flight> arrivals, departures; Flight f; int n, m, k; long long ans = 100002LL * 1000002LL; int main() { cin >> n >> m >> k; for (int i = 1; i <= m; i++) { cin >> f.day >> f.depCity >> f.arrCity >> f.cost; if (f.arrCity == 0) arrivals.push_back(f); else departures.push_back(f); } sort(arrivals.begin(), arrivals.end(), [&](Flight f1, Flight f2) { return f1.day < f2.day; }); sort(departures.begin(), departures.end(), [&](Flight f1, Flight f2) { return f1.day > f2.day; }); for (int i = 1; i <= n; i++) bestArrCost[i] = bestDepCost[i] = 1000002LL; int nr = n; long long totalBest = 1000002LL * n; for (auto flight : departures) { if (bestDepCost[flight.arrCity] == 1000002LL) nr--; totalBest -= bestDepCost[flight.arrCity]; bestDepCost[flight.arrCity] = min(bestDepCost[flight.arrCity], flight.cost); totalBest += bestDepCost[flight.arrCity]; if (nr == 0 && (bestDep.empty() || totalBest != bestDep.back().first)) bestDep.push_back(make_pair(totalBest, flight.day)); } nr = n, totalBest = 1000002LL * n; for (auto flight : arrivals) { if (bestArrCost[flight.depCity] == 1000002LL) nr--; totalBest -= bestArrCost[flight.depCity]; bestArrCost[flight.depCity] = min(bestArrCost[flight.depCity], flight.cost); totalBest += bestArrCost[flight.depCity]; if (nr == 0) { while (!bestDep.empty() && flight.day + k + 1 > bestDep.back().second) bestDep.pop_back(); if (bestDep.empty()) break; ans = min(ans, totalBest + bestDep.back().first); } } if (ans == 1000002LL * 100002LL) cout << -1; else cout << ans; cin >> 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
/* Keep solving problems. */ import java.util.*; import java.io.*; public class CFA { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; private static final long MOD = 1000 * 1000 * 1000 + 7; private static final int[] dx = {0, -1, 0, 1}; private static final int[] dy = {1, 0, -1, 0}; private static final String yes = "Yes"; private static final String no = "No"; class Node implements Comparable<Node> { int day; long cost; int city; public Node(int day, long cost, int city) { this.day = day; this.cost = cost; this.city = city; } @Override public int compareTo(Node o) { return Integer.compare(day, o.day); } } void solve() throws IOException { int n = nextInt(); int m = nextInt(); int k = nextInt(); List<Node> come = new ArrayList<>(); List<Node> leave = new ArrayList<>(); for (int i = 0; i < m; i++) { int day = nextInt(); int f = nextInt(); int t = nextInt(); long cost = nextInt(); if (f == 0) { leave.add(new Node(day, cost, t)); } else { come.add(new Node(day, cost, f)); } } Collections.sort(come); Collections.sort(leave); int limit = 1000000 + 100; long[] pref = new long[limit]; long sum = 0; int cnt = 0; long[] min = new long[limit]; Arrays.fill(min, Long.MAX_VALUE); Arrays.fill(pref, Long.MAX_VALUE); for (int i = 0; i < come.size(); i++) { Node cur = come.get(i); if (min[cur.city] == Long.MAX_VALUE) { min[cur.city] = cur.cost; sum += cur.cost; cnt++; } else { sum -= min[cur.city]; min[cur.city] = Math.min(min[cur.city], cur.cost); sum += min[cur.city]; } if (cnt == n) { pref[cur.day] = sum; } } long[] suff = new long[limit]; sum = 0; cnt = 0; min = new long[limit]; Arrays.fill(min, Long.MAX_VALUE); Arrays.fill(suff, Long.MAX_VALUE); for (int i = leave.size() - 1; i >= 0; i--) { Node cur = leave.get(i); if (min[cur.city] == Long.MAX_VALUE) { min[cur.city] = cur.cost; sum += cur.cost; cnt++; } else { sum -= min[cur.city]; min[cur.city] = Math.min(min[cur.city], cur.cost); sum += min[cur.city]; } if (cnt == n) { suff[cur.day] = sum; } } for (int i = 1; i < limit; i++) { pref[i] = Math.min(pref[i], pref[i - 1]); } for (int i = limit - 2; i >= 0; i--) { suff[i] = Math.min(suff[i], suff[i + 1]); } long res = Long.MAX_VALUE; for (int i = 0; i < limit; i++) { int j = i + k + 1; if (j < limit) { if (pref[i] != Long.MAX_VALUE && suff[j] != Long.MAX_VALUE) { res = Math.min(res, pref[i] + suff[j]); } } } outln(res == Long.MAX_VALUE ? -1 : res); } void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int) (Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } long gcd(long a, long b) { while(a != 0 && b != 0) { long c = b; b = a % b; a = c; } return a + b; } private void outln(Object o) { out.println(o); } private void out(Object o) { out.print(o); } private void formatPrint(double val) { System.out.format("%.9f%n", val); } public CFA() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new CFA(); } public long[] nextLongArr(int n) throws IOException{ long[] res = new long[n]; for(int i = 0; i < n; i++) res[i] = nextLong(); return res; } public int[] nextIntArr(int n) throws IOException { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } public String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { 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; int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m, i, j, k, a, b, day, cost, ans, MAXX = 1000010, INF = 1LL << 60, INFF = 1E9, s, ct, x; cin >> n >> m >> k; vector<vector<pair<long long, long long>>> in(MAXX), out(MAXX); for (i = 0; i < m; i++) { cin >> day >> a >> b >> cost; if (a == 0) out[day].push_back({b, cost}); if (b == 0) in[day].push_back({a, cost}); } vector<long long> c(n + 1), sl(MAXX, INF), sr(MAXX, INF); c.assign(n + 1, INFF); ct = 0; s = INFF * n; for (i = 1; i < MAXX; i++) { for (auto p : in[i]) { if (c[p.first] == INFF) ct++; s -= c[p.first]; c[p.first] = min(c[p.first], p.second); s += c[p.first]; } if (ct == n) sl[i] = s; } c.assign(n + 1, INFF); ct = 0; s = INFF * n; for (i = MAXX - 1; i >= 1; i--) { for (auto p : out[i]) { if (c[p.first] == INFF) ct++; s -= c[p.first]; c[p.first] = min(c[p.first], p.second); s += c[p.first]; } if (ct == n) sr[i] = s; } ans = INF; for (i = 2; i + k < MAXX; i++) { x = sl[i - 1] + sr[i + k]; ans = min(ans, x); } if (ans == INF) ans = -1; 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; template <typename _T> inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; } template <typename _T, typename... args> void _DBG(const char *s, _T x, args... a) { while (*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); } const int N = 1e6 + 7; const long long INF = 1e13 + 7; int n, m, k; long long mn[N], pre[N], suf[N]; vector<pair<int, int> > G[2][N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; int M = 0; for (int i = 0; i < m; i++) { int d, a, b, c; cin >> d >> a >> b >> c; if (b == 0) { G[0][d].push_back({a, c}); } else { G[1][d].push_back({b, c}); } M = max(M, d); } for (int i = 1; i <= n; i++) mn[i] = INF; long long cost = (long long)n * INF; for (int i = 1; i <= M; i++) { for (auto x : G[0][i]) { if (x.second < mn[x.first]) cost += x.second - mn[x.first], mn[x.first] = x.second; } pre[i] = cost; } for (int i = 1; i <= n; i++) mn[i] = INF; cost = (long long)n * INF; for (int i = M; i; i--) { for (auto x : G[1][i]) { if (x.second < mn[x.first]) cost += x.second - mn[x.first], mn[x.first] = x.second; } suf[i] = cost; } long long res = (long long)n * INF; for (int i = 1; i + k + 1 <= M; i++) res = min(res, pre[i] + suf[i + k + 1]); if (res >= INF) cout << "-1\n"; else cout << res << "\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; int OO = 1000000000; int a[100005], l[100005], f[100005], t[100005], c[100005], ta[1000005]; long long dp[1000005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k, tl = 0, it = 0, te; cin >> n >> m >> k; vector<pair<int, int>> day; for (int i = 0; i < m; i++) { cin >> te >> f[i] >> t[i] >> c[i]; day.push_back(make_pair(te, i)); } sort(day.begin(), day.end()); for (int i = 1; i <= 1000000; i++) { dp[i] = dp[i - 1]; ta[i] = ta[i - 1]; while (it < day.size() && day[it].first == i) { int d = day[it++].second; if (f[d] && (a[f[d]] == 0 || a[f[d]] > c[d])) { if (a[f[d]] == 0) { ta[i]++; dp[i] += c[d]; } else { dp[i] += c[d] - a[f[d]]; } a[f[d]] = c[d]; } } } long long r = 0, ans = 10000000000000000; it = day.size() - 1; for (int i = 1000000; i >= k + 2; i--) { while (it >= 0 && day[it].first == i) { int d = day[it--].second; if (t[d] && (l[t[d]] == 0 || l[t[d]] > c[d])) { if (l[t[d]] == 0) { tl++; r += c[d]; } else { r += c[d] - l[t[d]]; } l[t[d]] = c[d]; } } if (tl == n && ta[i - k - 1] == n) ans = min(ans, r + dp[i - k - 1]); } if (ans >= 10000000000000000) 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; using lli = long long int; using ld = long double; const int N = 1e6 + 1; const lli MOD = 998244353; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, k; cin >> n >> m >> k; int d[N]; vector<pair<int, pair<int, int> > > arr, depp; for (int i = 0; i < m; i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (f == 0) { depp.push_back(make_pair(d, make_pair(t, c))); } else { arr.push_back(make_pair(d, make_pair(f, c))); } } lli dep[N]; for (int i = 0; i < N; i++) { dep[i] = 1e18; } sort(arr.begin(), arr.end()); sort(depp.rbegin(), depp.rend()); int depcost[n + 1]; int arrcost[n + 1]; for (int i = 1; i <= n; i++) { depcost[i] = 1e9; arrcost[i] = 1e9; } int depx = 0; lli totaldep = 0; int arrx = 0; for (auto it : depp) { int d = it.first; int city = it.second.first; int cost = it.second.second; if (depcost[city] == 1e9) { depx++; totaldep += cost; depcost[city] = cost; } else if (cost < depcost[city]) { totaldep = totaldep - depcost[city] + cost; depcost[city] = cost; } if (depx == n) { dep[d] = min(dep[d], totaldep); } } for (int i = N - 2; i > 0; i--) { dep[i] = min(dep[i + 1], dep[i]); } lli totalarr = 0; lli ans = 1e18; for (auto it : arr) { int d = it.first; int city = it.second.first; int cost = it.second.second; if (arrcost[city] == 1e9) { arrx++; totalarr += cost; arrcost[city] = cost; } else if (cost < arrcost[city]) { totalarr = totalarr - arrcost[city] + cost; arrcost[city] = cost; } if (arrx == n) { if (d + k + 1 < N) { if (dep[d + k + 1] != 1e18) { ans = min(ans, totalarr + dep[d + k + 1]); } } } } if (ans == 1e18) { ans = -1; } 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 Node { long long d, from, to, val; } a[250000]; long long vis[250000], l[250000], r[250000]; inline long long cmp(Node p, Node q) { return p.d < q.d; } int main() { long long n, m, k; scanf("%I64d%I64d%I64d", &n, &m, &k); memset(vis, 0, sizeof vis); memset(l, -1, sizeof l); memset(r, -1, sizeof r); for (int i = 1; i <= m; ++i) scanf("%I64d%I64d%I64d%I64d", &a[i].d, &a[i].from, &a[i].to, &a[i].val); sort(a + 1, a + m + 1, cmp); long long sum = 0, tot = 0; for (int i = 1; i <= m; ++i) { if (a[i].from == 0) continue; if (vis[a[i].from] == 0) tot += a[i].val, ++sum, vis[a[i].from] = a[i].val; else if (vis[a[i].from] > a[i].val) tot -= vis[a[i].from], tot += a[i].val, vis[a[i].from] = a[i].val; if (sum == n) l[i] = tot; } sum = 0, tot = 0; memset(vis, 0, sizeof vis); for (int i = m; i >= 1; --i) { if (a[i].from != 0) continue; if (vis[a[i].to] == 0) tot += a[i].val, ++sum, vis[a[i].to] = a[i].val; else if (vis[a[i].to] > a[i].val) tot -= vis[a[i].to], tot += a[i].val, vis[a[i].to] = a[i].val; if (sum == n) r[i] = tot; } long long Ans = 9000000000000; for (int i = m; i >= 1; i--) if (r[i] == -1) continue; else 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; else { long long pos = -1, day = a[i].d + k + 1, ll = i + 1, rr = m; while (rr - ll >= 0) { long long Mid = rr + ll >> 1; if (a[Mid].d >= day) rr = Mid - 1, pos = Mid; else ll = Mid + 1; } if (pos == -1) continue; else if (r[pos] != -1) Ans = min(Ans, r[pos] + l[i]); } } if (Ans == 9000000000000) 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
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } const int MAXN = 2123456; const long long INF = 300000000000LL; int n, m, k; long long s[MAXN]; multiset<long long> bef[MAXN], aft[MAXN]; int ind[MAXN]; int d[MAXN], ini[MAXN], fim[MAXN]; vector<pair<int, long long> > in[MAXN], out[MAXN]; bool cmp(int a, int b) { return d[a] < d[b]; } long long sum; void add(int i, int j, long long x) { if (i == 0) { sum -= *aft[j].begin(); aft[j].insert(x); sum += *aft[j].begin(); } else { sum -= *bef[i].begin(); bef[i].insert(x); sum += *bef[i].begin(); } } void rmv(int i, int j, long long x) { if (i == 0) { sum -= *aft[j].begin(); aft[j].erase(aft[j].find(x)); sum += *aft[j].begin(); } else { sum -= *bef[i].begin(); bef[i].erase(bef[i].find(x)); sum += *bef[i].begin(); } } int main() { scanf("%d%d%d", &n, &m, &k); for (int a = 1; a <= n; a++) { bef[a].insert(INF); aft[a].insert(INF); sum += 2ll * INF; } for (int a = 0; a < m; a++) { scanf("%d%d%d%lld", &d[a], ini + a, fim + a, s + a); if (ini[a] == 0) out[d[a]].push_back(pair<int, long long>(fim[a], s[a])); else in[d[a]].push_back(pair<int, long long>(ini[a], s[a])); } for (int dia = 1000000; dia >= k; dia--) { for (int i = 0; i < out[dia].size(); i++) { pair<int, long long> voo = out[dia][i]; add(0, voo.first, voo.second); } } long long res = LLONG_MAX; for (int dia = 0; dia <= 1000000 - k; dia++) { res = min(res, sum); for (pair<int, long long> voo : in[dia]) add(voo.first, 0, voo.second); for (pair<int, long long> voo : out[dia + k]) rmv(0, voo.first, voo.second); } if (res >= INF) puts("-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
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; public class Main{ class MyScanner{ BufferedReader br; StringTokenizer token; public MyScanner(InputStream input){ this.br = new BufferedReader(new InputStreamReader(input)); token = new StringTokenizer(""); } public String next() throws Exception{ if(token.hasMoreTokens()) return token.nextToken(); else{ token = new StringTokenizer(br.readLine()); } return next(); } public int nextInt() throws Exception{ return Integer.parseInt(next()); } public long nextLong() throws Exception{ return Long.parseLong(next()); } public double nextDouble() throws Exception{ return Double.parseDouble(next()); } } class Flight implements Comparable<Flight> { int day, departure, arrival; long cost; public Flight(int day, int departure, int arrival, long cost) { this.day = day; this.departure = departure; this.arrival = arrival; this.cost = cost; } public boolean isDepartingOn(int day) { return this.day == day; } public boolean isDepartingFrom(int departure) { return this.departure == departure; } public boolean isArrivingAt(int arrival) { return this.arrival == arrival; } @Override public int compareTo(Flight other) { return Long.compare(this.day, other.day); } } private boolean debug = false; public void debugPrint(String s){ if(debug) System.out.print(s); } public void debugPrintln(String s){ if(debug) System.out.println(s); } public void run(){ MyScanner sc = new MyScanner(System.in); try { int days = 1000000; //Total possible days int n = sc.nextInt(); //Total juries int m = sc.nextInt(); //Total tickets int k = sc.nextInt(); //Total duration of meeting debugPrintln("Total Juries: " + n + ", Tickets: " + m + ", Duration: " + k); Flight[] tickets = new Flight[m]; //Tickets ArrayList<Flight>[] juryArrivalTickets = new ArrayList[n+1]; //Arrival Tickt list for each jury ArrayList<Flight>[] juryDepartureTickets = new ArrayList[n+1]; //Departure Tickt list for each jury for(int i=0; i < juryArrivalTickets.length; i++){//For each jury juryArrivalTickets[i] = new ArrayList<>(); //Initialize Ticket array juryDepartureTickets[i] = new ArrayList<>(); //Intitialize ticket array } debugPrintln("Receiving tickets as input: "); for(int i=0; i < m; i++){ //For each ticket int day = sc.nextInt(); //Get day of deprture int dep = sc.nextInt(); //Get place of departure int arr = sc.nextInt(); //Get place of arrival int cost = sc.nextInt(); //Get cost of ticket tickets[i] = new Flight(day, dep, arr, cost); debugPrintln(" Added ticket " + (i+1) + " to the list - Day: " + day + ", departure: " + dep + ", arrival: " + arr + ", cost: " + cost); } Arrays.sort(tickets); //Sort tickets according to ascending order of day for(int i=0; i < tickets.length; i++){ //For each ticket Flight t = tickets[i]; if(tickets[i].isArrivingAt(0)){//If ticket arrives at capital debugPrintln(" Adding ticket " + i + " to arrival list of " + tickets[i].departure); juryArrivalTickets[tickets[i].departure].add(t); //Add ticket to that jury's list of arrival tickets } else{ debugPrintln(" Adding ticket " + i + " to departure list of " + tickets[i].arrival); juryDepartureTickets[tickets[i].arrival].add(t); //Add ticket to that jury's list of departure tickets } } int lastArrivalDay = -1; //Last possible day of arrival int firstDepartureDay = Integer.MAX_VALUE; //Earliest possible day for departure for(int i=1; i < juryArrivalTickets.length; i++){//For each jury's arriving tickets if(juryArrivalTickets[i].isEmpty() || juryDepartureTickets[i].isEmpty()){//If the jury has no arrival tickets debugPrintln("Arrival or departure ticket for jury " + i + " not found"); System.out.println(-1);//Return Minimum cost not found return; } lastArrivalDay = Math.max(lastArrivalDay, juryArrivalTickets[i].get(0).day); //Store the last possible arrival day firstDepartureDay = Math.min(firstDepartureDay, juryDepartureTickets[i].get(juryDepartureTickets[i].size() - 1).day); //Store the first possible departure day } debugPrintln("Last arrival day: " + lastArrivalDay + ", Earliest Departure day: " + firstDepartureDay); long[] minTicketCost = new long[n+1]; //Minimum ticket cost for each jury long[] minArrivalCost = new long[days+1]; //Minimum arrival Ticket cost upto each day of the array long[] minDepartureCost = new long[days+2]; //Minimum departure Ticket cost upto each day of the array int ticket = 0; for(int day=1; day <= days; day++){//For each day minArrivalCost[day] = minArrivalCost[day-1]; //Minimum cost of arrival upto this day is same as previous day // debugPrintln("Current min arrival cost for day " + day + " is: " + minArrivalCost[day]); while(ticket < tickets.length && tickets[ticket].day == day){//For each ticket of this day debugPrintln(" Processing ticket: " + (ticket+1) + " for day " + tickets[ticket].day + " and arriving at: " + tickets[ticket].arrival); if(!tickets[ticket].isArrivingAt(0)){//If the ticket is not for arriving at the meeting debugPrintln(" Ticket not arriving at meeting"); ticket++; continue; //Skip the rest } Flight f = tickets[ticket]; debugPrintln(" Calculating minimum ticket cost for jury " + f.departure + ".. Current min cost: " + minTicketCost[f.departure] + " and this ticket cost " + f.cost); if(minTicketCost[f.departure] == 0 || f.cost < minTicketCost[f.departure]){//If minimum cost for the jury has not been added or is minimum than current min debugPrintln(" Modifiying ticket cost for jury " + f.departure + ".. current min arrival cost: " + minArrivalCost[day]); debugPrintln(" Adding cost " + f.cost); minArrivalCost[day] += f.cost; //Add this cost to minimum cost until this day debugPrintln(" Subtracting cost " + minTicketCost[f.departure]); minArrivalCost[day] -= minTicketCost[f.departure]; //Subtract previous minimum cost debugPrintln(" Updated min arrival cost " + minArrivalCost[day]); minTicketCost[f.departure] = f.cost; //Update current minimum cost for that jury } ticket++; } } minTicketCost = new long[n+1]; //Minimum ticket cost for each jury ticket = tickets.length - 1; debugPrintln("Calculating minimum departure cost for " + (ticket+1) + " tickets and days " + days); for(int day=days; day >= 0; day--){//For each day in descending order //debugPrintln("Day " + day + " prev cost: " + minDepartureCost[day+1]); minDepartureCost[day] = minDepartureCost[day+1]; //Minimum cost of departure upto this day is same as later day //debugPrintln("Current min departure cost for day " + day + " is: " + minDepartureCost[day]); while(ticket >= 0 && tickets[ticket].day == day){//For each ticket of this day debugPrintln(" Processing ticket: " + (ticket+1) + " for day " + tickets[ticket].day + " and departing from : " + tickets[ticket].departure); if(tickets[ticket].isArrivingAt(0)){//Is the ticket is for arriving at the meeting ticket--; continue; //Skip the rest of loop } Flight f = tickets[ticket]; if(minTicketCost[f.arrival] == 0 || f.cost < minTicketCost[f.arrival]){//If minimum cost for the jury has not been added or is minimum than current min minDepartureCost[day] += f.cost; //Add this cost to minimum cost until this day minDepartureCost[day] -= minTicketCost[f.arrival]; //Subtract previous minimum cost minTicketCost[f.arrival] = f.cost; } ticket--; } } if(firstDepartureDay - lastArrivalDay <= k){//If minimum possible duration of working day is less than specified debugPrintln("Duration of minimum working day: " + (firstDepartureDay - lastArrivalDay) + " and specified: " + k); System.out.println(-1); //Minimum cost cannot be found return; } long minCost = Long.MAX_VALUE; for(int i=lastArrivalDay; i+k+1 <= firstDepartureDay; i++){//For each day and work duration minCost = Math.min(minCost, minArrivalCost[i] + minDepartureCost[i+k+1]); //Calculate minimum cost } debugPrint("Minimum cost found: "); System.out.println(minCost); } catch (Exception e) { debugPrintln(e.getMessage()); System.out.println(-1); } } public static void main(String[] args){ new Main().run(); } }
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 gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long m, long long n) { return (m * n / gcd(m, n)); } vector<pair<long long, pair<long long, long long> > > A; vector<pair<pair<long long, long long>, pair<long long, long long> > > B; vector<pair<pair<long long, long long>, pair<long long, long long> > >::iterator T; long long Cf[100001]; long long Ct[100001]; set<long long> S; long long Sum; int main() { ios::sync_with_stdio(false); long long n, m, k, d, f, t, c; cin >> n >> m >> k; for (int i = 0; i < (int)m; i++) { cin >> d >> f >> t >> c; if (t == 0) { A.push_back(pair<long long, pair<long long, long long> >( d, pair<long long, long long>(f, c))); } else { B.push_back(pair<pair<long long, long long>, pair<long long, long long> >( pair<long long, long long>(d, 0), pair<long long, long long>(t, c))); } } sort(A.begin(), A.end()); sort(B.begin(), B.end()); for (int i = B.size() - 1; i >= 0; i--) { S.insert(B[i].second.first); if (Ct[B[i].second.first] == 0) { Sum += B[i].second.second; Ct[B[i].second.first] = B[i].second.second; } else { if (Ct[B[i].second.first] > B[i].second.second) { Sum -= Ct[B[i].second.first]; Sum += B[i].second.second; Ct[B[i].second.first] = B[i].second.second; } } if (S.size() == n) { B[i].first.second = true; B[i].second.second = Sum; } } S.clear(); Sum = 0; long long Res = 1e18; for (int i = 0; i < A.size(); i++) { S.insert(A[i].second.first); if (Cf[A[i].second.first] == 0) { Cf[A[i].second.first] = A[i].second.second; Sum += A[i].second.second; } else { if (Cf[A[i].second.first] > A[i].second.second) { Sum -= Cf[A[i].second.first]; Sum += A[i].second.second; Cf[A[i].second.first] = A[i].second.second; } } if (S.size() == n) { T = lower_bound( B.begin(), B.end(), pair<pair<long long, long long>, pair<long long, long long> >( pair<long long, long long>(A[i].first + k + 1, 0), pair<long long, long long>(0, 0))); if (T != B.end()) { if ((*T).first.second) { Res = min(Res, Sum + (*T).second.second); } } } } (Res == 1e18) ? cout << -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 n, m, k, t1, t2, tim; vector<int> type[2000010], a[2000010], b[2000010]; long long f[2000010], g[2000010]; bool ff[2000010], gg[2000010]; int t[2000010], tot; long long ans = 1e18; int main() { scanf("%d%d%d", &n, &m, &k); int x, y, z, w; for (int i = 1; i <= m; i++) { scanf("%d%d%d%d", &x, &y, &z, &w); tim = max(tim, x); if (z == 0) { type[x].push_back(1); a[x].push_back(y); b[x].push_back(w); } else { type[x].push_back(-1); a[x].push_back(z); b[x].push_back(w); } } tot = n; f[0] = 0; memset(t, 127, sizeof(t)); for (int i = 1; i <= tim; i++) { f[i] = f[i - 1]; for (int j = 0; j < a[i].size(); j++) { if (type[i][j] == 1) { if (t[a[i][j]] > 1e7) { tot--; t[a[i][j]] = b[i][j]; f[i] += b[i][j]; } else if (t[a[i][j]] > b[i][j]) { f[i] += b[i][j] - t[a[i][j]]; t[a[i][j]] = b[i][j]; } } } ff[i] = (tot == 0); } tot = n; g[tim + 1] = 0; memset(t, 127, sizeof(t)); for (int i = tim; i; i--) { g[i] = g[i + 1]; for (int j = 0; j < a[i].size(); j++) { if (type[i][j] == -1) { if (t[a[i][j]] > 1e7) { tot--; t[a[i][j]] = b[i][j]; g[i] += b[i][j]; } else if (t[a[i][j]] > b[i][j]) { g[i] += b[i][j] - t[a[i][j]]; t[a[i][j]] = b[i][j]; } } } gg[i] = (tot == 0); } for (int i = 1; i <= tim; i++) if (ff[i] && gg[i + k + 1]) ans = min(ans, f[i] + g[i + k + 1]); if (ans < 1e18) printf("%lld\n", ans); else puts("-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 int MOD = (int)1e9 + 7; const int MOD2 = 1007681537; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } inline long long isqrt(long long k) { long long r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline long long icbrt(long long k) { long long r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } inline int sign(long double x) { return x < -EPS ? -1 : x > +EPS; } inline int sign(long double x, long double y) { return sign(x - y); } const int maxn = 1e6 + 5; int n, m, k; int d[maxn]; int f[maxn]; int t[maxn]; int c[maxn]; long long mn[maxn]; long long tot; long long prf[maxn]; long long suf[maxn]; void solve() { scanf("%d%d%d", &n, &m, &k); vector<pair<pair<int, int>, pair<int, int> > > v; for (int i = (0); i < (m); i++) { scanf("%d%d%d%d", d + i, f + i, t + i, c + i); v.push_back(make_pair(make_pair(d[i], f[i]), make_pair(t[i], c[i]))); } sort((v).begin(), (v).end()); for (int i = (0); i < (m); i++) { d[i] = v[i].first.first; f[i] = v[i].first.second; t[i] = v[i].second.first; c[i] = v[i].second.second; } fill_n(mn, n + 1, (long long)1e12); tot = accumulate(mn + 1, mn + n + 1, 0LL); int ptr = 0; for (int i = (0); i < (maxn); i++) { prf[i] = tot; while (ptr < m && d[ptr] <= i) { if (!t[ptr]) { if (mn[f[ptr]] > c[ptr]) { tot -= mn[f[ptr]]; mn[f[ptr]] = c[ptr]; tot += mn[f[ptr]]; } } ptr++; } } fill_n(mn, n + 1, (long long)1e12); tot = accumulate(mn + 1, mn + n + 1, 0LL); ptr = m - 1; for (int i = (maxn)-1; i >= (0); i--) { suf[i] = tot; while (ptr >= 0 && d[ptr] >= i) { if (!f[ptr]) { if (mn[t[ptr]] > c[ptr]) { tot -= mn[t[ptr]]; mn[t[ptr]] = c[ptr]; tot += mn[t[ptr]]; } } ptr--; } } long long ans = (long long)1e12; for (int i = (0); i < (maxn - k + 1); i++) { chkmin(ans, prf[i] + suf[i + k - 1]); } if (ans == (long long)1e12) { ans = -1; } cout << ans << "\n"; } int main() { int JUDGE_ONLINE = 1; if (fopen("in.txt", "r")) { JUDGE_ONLINE = 0; assert(freopen("in.txt", "r", stdin)); } else { } solve(); if (!JUDGE_ONLINE) { } 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 + 3; long long inf = 1e18 + 7; struct F { int day, des, cost; F(){}; F(int _d, int _des, int _c) : day(_d), des(_des), cost(_c){}; } arr[N], leav[N]; int m, m1 = 0, m2 = 0, n, k; long long ans = inf; bool used[N]; struct cmps { bool operator()(F a, F b) { return a.cost < b.cost; } }; bool cmp(F a, F b) { return a.day < b.day; } set<F, cmps> sarr[N], sleav[N]; bool canArrive[N]; int main() { memset(used, 0, sizeof(used)); memset(canArrive, 0, sizeof(canArrive)); scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; ++i) { int d, u, v, c; scanf("%d%d%d%d", &d, &u, &v, &c); if (u < v) { leav[m2++] = F(d, v, c); } else { arr[m1++] = F(d, u, c); } } sort(leav, leav + m2, cmp); sort(arr, arr + m1, cmp); long long sum = 0; int t1 = 0, t2 = 0, cnt = 0; while (t1 < m1 && cnt < n) { if (!canArrive[arr[t1].des]) { canArrive[arr[t1].des] = true; ++cnt; } if (sarr[arr[t1].des].empty()) { sarr[arr[t1].des].insert(arr[t1]); sum += 1LL * arr[t1].cost; } else { auto it = sarr[arr[t1].des].begin(); if (arr[t1].cost < it->cost) { sum -= it->cost; sarr[arr[t1].des].insert(arr[t1]); sum += arr[t1].cost; } } ++t1; } if (cnt < n) { printf("-1"); return 0; } int last = arr[t1 - 1].day + k + 1; while (t2 < m2 && leav[t2].day < last) ++t2; for (int i = t2; i < m2; ++i) { if (sleav[leav[i].des].empty()) { sleav[leav[i].des].insert(leav[i]); sum += 1LL * leav[i].cost; } else { auto it = sleav[leav[i].des].begin(); if (leav[i].cost < (it->cost)) { sum -= it->cost; sum += leav[i].cost; } } sleav[leav[i].des].insert(leav[i]); } for (int i = 1; i <= n; ++i) { if (sleav[i].empty()) { printf("-1"); return 0; } } ans = sum; bool done = false; for (int i = t1; i < m1; ++i) { last = arr[i].day + k + 1; while ((!done) && t2 < m2 && leav[t2].day < last) { auto it = sleav[leav[t2].des].find( F(leav[t2].day, leav[t2].des, leav[t2].cost)); if (it == sleav[leav[t2].des].begin()) { sum -= it->cost; sleav[leav[t2].des].erase(it); if (sleav[leav[t2].des].empty()) { done = true; } else { it = sleav[leav[t2].des].begin(); sum += 1LL * (it->cost); } } else { sleav[leav[t2].des].erase(it); if (sleav[leav[t2].des].empty()) { done = true; } } ++t2; } if (done) break; auto it2 = sarr[arr[i].des].begin(); if (arr[i].cost < it2->cost) { sum -= it2->cost; sum += arr[i].cost; sarr[arr[i].des].insert(arr[i]); } ans = min(ans, sum); if (done) break; } if (ans == inf) printf("-1"); else printf("%I64d", 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 = 100100; const int M = 1001000; const int OO = 1e7; const long long OOLL = 1e14; int n, m, k; multiset<int> st[N]; vector<pair<int, int>> a[M], b[M]; int pr[N]; long long sum; int cnt; bool u[N]; void add_from(int idx, int price) { cnt += !u[idx]; u[idx] = true; sum -= pr[idx]; pr[idx] = min(price, pr[idx]); sum += pr[idx]; } void rmv(int idx, int val) { sum -= *st[idx].begin(); --cnt; st[idx].erase(st[idx].find(val)); if (st[idx].empty()) sum = OOLL; else { ++cnt; sum += *st[idx].begin(); } } int main() { scanf("%d%d%d", &n, &m, &k); ++n; for (int i = 1; i < n; ++i) { pr[i] = OO; sum += OO; } for (int i = 0; i < m; ++i) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (f > 0) a[d].emplace_back(c, f); else { b[d].emplace_back(c, t); if (d > k) st[t].insert(c); } } bool f = true; for (int i = 1; i < n; ++i) { f &= !st[i].empty(); if (!st[i].empty()) sum += *st[i].begin(); } long long minSum = OOLL; for (int i = k + 1; i < M; ++i) { for (auto p : a[i - k]) add_from(p.second, p.first); for (auto p : b[i]) rmv(p.second, p.first); if (cnt == n - 1) minSum = min(minSum, sum); } if (minSum == OOLL) minSum = -1; if (f) printf("%lld\n", minSum); 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 int MAXN = 1e5 + 2; const int MAXL = 1e6 + 2; const int INF = 0x3f3f3f3f; const long long LINF = 1e11 + 1; vector<pair<int, int> > in[MAXN], out[MAXN]; int n, m, k; long long cost, ti[MAXL], to[MAXL]; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 0, d, f, t, c; i < m; i++) { scanf("%d%d%d%d", &d, &f, &t, &c); if (f == 0) out[t].push_back(make_pair(d, c)); else in[f].push_back(make_pair(d, c)); } cost = n * LINF; bool f = true; for (int i = 1; i <= n; i++) { sort(in[i].begin(), in[i].end()); sort(out[i].begin(), out[i].end()); int j = 0, t = 0; long long l = LINF; ti[0] += l; for (int j = 0; j < (int)in[i].size(); j++) { ti[in[i][j].first] += min(0LL, in[i][j].second - l); l += min(0LL, in[i][j].second - l); } l = LINF; to[MAXL - 1] += l; for (int j = (int)out[i].size() - 1; j >= 0; j--) { to[out[i][j].first] += min(0LL, out[i][j].second - l); l += min(0LL, out[i][j].second - l); } } for (int j = 1; j < MAXL; j++) ti[j] += ti[j - 1]; for (int j = MAXL - 2; j >= 0; j--) to[j] += to[j + 1]; for (int j = 0; j < MAXL - k - 1; j++) cost = min(cost, ti[j] + to[j + k + 1]); if (cost >= LINF) printf("-1\n"); else printf("%I64d\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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; /** * @author Don Li */ public class JuryMeeting { int N = (int) 1e6 + 10; int INF = (int) 1e9; void solve() { int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); int[] d = new int[m], f = new int[m], t = new int[m], c = new int[m]; for (int i = 0; i < m; i++) { d[i] = in.nextInt(); f[i] = in.nextInt(); t[i] = in.nextInt(); c[i] = in.nextInt(); } Queue<Integer> arrive = new PriorityQueue<>(Comparator.comparingInt(i -> d[i])); Queue<Integer> leave = new PriorityQueue<>(Comparator.comparingInt(i -> -d[i])); for (int i = 0; i < m; i++) { if (f[i] > 0) arrive.offer(i); else leave.offer(i); } int[] cnt = new int[N], min = new int[n + 1]; long[] sum = new long[N]; Arrays.fill(min, INF); for (int i = 1; i < N; i++) { cnt[i] += cnt[i - 1]; sum[i] += sum[i - 1]; while (!arrive.isEmpty() && d[arrive.peek()] == i) { int x = arrive.poll(); int city = f[x], cost = c[x]; if (min[city] == INF) { cnt[i]++; sum[i] += cost; min[city] = cost; } else if (min[city] > cost) { sum[i] -= min[city] - cost; min[city] = cost; } } } long ans = Long.MAX_VALUE; int cnt2 = 0; long sum2 = 0; int[] min2 = new int[n + 1]; Arrays.fill(min2, INF); for (int i = N - 2; i > 0; i--) { while (!leave.isEmpty() && d[leave.peek()] == i) { int x = leave.poll(); int city = t[x], cost = c[x]; if (min2[city] == INF) { cnt2++; sum2 += cost; min2[city] = cost; } else if (min2[city] > cost) { sum2 -= min2[city] - cost; min2[city] = cost; } if (i - k - 1 >= 0 && cnt[i - k - 1] == n && cnt2 == n) { ans = Math.min(ans, sum[i - k - 1] + sum2); } } } if (ans == Long.MAX_VALUE) ans = -1; out.println(ans); } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new JuryMeeting().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public 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; int n, m, k; const int MAXN = 1e5; const int MAXM = 1e5; const long long int INF = 1e15; struct Flight { int day; int departure; int arrival; int cost; Flight() {} Flight(int day, int departure, int arrival, int cost) { this->day = day; this->departure = departure; this->arrival = arrival; this->cost = cost; } } flights[MAXM]; static bool operator<(Flight f1, Flight f2) { return f1.day < f2.day; } int cities_mins[MAXN + 1][2]; struct MSet { int index; MSet() {} MSet(int index) { this->index = index; } }; static bool operator<(MSet m1, MSet m2) { return flights[m1.index].cost < flights[m2.index].cost; } struct Que { int index; multiset<MSet>::iterator it; Que() {} Que(int index, multiset<MSet>::iterator it) { this->index = index; this->it = it; } }; static bool operator<(Que q1, Que q2) { return q1.index > q2.index; } multiset<MSet> sets[MAXN + 1]; queue<Que> que; void read() { cin >> n >> m >> k; for (int i = 0; i < m; ++i) { int d, f, t, c; cin >> d >> f >> t >> c; flights[i] = Flight(d, f, t, c); } sort(flights, flights + m); } long long int init() { for (int i = 0; i < m; ++i) { Flight f = flights[i]; if (f.departure == 0) { auto it = sets[f.arrival].insert(MSet(i)); que.push(Que(i, it)); } } long long int res = 0; for (int i = 1; i <= n; ++i) { if (sets[i].empty()) return -1; cities_mins[i][1] = flights[(*sets[i].begin()).index].cost; res += cities_mins[i][1]; } return res; } long long int count_answer() { long long int backs = init(); if (backs == -1) return INF; int index = 0; long long int fronts = 0; int left = n; while (index < m && left) { Flight f = flights[index]; if (f.arrival == 0) { if (que.empty()) return INF; while (flights[que.front().index].day <= k + f.day) { auto front = que.front(); que.pop(); int city = flights[front.index].arrival; sets[city].erase(front.it); if (sets[city].empty()) return INF; int new_cost = flights[sets[city].begin()->index].cost; backs -= cities_mins[city][1]; backs += new_cost; cities_mins[city][1] = new_cost; } if (!cities_mins[f.departure][0]) { left--; } if (!cities_mins[f.departure][0] || cities_mins[f.departure][0] > f.cost) { fronts -= cities_mins[f.departure][0]; fronts += f.cost; cities_mins[f.departure][0] = f.cost; } } index++; } if (left) return INF; long long int res = backs + fronts; for (; index < m; ++index) { Flight f = flights[index]; if (f.arrival != 0) continue; if (que.empty()) return res; while (flights[que.front().index].day <= k + f.day) { auto front = que.front(); que.pop(); int city = flights[front.index].arrival; sets[city].erase(front.it); if (sets[city].empty()) return res; int new_cost = flights[sets[city].begin()->index].cost; backs -= cities_mins[city][1]; backs += new_cost; cities_mins[city][1] = new_cost; } if (!cities_mins[f.departure][0] || cities_mins[f.departure][0] > f.cost) { fronts -= cities_mins[f.departure][0]; fronts += f.cost; cities_mins[f.departure][0] = f.cost; } res = min(res, backs + fronts); } return res; } int main() { ios_base::sync_with_stdio(false); read(); long long int ans = count_answer(); if (ans == INF) { 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; struct Flight { int day, city; long long cost; Flight(int d, int ci, int c) : day(d), city(ci), cost(c) {} }; long long cost[1000100]; long long tmp[100100]; int cnt[1000100]; bool cmp0(Flight f1, Flight f2) { return f1.day > f2.day; } bool cmp1(Flight f1, Flight f2) { return f1.day < f2.day; } int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); vector<Flight> fli; int latest = 0; for (int i = 0; i < m; i++) { int d, s, t, c; scanf("%d %d %d %d", &d, &s, &t, &c); latest = max(latest, d); int city = s - t; fli.push_back(Flight(d, city, c)); } sort(fli.begin(), fli.end(), cmp0); int now = 0; for (int ti = latest; ti > 0; ti--) { cost[ti] = cost[ti + 1]; cnt[ti] = cnt[ti + 1]; while (now < fli.size() && fli[now].day == ti) { int city = fli[now].city; if (city < 0) { city = -city; cost[ti] -= tmp[city]; if (tmp[city] == 0) { cnt[ti] += 1; tmp[city] = fli[now].cost; } else { tmp[city] = min(tmp[city], fli[now].cost); } cost[ti] += tmp[city]; } now++; } } sort(fli.begin(), fli.end(), cmp1); memset(tmp, 0, sizeof(tmp)); now = 0; int arr = 0; long long sum = 0, ans = 0; for (int ti = 1; ti + k <= latest; ti++) { if (arr == n && cnt[ti + k] == n) { if (ans == 0) { ans = sum + cost[ti + k]; } else { ans = min(ans, sum + cost[ti + k]); } } while (now < fli.size() && fli[now].day == ti) { int city = fli[now].city; if (city > 0) { sum -= tmp[city]; if (tmp[city] == 0) { arr += 1; tmp[city] = fli[now].cost; } else { tmp[city] = min(tmp[city], fli[now].cost); } sum += tmp[city]; } now++; } } cout << (ans == 0 ? -1 : 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 MAXN = 100500; const int MAXD = 1000500; vector<pair<int, int>> inbound[MAXD]; vector<pair<int, int>> outbound[MAXD]; int minInbound[MAXN]; int maxOutbound[MAXN]; int mincost[MAXN]; unsigned long long total_cost_right[MAXD]; unsigned long long total_cost_left[MAXD]; int main() { int N, M, K; cin >> N >> M >> K; for (int i = 0; i < MAXN; i++) { minInbound[i] = MAXD; maxOutbound[i] = 0; mincost[i] = MAXD; } for (int i = 0; i < M; i++) { int day, dep, arr, cost; cin >> day >> dep >> arr >> cost; if (arr == 0) { inbound[day].push_back(make_pair(dep, cost)); minInbound[dep] = min(minInbound[dep], day); } else { outbound[day].push_back(make_pair(arr, cost)); maxOutbound[arr] = max(maxOutbound[arr], day); } } int minday = 0, maxday = MAXD; for (int i = 1; i <= N; i++) { minday = max(minday, minInbound[i]); maxday = min(maxday, maxOutbound[i]); } if (minday == 0 || maxday == MAXD || maxday - minday <= K) { cout << "-1\n"; return 0; } if (minday < 0 || minday > maxday || maxday < 0) { cout << "-1\n"; return 0; } for (int i = 1; i <= minday; i++) { for (auto x : inbound[i]) mincost[x.first] = min(mincost[x.first], x.second); } total_cost_left[minday] = 0; for (int i = 1; i <= N; i++) total_cost_left[minday] += mincost[i]; for (int i = minday + 1; i <= maxday; i++) { total_cost_left[i] = total_cost_left[i - 1]; for (auto x : inbound[i]) { if (x.second < mincost[x.first]) { total_cost_left[i] = total_cost_left[i] - mincost[x.first] + x.second; mincost[x.first] = x.second; } } } for (int i = 1; i <= N; i++) { mincost[i] = MAXD; } for (int i = MAXD - 1; i >= maxday; i--) for (auto x : outbound[i]) mincost[x.first] = min(mincost[x.first], x.second); total_cost_right[maxday] = 0; for (int i = 1; i <= N; i++) total_cost_right[maxday] += mincost[i]; for (int i = maxday - 1; i >= minday; i--) { total_cost_right[i] = total_cost_right[i + 1]; for (auto x : outbound[i]) { if (x.second < mincost[x.first]) { total_cost_right[i] = total_cost_right[i] - mincost[x.first] + x.second; mincost[x.first] = x.second; } } } unsigned long long ans = total_cost_left[minday] + total_cost_right[minday + K + 1]; for (int i = minday + 1; i <= maxday - K - 1; i++) { if (total_cost_left[i] + total_cost_right[i + K + 1] < ans) ans = total_cost_left[i] + total_cost_right[i + K + 1]; } 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
def bs_ceil(arr, val): l, r = -1, len(arr) # 0 to n while r - l > 1: mid = (l + r) / 2 if arr[mid][0] >= val: r = mid else: l = mid return r n, m, k = map(int, raw_input().split()) D = [float('inf')] * (n + 1) A = [float('inf')] * (n + 1) arr1 = [] arr2 = [] for i in xrange(m): d, s1, s2, c = map(int, raw_input().split()) if s2 == 0: arr1.append((d, s1, c)) if s1 == 0: arr2.append((d, s2, c)) arr1.sort() arr2.sort() n1, n2 = len(arr1), len(arr2) f, tc = 0, 0 a1, a2 = [], [] for i in xrange(n1): d, s1, c = arr1[i] if D[s1] == float('inf'): D[s1] = c f, tc = f + 1, tc + D[s1] else: tc -= D[s1] D[s1] = min(D[s1], c) tc += D[s1] if f == n and (a1 == [] or a1[-1][1] > tc): a1.append((d, tc)) f, tc = 0, 0 for i in reversed(xrange(n2)): d, s1, c = arr2[i] if A[s1] == float('inf'): A[s1] = c f, tc = f + 1, tc + A[s1] else: tc -= A[s1] A[s1] = min(A[s1], c) tc += A[s1] if f == n and (a2 == [] or a2[-1][1] > tc): a2.append((d, tc)) #print a1, a2 a2 = a2[::-1] n1, n2 = len(a1), len(a2) ans = float('inf') if len(a2) > 0: for i in xrange(n1): d, c = a1[i] p = bs_ceil(a2, d + k + 1) if p != len(a2): ans = min(ans, c + a2[p][1]) if ans == float('inf'): print -1 else: print ans
PYTHON
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 N = 1e5 + 2, inf = (1ll << 60); long long n, m, k, val[N], inv[N], outv[N], ind[N], outd[N], ans = inf; struct node { long long d, f, t, c; bool operator<(const node &t) const { return d < t.d; } } p; vector<node> in, out; long long read() { long long x = 0, f = 1; char s; while ((s = getchar()) > '9' || s < '0') if (s == '-') f = -1; while (s >= '0' && s <= '9') { x = (x << 1) + (x << 3) + (s ^ 48); s = getchar(); } return x * f; } bool check() { sort(in.begin(), in.end()); sort(out.begin(), out.end()); bool flag = 0; long long date = 0, Val = 0, cnt = 0; for (long long i = 0; i < in.size(); ++i) { p = in[i]; if (!val[p.f]) { val[p.f] = p.c; Val += p.c; date = p.d; ++cnt; } else if (val[p.f] > p.c) { Val += p.c - val[p.f]; val[p.f] = p.c; date = p.d; } if (cnt == n) { inv[i] = Val; ind[i] = date; flag = 1; } else inv[i] = inf; } if (!flag) return 0; flag = 0; date = Val = cnt = 0; memset(val, 0, sizeof val); for (long long i = out.size() - 1; i >= 0; --i) { p = out[i]; if (!val[p.t]) { val[p.t] = p.c; Val += p.c; date = p.d; ++cnt; } else if (val[p.t] > p.c) { Val += p.c - val[p.t]; val[p.t] = p.c; date = p.d; } if (cnt == n) { outv[i] = Val; outd[i] = date; flag = 1; } else outv[i] = inf; } if (!flag) return 0; return 1; } void solve() { if (!check()) { puts("-1"); return; } long long i = 0, j = 0; while (inv[i] == inf) ++i; while (i < in.size()) { long long date = ind[i]; while (j < out.size() && outv[j] < inf && outd[j] < date + k + 1) ++j; if (j >= out.size() || outv[j] == inf) break; ans = min(ans, inv[i] + outv[j]); ++i; } if (ans == inf) ans = -1; printf("%lld\n", ans); } signed main() { n = read(), m = read(), k = read(); for (long long i = 1; i <= m; ++i) { p.d = read(), p.f = read(), p.t = read(), p.c = read(); if (p.f == 0) out.push_back(p); else in.push_back(p); } solve(); 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 Gcd(int a, int b) { if (b == 0) return a; return Gcd(b, a % b); } int Lcm(int a, int b) { return a / Gcd(a, b) * b; } inline long long read() { long long f = 1, x = 0; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -f; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int maxn = 2e6; const int inf = 0x3f3f3f3f; struct node { int d, f, t, c; bool operator<(const node& rhs) const { return d < rhs.d; } } p[maxn]; vector<node> res[2]; long long sum[maxn], val[maxn]; long long sum2[maxn], val2[maxn]; int ok1[maxn], ok2[maxn]; int main() { int n = read(), m = read(), k = read(); for (int i = 1; i <= m; i++) { p[i].d = read(); p[i].f = read(); p[i].t = read(); p[i].c = read(); if (p[i].t == 0) { res[0].push_back(p[i]); } else { res[1].push_back(p[i]); } } sort(res[0].begin(), res[0].end()); sort(res[1].begin(), res[1].end()); int cur1 = 0, cur2 = (int)res[1].size() - 1; for (int i = 1; i <= 1e6; i++) { val[i] = val2[i] = inf; } sum[0] = 1ll * inf * n; int cnt1 = 0, cnt2 = 0; for (int i = 1; i <= 1e6; i++) { sum[i] = sum[i - 1]; while (cur1 < res[0].size() && i >= res[0][cur1].d) { int st = res[0][cur1].f; if (res[0][cur1].c < val[st]) { if (val[st] == inf) cnt1++; sum[i] -= val[st]; val[st] = res[0][cur1].c; sum[i] += val[st]; } cur1++; } if (cnt1 == n) ok1[i] = 1; } sum2[1000001] = 1ll * inf * n; for (int i = 1e6; i >= 1; i--) { sum2[i] = sum2[i + 1]; while (cur2 >= 0 && i <= res[1][cur2].d) { int st = res[1][cur2].t; if (res[1][cur2].c < val2[st]) { if (val2[st] == inf) cnt2++; sum2[i] -= val2[st]; val2[st] = res[1][cur2].c; sum2[i] += val2[st]; } cur2--; } if (cnt2 == n) ok2[i] = 1; } long long ans = LLONG_MAX; for (int st = 1; st <= 1e6; st++) { int ed = st + k + 1; if (ok1[st] && ok2[ed]) { ans = min(ans, sum[st] + sum2[ed]); } } if (ans == LLONG_MAX) 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; struct ren { int sei, huafei; }; vector<ren> v[2][1000000 + 233]; long long f[1000000 + 233], mi[1000000 + 233] = {0}, answer, result = 233333333333; int n, m, k; void nico(ren a) { if (mi[a.sei] > a.huafei) answer -= (mi[a.sei] - a.huafei), mi[a.sei] = a.huafei; } int main() { int i; scanf("%d%d%d", &n, &m, &k); for (int d, x, y, z; m--; v[!!y][d].push_back((ren){x + y, z})) scanf("%d%d%d%d", &d, &x, &y, &z); for (i = 1; i <= n; i++) mi[i] = 233333333333; for (answer = 233333333333 * n, i = 1; i <= 1000000; f[i] = answer, i++) for (int j = 0; j < (int)v[0][i].size(); j++) nico(v[0][i][j]); for (i = 1; i <= n; i++) mi[i] = 233333333333; for (answer = 233333333333 * n, i = 1000000; i; result = min(result, (long long)i - k - 1 > 0 ? answer + f[i - k - 1] : 233333333333), i--) for (int j = 0; j < (int)v[1][i].size(); j++) nico(v[1][i][j]); if (result == 233333333333) result = -1; cout << result; }
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 = 1000005; const long long mod = 1e9 + 7; const int INF = 0x3f3f3f3f; const double eps = 1e-9; int n, m, k; int dis[maxn]; long long ss[maxn], tt[maxn]; struct node { int d, u, v, c; } e[maxn]; bool cmp(const node &a, const node &b) { return a.d < b.d; } int main() { int Max = 0; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%d", &e[i].d, &e[i].u, &e[i].v, &e[i].c); Max = max(Max, e[i].d); } sort(e + 1, e + 1 + m, cmp); long long sum; int cnt = n; for (int i = 1; i <= m; i++) { int now = e[i].d; if (e[i].u) { if (!dis[e[i].u]) { dis[e[i].u] = e[i].c; cnt--; if (!cnt) { sum = 0; for (int i = 1; i <= n; i++) sum += dis[i]; ss[now] = sum; } } else if (dis[e[i].u] > e[i].c) { if (!cnt) { sum -= (dis[e[i].u] - e[i].c); dis[e[i].u] = e[i].c; ss[now] = sum; } else dis[e[i].u] = e[i].c; } } } if (cnt != 0) { puts("-1"); return 0; } memset((dis), (0), sizeof(dis)); cnt = n; for (int i = m; i >= 1; i--) { int now = e[i].d; if (e[i].v) { if (!dis[e[i].v]) { dis[e[i].v] = e[i].c; cnt--; if (!cnt) { sum = 0; for (int i = 1; i <= n; i++) sum += dis[i]; tt[now] = sum; } } else if (dis[e[i].v] > e[i].c) { if (!cnt) { sum -= (dis[e[i].v] - e[i].c); dis[e[i].v] = e[i].c; tt[now] = sum; } else dis[e[i].v] = e[i].c; } } } if (cnt != 0) { puts("-1"); return 0; } for (int i = 1; i <= Max; i++) { if (!ss[i]) ss[i] = ss[i - 1]; else if (ss[i - 1]) ss[i] = min(ss[i], ss[i - 1]); } for (int i = Max; i >= 1; i--) { if (!tt[i]) tt[i] = tt[i + 1]; else if (tt[i + 1]) tt[i] = min(tt[i], tt[i + 1]); } long long ans = 1e18; for (int i = 1; i <= Max - k - 1; i++) { if (ss[i] && tt[i + k + 1]) ans = min(ans, ss[i] + tt[i + k + 1]); } if (ans != 1e18) printf("%I64d\n", ans); else puts("-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
/** * Created by Aminul on 9/6/2017. */ import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.StringTokenizer; public class Main { public static int index; public static void main(String[] args)throws Exception { SystemInputReader in = new SystemInputReader(); PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); int n = in.nextInt(), m = in.nextInt(), k = in.nextInt(); long inf = (long)1e12; int maxN = (int)1e6; List<Node> list[] = new List[maxN+1]; for(int i = 0; i <= maxN; i++){ list[i] = new ArrayList<>(); } for(int i = 1; i <= m; i++){ int d = in.nextInt(), f = in.nextInt(), t = in.nextInt(), c = in.nextInt(); list[d].add(new Node(d, f, t, c)); } long totalSum = 0; int totalN = 0; long pre[] = new long[maxN+1]; long suf[] = new long[maxN+1]; int preN[] = new int[maxN+1]; int sufN[] = new int[maxN+1]; long[] min = new long[maxN+1]; Arrays.fill(min, inf); for(int i = 1; i <= maxN; i++){ for(Node node : list[i]){ if(node.t == 0){ if(min[node.f] > node.c){ if(min[node.f] == inf){ totalN++; min[node.f] = 0; } totalSum -= min[node.f]; min[node.f] = node.c; totalSum += min[node.f]; } } } preN[i] = totalN; pre[i] = totalSum; } Arrays.fill(min, inf); totalN = 0; totalSum = 0; for(int i = maxN; i >= 1; i--){ for(Node node : list[i]){ if(node.f == 0){ if(min[node.t] > node.c){ if(min[node.t] == inf){ totalN++; min[node.t] = 0; } totalSum -= min[node.t]; min[node.t] = node.c; totalSum += min[node.t]; } } } sufN[i] = totalN; suf[i] = totalSum; } boolean ok = true; long ans = inf; for(int i = 1, j = i+k+1; j <= maxN; i++, j++){ if(preN[i] == n && sufN[j] == n){ ans = Math.min(pre[i] + suf[j], ans); } } if(ans == inf) ans = -1; pw.println(ans); pw.close(); } static class Node implements Comparable<Node>{ int d, f, t; long c; Node(int D, int F, int T, long C){ d = D; f = F; t = T; c = C; } public int compareTo(Node node){ return d - node.d; } public String toString(){ return d+" "+f+" "+t+" "+c; } } static void debug(Object...obj) { System.err.println(Arrays.deepToString(obj)); } static class SystemInputReader { static final int ints[] = new int[128]; static final byte[] buff = new byte[100000000]; static int ind = 0; static { for(int i='0';i<='9';i++) ints[i]=i-'0'; try { System.in.read(buff); } catch (IOException e) { e.printStackTrace(); } } // @Override public int nextInt() throws IOException { int ret = 0, c; while ((c = getRead()) < '-'); //System.err.println(c + " " + ints[c]); boolean isNeg = false; if(c=='-') isNeg = true; else ret = ints[c]; while ( (c = getRead()) >='0') { //System.err.println(c + " " + ints[c]); ret = (ret<<3) + (ret<<1) + ints[c]; } return isNeg?-ret:ret; } private int getRead() throws IOException { // return System.in.read(); return buff[ind++]; } } }
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 n, m, k; struct flight { int d, f, c; flight(int _d, int _f, int _c) { d = _d; f = _f; c = _c; } }; vector<flight> a, b; long long fa[100011], fb[100011]; int dau[100011]; bool cmp1(flight p, flight q) { return p.d < q.d; } bool cmp2(flight p, flight q) { return p.d > q.d; } void init(vector<flight>& x, long long f[]) { long long sum = 0; int cnt = 0; for (int i = 1; i <= n; i++) dau[i] = 0; for (int i = 0; i < x.size(); i++) f[i] = -1; for (int i = 0; i < x.size(); i++) { if (dau[x[i].f] == 0) { dau[x[i].f] = x[i].c; sum += x[i].c; cnt++; } else { if (dau[x[i].f] > x[i].c) { sum = sum - dau[x[i].f] + x[i].c; dau[x[i].f] = x[i].c; } } if (cnt == n) f[i] = sum; } } int main() { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < m; i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (f == 0) b.push_back(flight(d, t, c)); if (t == 0) a.push_back(flight(d, f, c)); } sort(a.begin(), a.end(), cmp1); init(a, fa); sort(b.begin(), b.end(), cmp2); init(b, fb); int j = b.size() - 1; long long ans = -1; for (int i = 0; i < a.size(); i++) { if (fa[i] != -1) { while (j >= 0 && b[j].d <= a[i].d + k) j--; if (j < 0 || fb[j] == -1) break; if (ans == -1) { ans = fa[i] + fb[j]; } else { ans = min(ans, fa[i] + fb[j]); } } } 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 mod = 1e9 + 7; vector<pair<int, int> > go[1000010], back[1000010]; multiset<int> goi[1000010], backi[1000010]; int curgo[1000010], curback[1000010]; int main() { int n, m, k, d, s, t, c; cin >> n >> m >> k; for (int i = 1; i <= m; ++i) { scanf("%d %d %d %d", &d, &s, &t, &c); if (t == 0) go[d].push_back(make_pair(s, c)); else back[d].push_back(make_pair(t, c)); } long long ans = 1ll << 62, tmp = 0; int l = 1, r = l + k - 1, lim = 1000000; int cntgo = 0, cntback = 0; for (int i = r + 1; i <= lim; ++i) for (auto v : back[i]) backi[v.first].insert(v.second); for (int i = 1; i <= n; ++i) if (!backi[i].empty()) cntback++, tmp += (curback[i] = *(backi[i].begin())); for (; r <= lim;) { if (cntback < n) break; if (cntgo == n) ans = min(ans, tmp); ++r; for (auto v : back[r]) { tmp -= curback[v.first]; backi[v.first].erase(backi[v.first].find(v.second)); if (backi[v.first].empty()) --cntback; else tmp += (curback[v.first] = *backi[v.first].begin()); } for (auto v : go[l]) { if (goi[v.first].empty()) ++cntgo, curgo[v.first] = v.second, tmp += v.second, goi[v.first].insert(v.second); else { tmp -= curgo[v.first]; goi[v.first].insert(v.second); curgo[v.first] = *goi[v.first].begin(); tmp += curgo[v.first]; } } ++l; } if (ans == 1ll << 62) 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
import java.io.*; import java.util.*; import java.math.BigInteger; import java.util.Map.Entry; import static java.lang.Math.*; public class B extends PrintWriter { class Flight implements Comparable<Flight> { final int d, f, t, c; public Flight(int d, int f, int t, int c) { this.d = d; this.f = f; this.t = t; this.c = c; } @Override public int compareTo(Flight flight) { return Integer.compare(d, flight.d); } } void run() { final int n = nextInt(), m = nextInt(), k = nextInt(); final int w = 1_000_003; Flight[] f = new Flight[m]; for (int j = 0; j < m; j++) { f[j] = new Flight(nextInt(), nextInt() - 1, nextInt() - 1, nextInt()); } Arrays.sort(f); final long inf = (long) 1e15; long[] l = new long[w], r = new long[w]; long ans = inf; Arrays.fill(l, inf); Arrays.fill(r, inf); long[] b = new long[n]; { int cnt = 0; Arrays.fill(b, inf); long cur = 0; int p = 0; for (int d = 0; d < w; d++) { while (p < m && f[p].d < d) { Flight flight = f[p++]; int u = flight.f; int v = flight.t; if (u != -1 && v == -1) { long c = flight.c; if (c < b[u]) { if (b[u] == inf) { ++cnt; cur += c; } else { cur -= b[u]; cur += c; } b[u] = c; } } } if (cnt == n) { l[d] = cur; } } } { int cnt = 0; Arrays.fill(b, inf); long cur = 0; int p = m - 1; for (int d = w - 1; d >= 0; d--) { while (p >= 0 && d < f[p].d) { Flight flight = f[p--]; int v = flight.f; int u = flight.t; if (u != -1 && v == -1) { long c = flight.c; if (c < b[u]) { if (b[u] == inf) { ++cnt; cur += c; } else { cur -= b[u]; cur += c; } b[u] = c; } } } if (cnt == n) { r[d] = cur; } } } for (int to = k - 1; to < w; to++) { int from = to - k + 1; ans = min(ans, l[from] + r[to]); } if (ans < inf) { println(ans); } else { print(-1); } } void skip() { while (hasNext()) { next(); } } int[][] nextMatrix(int n, int m) { int[][] matrix = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) matrix[i][j] = nextInt(); return matrix; } String next() { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(nextLine()); return tokenizer.nextToken(); } boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String line = nextLine(); if (line == null) { return false; } tokenizer = new StringTokenizer(line); } return true; } int[] nextArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { try { return reader.readLine(); } catch (IOException err) { return null; } } public B(OutputStream outputStream) { super(outputStream); } static BufferedReader reader; static StringTokenizer tokenizer = new StringTokenizer(""); static Random rnd = new Random(); static boolean OJ; public static void main(String[] args) throws IOException { OJ = System.getProperty("ONLINE_JUDGE") != null; B solution = new B(System.out); if (OJ) { reader = new BufferedReader(new InputStreamReader(System.in)); solution.run(); } else { reader = new BufferedReader(new FileReader(new File(B.class.getName() + ".txt"))); long timeout = System.currentTimeMillis(); while (solution.hasNext()) { solution.run(); solution.println(); solution.println("----------------------------------"); } solution.println("time: " + (System.currentTimeMillis() - timeout)); } solution.close(); reader.close(); } }
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.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.List; import java.util.PriorityQueue; public class JuryMeeting { public static void main(String[] args) { FasterScanner sc = new FasterScanner(); int n = sc.nextInt(); int m = sc.nextInt(); int k = sc.nextInt(); List<Flight> arr = new ArrayList<>(); List<Flight> dep = new ArrayList<>(); for(int i=0;i<m;i++) { int day = sc.nextInt(); int start = sc.nextInt(); int end = sc.nextInt(); int cost = sc.nextInt(); if (start == 0) { dep.add(new Flight(day, cost, end)); } else { arr.add(new Flight(day, cost, start)); } } dep.sort((f1,f2) -> f1.day - f2.day); arr.sort((f1,f2) -> f1.day - f2.day); int[] minStart = new int[n+1]; int total = 0; int ind = 0; while(total < n && ind < arr.size()) { if (minStart[arr.get(ind).city] == 0) { minStart[arr.get(ind).city] = arr.get(ind).cost; total++; } minStart[arr.get(ind).city] = Math.min(minStart[arr.get(ind).city], arr.get(ind).cost); ind++; } long costArr = 0; for (int x : minStart) { costArr += x; } //Not enough flights towards M if(total < n) { impossible(); } int startDep = arr.get(ind-1).day+k+1; PriorityQueue<Flight>[] minDep = new PriorityQueue[n+1]; int depPointer = 0; for (Flight f : dep) { if (f.day >= startDep) { if (minDep[f.city] == null) minDep[f.city] = new PriorityQueue<Flight>((f1,f2) -> f1.cost-f2.cost); minDep[f.city].add(f); } else { depPointer++; } } long costDep = 0; for (int i=1; i<=n; i++) { if (minDep[i] == null) { impossible(); } costDep += minDep[i].peek().cost; } long bestTotal = costArr + costDep; int startArr = 0; for (int i=ind;i<arr.size();i++) { //Add new item to arriving flights Flight newArr = arr.get(i); if (minStart[newArr.city] > newArr.cost) { costArr -= minStart[newArr.city] - newArr.cost; minStart[newArr.city] = newArr.cost; //Remove all elements within k days of new arrival flight while(dep.get(depPointer).day < newArr.day+k+1) { if (minDep[dep.get(depPointer).city].peek() == dep.get(depPointer)) { long costDelta = -dep.get(depPointer).cost; while(minDep[dep.get(depPointer).city].peek().day < newArr.day+k+1) { minDep[dep.get(depPointer).city].poll(); if (minDep[dep.get(depPointer).city].isEmpty()) { printResultAndStop(bestTotal); } } costDelta += minDep[dep.get(depPointer).city].peek().cost; costDep += costDelta; } depPointer++; if (depPointer >= dep.size()) { printResultAndStop(bestTotal); } } bestTotal = Math.min(bestTotal, costArr + costDep); } } printResultAndStop(bestTotal); } static void printResultAndStop(long total) { System.out.println(total); System.exit(0); } static void impossible() { printResultAndStop(-1); } static class Flight{ int cost; int day; int city; public Flight(int day, int cost, int city) { this.day = day; this.cost = cost; this.city = city; } } public static class FasterScanner { private InputStream mIs; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FasterScanner() { this(System.in); } public FasterScanner(InputStream is) { mIs = is; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = mIs.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } 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 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 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 < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
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; void getint(int &v) { char ch, fu = 0; for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar()) ; if (ch == '-') fu = 1, ch = getchar(); for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0'; if (fu) v = -v; } const long long INF = 1e18; int n, m, k; long long now, ans, L[1000010], R[1000010]; map<int, int> M; vector<pair<int, int> > A[1000010], B[1000010]; int main() { cin >> n >> m >> k; for (int i = 1; i <= m; ++i) { int d, f, t, c; getint(d), getint(f), getint(t), getint(c); if (t == 0) A[d].push_back(make_pair(f, c)); else B[d].push_back(make_pair(t, c)); } for (int i = 1; i <= 1000000; ++i) { for (int j = 0; j < A[i].size(); ++j) { pair<int, int> t = A[i][j]; if (M.find(t.first) == M.end()) { M[t.first] = t.second; now += M[t.first]; } else { now -= M[t.first]; M[t.first] = min(M[t.first], t.second); now += M[t.first]; } } if (M.size() == n) L[i] = now; else L[i] = -1; } M.clear(); now = 0; for (int i = 1000000; i >= 1; --i) { for (int j = 0; j < B[i].size(); ++j) { pair<int, int> t = B[i][j]; if (M.find(t.first) == M.end()) { M[t.first] = t.second; now += M[t.first]; } else { now -= M[t.first]; M[t.first] = min(M[t.first], t.second); now += M[t.first]; } } if (M.size() == n) R[i] = now; else R[i] = -1; } ans = INF; for (int i = 1; i <= 1000000; ++i) { if (i + k + 1 > 1000000) continue; if (L[i] == -1 || R[i + k + 1] == -1) continue; ans = min(ans, L[i] + R[i + k + 1]); } if (ans < INF) 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 = 0x3f3f3f3f3f3f3f; long long pre[2000005]; long long suf[2000005]; bool vis[2000005]; long long now[2000005]; struct edge { int id; long long c; }; vector<edge> G1[2000005]; vector<edge> G2[2000005]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); int maxx = 0; while (m--) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); edge e; e.c = c; if (f != 0) { e.id = f; G1[d].push_back(e); } else { e.id = t; G2[d].push_back(e); } maxx = max(maxx, d); } memset(suf, 0x3f, sizeof(suf)); memset(pre, 0x3f, sizeof(pre)); memset(now, 0x3f, sizeof(now)); memset(vis, false, sizeof(vis)); int cnt = 0; long long ans = 0; for (int i = 1; i <= maxx; i++) { for (int j = 0; j < G1[i].size(); j++) { int v = G1[i][j].id; if (!vis[v]) { vis[v] = true; ++cnt; ans += G1[i][j].c; now[v] = G1[i][j].c; } else if (G1[i][j].c < now[v]) { ans -= now[v]; now[v] = G1[i][j].c; ans += now[v]; } } if (cnt == n) { pre[i] = ans; } else { pre[i] = inf; } } memset(vis, false, sizeof(vis)); memset(now, 0x3f, sizeof(now)); ans = 0; cnt = 0; for (int i = maxx; i >= 1; i--) { for (int j = 0; j < G2[i].size(); j++) { int v = G2[i][j].id; if (!vis[v]) { vis[v] = true; ++cnt; now[v] = G2[i][j].c; ans += G2[i][j].c; } else if (G2[i][j].c < now[v]) { ans -= now[v]; now[v] = G2[i][j].c; ans += now[v]; } } if (cnt == n) { suf[i] = ans; } else { suf[i] = inf; } } long long Ans = inf; for (int i = 1; i + k + 1 <= maxx; i++) { Ans = min(Ans, pre[i] + suf[i + k + 1]); } 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
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 50; ; const int M = 17; const int mod = 19260817; const int mo = 123; const double pi = acos(-1.0); long long qpow(int x, int qq) { long long f = 1, p = x; while (qq) { if (qq & 1) f = f * p % mod; p = 1LL * p * p % mod; qq >>= 1; } } int n, m, k; int mn[2][N], pre[N]; bool vis[N]; multiset<int> s[N]; struct man { int d, f, t, c; } arrive[N], depart[N]; bool cmp(const man &a, const man &b) { return a.d < b.d; }; int main() { for (int i = 1; i < N; i++) mn[0][i] = mn[1][i] = 10000000; int cnt1 = 0, cnt2 = 0, cnt = 0, L = -1, R = -1; scanf("%d%d%d", &n, &m, &k); while (m--) { int d, f, t, c; scanf("%d%d%d%d", &d, &f, &t, &c); if (!f) depart[++cnt2] = man{d, f, t, c}; if (!t) arrive[++cnt1] = man{d, f, t, c}; } sort(arrive + 1, arrive + 1 + cnt1, cmp); sort(depart + 1, depart + 1 + cnt2, cmp); for (int i = 1; i <= cnt1; i++) { mn[0][arrive[i].f] = min(mn[0][arrive[i].f], arrive[i].c); if (!vis[arrive[i].f]) { vis[arrive[i].f] = true; cnt++; } if (cnt == n) { L = i; break; } } int l = L, r; memset(vis, false, sizeof vis); cnt = 0; for (int i = cnt2; i >= 1; i--) { if (depart[i].d < arrive[L].d + k + 1) break; mn[1][depart[i].t] = min(mn[1][depart[i].t], depart[i].c); s[depart[i].t].insert(depart[i].c); r = i; if (!vis[depart[i].t]) { vis[depart[i].t] = true; cnt++; } if (cnt == n && R == -1) { R = i; } } long long ans = 0, res; for (int i = 1; i <= n; i++) { pre[i] = mn[1][i]; ans += mn[0][i] + mn[1][i]; } res = ans; while (1) { if (arrive[l].d + k + 1 > depart[R].d || r > R || l >= cnt1) break; while (arrive[l].d + k + 1 <= depart[r].d && l < cnt1) { l++; if (arrive[l].c < mn[0][arrive[l].f]) { ans -= (mn[0][arrive[l].f] - arrive[l].c); mn[0][arrive[l].f] = arrive[l].c; } if (arrive[l].d + k + 1 <= depart[r].d) res = min(res, ans); } while (depart[r].d < arrive[l].d + 1 + k && r < R) { r++; s[depart[r - 1].t].erase(s[depart[r - 1].t].find(depart[r - 1].c)); ans -= (pre[depart[r - 1].t] - *s[depart[r - 1].t].begin()); pre[depart[r - 1].t] = *s[depart[r - 1].t].begin(); if (arrive[l].d + k + 1 <= depart[r].d) res = min(res, ans); } } if (L == -1 || R == -1) puts("-1"); else if (depart[R].d - arrive[L].d >= k + 1) printf("%lld\n", res); 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 = 1e18L; const int inf = 0x3f3f3f3f; const int maxn = 1e6 + 5; const int N = 1e5 + 5; const int Mod = 1e9 + 7; struct node { int d, u, c; void init(int _d = 0, int _u = 0, int _c = 0) { d = _d; u = _u; c = _c; } friend bool operator<(node p, node q) { return p.d < q.d; } } a[N], b[N]; int t1, t2; int n, m, k, d, f, t, c; unsigned long long dp1[maxn], dp2[maxn], arr[N], away[N]; int main() { ios::sync_with_stdio(false); cin >> n >> m >> k; t1 = t2 = 0; for (int i = 1; i <= m; i++) { cin >> d >> f >> t >> c; if (t == 0) a[t1++].init(d, f, c); else b[t2++].init(d, t, c); } sort(a, a + t1); sort(b, b + t2); int i = 0, num = 0; for (int i = 0; i < N; i++) arr[i] = away[i] = INF; unsigned long long cost = 0; for (d = 1; d < maxn; d++) { while (i < t1 && a[i].d <= d) { int u = a[i].u; if (arr[u] == INF) { arr[u] = a[i].c; cost += a[i].c; num++; } else if (arr[u] > a[i].c) { cost += -arr[u] + a[i].c; arr[u] = a[i].c; } i++; } if (num < n) dp1[d] = INF; else dp1[d] = cost; } num = cost = 0; i = t2 - 1; for (d = maxn - 1; d >= 1; d--) { while (i >= 0 && b[i].d >= d) { int u = b[i].u; if (away[u] == INF) { away[u] = b[i].c; cost += b[i].c; num++; } else if (away[u] > b[i].c) { cost += -away[u] + b[i].c; away[u] = b[i].c; } i--; } if (num < n) dp2[d] = INF; else dp2[d] = cost; } unsigned long long ans = INF; for (int i = 1; i < maxn; i++) { if (i + k + 1 < maxn) ans = min(ans, dp1[i] + dp2[i + k + 1]); } if (ans < INF) cout << ans << 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
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n, m, k; vector<pair<long long, int> > a[N], b[N]; int dd, xx, yy; long long cc; long long f1[N], f2[N]; long long c1[N], c2[N]; int g1[N], g2[N]; long long now; int ng; long long ans; bool cmp(const pair<long long, int> &a, const pair<long long, int> &b) { return a.first > b.first; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i(1); i <= (m); ++i) { scanf("%d%d%d%lld", &dd, &xx, &yy, &cc); if (yy == 0) { a[dd].push_back(make_pair(cc, xx)); } else { b[dd].push_back(make_pair(cc, yy)); } } for (int i(1); i <= (1e6); ++i) { sort(a[i].begin(), a[i].end(), cmp); sort(b[i].begin(), b[i].end(), cmp); } memset(f1, 0, sizeof f1); now = 0; ng = 0; for (int i(1); i <= (1e6); ++i) { for (auto cnt : a[i]) { int u = cnt.second; long long w = cnt.first; if (f1[u]) { if (f1[u] > w) { now -= f1[u]; now += w; f1[u] = w; } } else { now += w; f1[u] = w; ++ng; } } c1[i] = now; g1[i] = ng; } now = 0; ng = 0; for (int i(1e6); i >= (1); --i) { for (auto cnt : b[i]) { int u = cnt.second; long long w = cnt.first; if (f2[u]) { if (f2[u] > w) { now -= f2[u]; now += w; f2[u] = w; } } else { now += w; f2[u] = w; ++ng; } } c2[i] = now; g2[i] = ng; } ans = 2e18; for (int i(1); i <= (1e6); ++i) { if (g1[i] == n) { if (i + k + 1 <= 1e6) { if (g2[i + k + 1] == n) { ans = min(ans, c1[i] + c2[i + k + 1]); } } } } if (ans < 1e18) printf("%lld\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 = acos(-1.0); double fRand(double fMin, double fMax) { double f = (double)rand() / RAND_MAX; return fMin + f * (fMax - fMin); } template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } struct flight { int d, u, c; }; bool incD(const flight &a, const flight &b) { return (a.d < b.d); } bool decD(const flight &a, const flight &b) { return (a.d > b.d); } int n, m, k, cntPos; long long dpL[1000005], dpR[1000005]; int minC[100005]; vector<flight> f[2]; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = (1); i <= (m); ++i) { int d, s, e, c; scanf("%d%d%d%d", &d, &s, &e, &c); int t, u; if (e == 0) { t = 0; u = s; } else { t = 1; u = e; } f[t].push_back({d, u, c}); } sort(f[0].begin(), f[0].end(), incD); int cntPos = n; long long sum = 0; for (int i = (1); i <= (n); ++i) { minC[i] = 1000000007; sum += minC[i]; } int j = 0; for (int i = (1); i <= (1000005 - 1); ++i) { while (j < (int)f[0].size() && f[0][j].d <= i) { int u = f[0][j].u, c = f[0][j].c; if (c < minC[u]) { if (minC[u] == 1000000007) --cntPos; sum += c - minC[u]; minC[u] = c; } ++j; } dpL[i] = (cntPos == 0) ? sum : (1LL * 1000000007 * 1000000007); } sort(f[1].begin(), f[1].end(), decD); cntPos = n; sum = 0; for (int i = (1); i <= (n); ++i) { minC[i] = 1000000007; sum += minC[i]; } j = 0; for (int i = (1000005 - 1); i >= (1); --i) { while (j < (int)f[1].size() && f[1][j].d >= i) { int u = f[1][j].u, c = f[1][j].c; if (c < minC[u]) { if (minC[u] == 1000000007) --cntPos; sum += c - minC[u]; minC[u] = c; } ++j; } dpR[i] = (cntPos == 0) ? sum : (1LL * 1000000007 * 1000000007); } long long ans = 1LL * 1000000007 * 1000000007; for (int i = (1); i <= (1000005 - k - 2); ++i) { ans = min(ans, dpL[i] + dpR[i + k + 1]); } if (ans >= 1LL * 1000000007 * 1000000007) ans = -1; 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; const int N = 100000 + 10; const int K = 1000000 + 10; int n, m, k, iin, iout; vector<pair<int, int> > in[K], out[K]; int mnst[N], mnen[N]; long long st[K], en[K]; int main() { scanf("%d %d %d", &n, &m, &k); for (int i = 1, d, f, t, c; i <= m; i++) { scanf("%d %d %d %d", &d, &f, &t, &c); if (f == 0) { out[d].push_back(make_pair(t, c)); } else { in[d].push_back(make_pair(f, c)); } } memset(mnst, -1, sizeof(mnst)); long long cur = 0, cnt = 0; for (int i = 1; i < K; i++) { for (int j = 0; j < in[i].size(); j++) { if (mnst[in[i][j].first] == -1) { mnst[in[i][j].first] = in[i][j].second; cur += in[i][j].second; cnt++; } else if (in[i][j].second < mnst[in[i][j].first]) { cur = cur - mnst[in[i][j].first] + in[i][j].second; mnst[in[i][j].first] = in[i][j].second; } } if (cnt == n) st[i] = cur; else st[i] = -1; } memset(mnen, -1, sizeof(mnen)); cur = 0, cnt = 0; for (int i = K - 1; i; i--) { for (int j = 0; j < out[i].size(); j++) { if (mnen[out[i][j].first] == -1) { mnen[out[i][j].first] = out[i][j].second; cur += out[i][j].second; cnt++; } else if (out[i][j].second < mnen[out[i][j].first]) { cur = cur - mnen[out[i][j].first] + out[i][j].second; mnen[out[i][j].first] = out[i][j].second; } } if (cnt == n) en[i] = cur; else en[i] = -1; } long long ans = -1; for (int i = 1; i < K; i++) { if (st[i] == -1 || i + k + 1 >= K || en[i + k + 1] == -1) continue; if (ans == -1) ans = st[i] + en[i + k + 1]; else ans = min(ans, st[i] + en[i + k + 1]); } 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; const long long INF = 1e12; const int INF1 = 1e7; const int maxn = 100000 + 10; int n, m, k; int fd, u, v, w; struct node { int to, cost, fd; } a[maxn], b[maxn]; bool cmp(node f, node g) { return f.fd < g.fd; } bool cmp1(node f, node g) { return f.fd > g.fd; } int ecnt = 0, ecnt1 = 0; long long dp[maxn * 10], dp1[maxn * 10]; int d[maxn], d1[maxn]; int mx = 0; int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= m; i++) { scanf("%d%d%d%d", &fd, &u, &v, &w); if (v == 0) { a[ecnt].to = u, a[ecnt].cost = w, a[ecnt].fd = fd; ecnt++; } if (u == 0) { b[ecnt1].to = v, b[ecnt1].cost = w, b[ecnt1].fd = fd; ecnt1++; } mx = max(mx, fd); } sort(a, a + ecnt, cmp); sort(b, b + ecnt1, cmp1); for (int i = 1; i <= n; i++) d[i] = INF1, d1[i] = INF1; int cnt = 0; int tmp = -1; for (int i = 0; i < ecnt; i++) { v = a[i].to; if (d[v] == INF1) { cnt++; } d[v] = min(d[v], a[i].cost); if (cnt == n) { tmp = i; break; } } if (tmp == -1) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) dp[a[tmp].fd] += d[i]; for (int i = tmp + 1; i < ecnt; i++) { v = a[i].to; dp[a[i].fd] = dp[a[i - 1].fd]; dp[a[i].fd] = dp[a[i].fd] - d[v]; d[v] = min(d[v], a[i].cost); dp[a[i].fd] += d[v]; } cnt = 0; tmp = -1; for (int i = 0; i < ecnt1; i++) { v = b[i].to; if (d1[v] == INF1) { cnt++; } d1[v] = min(d1[v], b[i].cost); if (cnt == n) { tmp = i; break; } } if (tmp == -1) { printf("-1\n"); return 0; } for (int i = 1; i <= n; i++) dp1[b[tmp].fd] += d1[i]; for (int i = tmp + 1; i < ecnt1; i++) { v = b[i].to; dp1[b[i].fd] = dp1[b[i - 1].fd]; dp1[b[i].fd] = dp1[b[i].fd] - d1[v]; d1[v] = min(d1[v], b[i].cost); dp1[b[i].fd] += d1[v]; } for (int i = 1; i <= mx; i++) { if (dp[i] == 0) dp[i] = dp[i - 1]; } for (int i = mx; i >= 1; i--) { if (dp1[i] == 0) dp1[i] = dp1[i + 1]; } long long ans = INF; for (int i = 0; i < ecnt; i++) { if (dp[a[i].fd] != 0) { if (a[i].fd + k + 1 <= mx && dp1[a[i].fd + k + 1] != 0) ans = min(ans, dp[a[i].fd] + dp1[a[i].fd + 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; void func(void) { freopen("input.c", "r", stdin); freopen("output.c", "w", stdout); } void print(vector<long long> &v) { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { printf("%lld ", v[i]); } printf("\n"); } void print(vector<pair<long long, long long> > &v) { cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { printf("%lld %lld\n", v[i].first, v[i].second); } } void print(double d) { cout << fixed << setprecision(10) << d << endl; } void print(string s, double d) { cout << s << " "; cout << fixed << setprecision(10) << d << endl; } const int N = 1e5 + 100; struct info { long long day, cost, place; bool operator<(info k1) const { return day < k1.day; } }; vector<info> in; vector<info> out; set<pair<long long, long long> > outFlight[N]; long long inFlight[N]; int main() { long long n, q, i, j = 0, temp, t, k, ans = 0, sum = 0, x, y, z, cnt = 0, m, fg = 0, mx = 0, mx1 = 0, mn = 8e18, mn1 = 8000000000000000000; scanf("%lld %lld %lld", &n, &m, &k); for (int i = 0; i < m; i++) { scanf("%lld %lld", &x, &y); scanf("%lld %lld", &z, &t); info xx; xx.day = x; xx.place = y; if (y == 0) xx.place = z; xx.cost = t; if (y) in.push_back(xx); else out.push_back(xx); } sort(in.begin(), in.end()); sort(out.begin(), out.end()); for (int i = 0; i < N; i++) inFlight[i] = 1e10; int ok = 0; for (int i = 0; i < out.size(); i++) { outFlight[out[i].place].insert({out[i].cost, i}); } long long outSum = 0; long long inSum = 0; for (int i = 1; i <= n; i++) { if (outFlight[i].size() == 0) { printf("-1\n"); return 0; } auto it = outFlight[i].begin(); pair<long long, long long> p = *it; outSum += p.first; } cnt = 0; for (int i = 0; i < in.size(); i++) { long long now = in[i].day; long long place = in[i].place; long long cost = in[i].cost; if (inFlight[place] == 1e10) { inFlight[place] = cost; inSum += cost; cnt++; } else { inSum -= inFlight[place]; inFlight[place] = min(inFlight[place], cost); inSum += inFlight[place]; } now += k + 1; fg = 0; for (; ok < out.size(); ok++) { if (out[ok].day >= now) break; auto it = outFlight[out[ok].place].begin(); pair<long long, long long> p = *it; outSum -= p.first; p = {out[ok].cost, ok}; outFlight[out[ok].place].erase(p); if (outFlight[out[ok].place].size() == 0) { fg = 1; break; } it = outFlight[out[ok].place].begin(); p = *it; outSum += p.first; } if (fg) break; if (cnt == n) { mn = min(mn, inSum + outSum); } } if (mn == 8e18) 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; const int N = 1e6; inline int getint() { int w = 0, q = 0; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') q = 1, c = getchar(); while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar(); return q ? -w : w; } struct Node { int c; int day; long long cost; Node() {} Node(int cc, int d, long long co) : c(cc), day(d), cost(co) {} bool operator<(const Node &t1) const { return day < t1.day; } bool operator>(const Node &t1) const { return day > t1.day; } }; vector<Node> v[100005]; priority_queue<Node, vector<Node>, greater<Node> > q; long long min_come_cost[100005]; long long min_back_cost[100005]; int min_back_day[1000005]; int last_back[100005]; int n, m, k; void updata(int rt, int l, int r, int x, int cost) { if (l == r) { min_back_day[rt] = cost; return; } int mid = (l + r) >> 1; if (mid >= x) { updata(rt << 1, l, mid, x, cost); } else { updata(rt << 1 | 1, mid + 1, r, x, cost); } min_back_day[rt] = min(min_back_day[rt << 1], min_back_day[rt << 1 | 1]); } int query(int rt = 1, int l = 1, int r = n) { if (l == r) { return l; } if (min_back_day[rt] == min_back_day[rt << 1]) { return query(rt << 1, l, (l + r) >> 1); } else { return query(rt << 1 | 1, ((l + r) >> 1) + 1, r); } } int main() { n = getint(); m = getint(); k = getint(); int a, b, c, d; for (int i = 0; i < m; i++) { a = getint(); b = getint(); c = getint(); d = getint(); if (b == 0) { v[c].push_back(Node(c, a, d)); } else { q.push(Node(b, a, d)); } } for (int i = 1; i <= n; i++) { sort(v[i].begin(), v[i].end()); } for (int i = 1; i <= n; i++) { for (int j = v[i].size() - 1; j > 0; j--) { v[i][j - 1].cost = min(v[i][j - 1].cost, v[i][j].cost); } } long long ans = 0X3F3F3F3F3F3F3F3F; long long sum = 0; for (int i = 1; i <= n; i++) { if (v[i].size() > 0) { sum += v[i][0].cost; updata(1, 1, n, i, v[i][0].day); min_back_cost[i] = v[i][0].cost; } else { ans = -1; printf("%I64d\n", ans); return 0; } } int have_come = 0; while (true) { if (!q.empty()) { while (!q.empty() && q.top().day + k < min_back_day[1]) { if (min_come_cost[q.top().c] == 0) { have_come++; sum += q.top().cost; min_come_cost[q.top().c] = q.top().cost; } else { if (min_come_cost[q.top().c] > q.top().cost) { sum -= min_come_cost[q.top().c]; sum += q.top().cost; min_come_cost[q.top().c] = q.top().cost; } } q.pop(); } } if (have_come == n) { ans = min(sum, ans); } int t = query(); if (last_back[t] + 1 >= v[t].size()) { break; } sum -= v[t][last_back[t]].cost; last_back[t]++; sum += v[t][last_back[t]].cost; min_back_cost[t] = v[t][last_back[t]].cost; updata(1, 1, n, t, v[t][last_back[t]].day); } if (ans != 0X3F3F3F3F3F3F3F3F) { for (int i = 1; i <= n; i++) { while (++last_back[i] < v[i].size()) { if (min_back_cost[i] > v[i][last_back[i]].cost) { sum -= min_back_cost[i]; sum += v[i][last_back[i]].cost; min_back_cost[i] = v[i][last_back[i]].cost; } } } ans = min(sum, ans); printf("%I64d\n", ans); } else { printf("-1\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.math.BigInteger; import java.util.StringTokenizer; import java.util.*; import java.io.*; public class Wtf { public static void main(String[] args) throws IOException { Ifmo ifmo = new Ifmo(); } } class Ifmo { ArrayList<Integer> Graph[]; int n, l, m, primes, timer; char[][] field; ArrayList<FUCKINGVERTEX> Ribs; int[][] roofs, ans, up; Vertex[][] highes; ArrayList<Integer> fin; static int cur; int[] p, size, tin, tout; boolean[] used; int logn; int[][] pref; int[][] longest; int[][][] dp = new int[31][31][51]; long mod; HashMap<Integer, Long>[] tree, bit; Ifmo() throws IOException { FScanner fs = new FScanner(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int n = fs.nextInt(), m = fs.nextInt(), k = fs.nextInt(); ArrayList<Pair>[] in = new ArrayList[n]; ArrayList<Pair>[] out = new ArrayList[n]; for (int i =0 ; i<n; i++) { in[i] = new ArrayList<>(); out[i] = new ArrayList<>(); } for (int i =0 ; i<m; i++) { int d = fs.nextInt(); int s1 = fs.nextInt(); int s2 = fs.nextInt(); int c = fs.nextInt(); if (s1 == 0) out[s2-1].add(new Pair(d, c)); else in[s1-1].add(new Pair(d, c)); } for (int i = 0; i<n; i++) { Collections.sort(in[i]); Collections.sort(out[i]); } TreeMap<Integer, ArrayList<Pair>> mapin = new TreeMap<>(); int st = 1, end = (int) 1e6; TreeMap<Integer, ArrayList<Pair>> mapout = new TreeMap<>(); for (int i =0 ; i<n; i++) { if (in[i].size() == 0 || out[i].size() == 0) { System.out.println(-1); System.exit(0); } st = Math.max(st, in[i].get(0).x); end = Math.min(end, out[i].get(out[i].size()-1).x); if (end-st-1<k) { System.out.println(-1); System.exit(0); } } ArrayList<Pair> iq[] = new ArrayList[n]; ArrayList<Pair> ou[] = new ArrayList[n]; for (int i = 0; i<n; i++) { iq[i] = new ArrayList<>(); ou[i] = new ArrayList<>(); for (int j =0;j<in[i].size(); j++) { if (in[i].get(j).x<=end-k-1) { if (j == 0) { iq[i].add(in[i].get(j)); } else { if (in[i].get(j-1).x == in[i].get(j).x) { if (in[i].get(j-1).y>in[i].get(j).y) { iq[i].remove(iq[i].size()-1); iq[i].add(in[i].get(j)); } } else { iq[i].add(in[i].get(j)); } } }} for (int j =0;j<out[i].size(); j++) { if (out[i].get(j).x>=st+k+1) { if (j == 0) { ou[i].add(out[i].get(j)); } else { if (out[i].get(j-1).x == out[i].get(j).x) { if (out[i].get(j-1).y>out[i].get(j).y) { ou[i].remove(ou[i].size()-1); ou[i].add(out[i].get(j)); } } else { ou[i].add(out[i].get(j)); } }} } } int[] prefs = new int[n]; int[] suffs = new int[n]; for (int i = 0; i<n; i++) prefs[i] = (suffs[i] = (int) 1e9); for (int i =0 ; i<n; i++) { int j; for (j = 0; j<iq[i].size() && iq[i].get(j).x<=st; j++) { prefs[i] = Math.min(prefs[i], iq[i].get(j).y); } for (; j<iq[i].size(); j++) { ArrayList<Pair> cur; if (!mapin.containsKey(iq[i].get(j).x)) { cur = new ArrayList<>(); } else cur = mapin.get(iq[i].get(j).x); cur.add(new Pair(i, iq[i].get(j).y)); mapin.put(iq[i].get(j).x, cur); } j = 0; for (j =0; j<ou[i].size(); j++) suffs[i] = Math.min(suffs[i], ou[i].get(j).y); int prev = (int) (1e9+1); for (j = ou[i].size()-1; j>=0; j--) { ArrayList<Pair> cur; if (!mapout.containsKey(ou[i].get(j).x)) cur = new ArrayList<>(); else cur = mapout.get(ou[i].get(j).x); cur.add(new Pair(i, prev)); mapout.put(ou[i].get(j).x, cur); prev = Math.min(prev, ou[i].get(j).y); } } long total = 0; for (int i =0 ; i<n; i++) total+=prefs[i]+suffs[i]; long copy = total; LQ: for (st = st+1; st<=end-1-k; st++) { if (mapin.containsKey(st)) { ArrayList<Pair> SF = mapin.get(st); for (Pair x : SF) { int s = x.x; int co = x.y; if (co<prefs[s]) { copy-=prefs[s]; copy+=co; prefs[s] = co; } } } if (mapout.containsKey(st+k)) { ArrayList<Pair> SF = mapout.get(st+k); for (Pair x: SF) { int s = x.x; if (x.y == 1e9+1) break LQ; copy+=(x.y-suffs[s]); suffs[s] = x.y; } } total = Math.min(total, copy); } pw.println(total); pw.close(); } void dfs(int v, Pair[] deep, int q) { used[v] = true; deep[v] = new Pair(q, v); for (Map.Entry<Integer, Long> x : tree[v].entrySet()) if (!used[x.getKey()]) dfs(x.getKey(), deep, q+1); } void dfs_combo(int cur, int it, int[] p, int[] q) { used[cur] = true; if (q[cur] == -1 || q[cur] == p[cur]); } void dfs(int cur, int[] mas, HashMap<Integer, Integer> numofv, HashMap<Integer, HashSet<Integer>> fullis, HashSet<Integer> currents, ArrayList<Integer>[] bin) { used[cur] = true; if (bin[mas[cur]].size() == 0) primes++; else { currents.add(mas[cur]); for (int i = 0; i<bin[cur].size(); i+=2) { int val = bin[cur].get(i); int how = bin[cur].get(i+1); if (numofv.containsKey(val)) { } } } } boolean upper(int a, int b) { return tin[a]<=tin[b] && tout[a]>=tout[b]; } int lca(int a, int b) { if (upper(a, b)) return a; if (upper(b, a)) return b; for (int i = logn-1; i>=0; i--) { if (!upper(up[a][i], b)) a = up[a][i]; } return up[a][0]; } int log_2(int g) { int s = 1; int ans = 0; while (g>=s) { ans++; s*=2; } return ans; } int dsu_get(int v) { return p[v] == v? v : (p[v] = dsu_get(p[v])); } void dsu_unit(int a, int b) { // a or dsu_get(a)? what's your choice? if (a!=b) { if (size[b]>size[a]) { int q = a; a = b; b = q; } p[b] = a; size[a]+=size[b]; } } int gcd (int a, int b) { if (b == 0) return a; else return gcd (b, a % b); } } class forStr { int a, b, size; forStr(int a1, int b1, int size1) { a = a1; b = b1; size = size1; } } class Pair implements Comparable<Pair> { int x, y; Pair(int x1, int y1) { x = x1; y = y1; } @Override public int compareTo(Pair o) { if (x == o.x) return Integer.compare(y, o.y); return Integer.compare(x, o.x); } } class Lines implements Comparable<Lines> { int pos; boolean start; Lines(int x, boolean f) { pos = x; start = f; } @Override public int compareTo(Lines o) { return Integer.compare(pos, o.pos); } } class Vertex implements Comparable<Vertex> { int b, c; int a; Vertex(int a1, int b1, int c1) { a = a1; b = b1; c = c1; } @Override public int compareTo(Vertex o) { return Long.compare(c, o.c); } } class FUCKINGVERTEX implements Comparable<FUCKINGVERTEX> { int a, b, c, d; FUCKINGVERTEX(int a1, int b1, int c1, int d1) { a = a1; b = b1; c = c1; d = d1; } @Override public int compareTo(FUCKINGVERTEX o) { return Integer.compare(a, o.a); } } class FScanner { StringTokenizer st; BufferedReader reader; FScanner(InputStreamReader isr) throws IOException { reader = new BufferedReader(isr); } String nextLine() throws IOException { return reader.readLine(); } String nextToken() throws IOException{ while (st == null || !st.hasMoreTokens()) { String s = reader.readLine(); if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) reader.read(); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } int[] iarr(int n) throws IOException { int[] mas = new int[n]; for (int i =0 ; i<n; i++) mas[i] = nextInt(); return mas; } double[] darr(int n) throws IOException { double[] mas = new double[n]; for (int i =0 ; i<n; i++) mas[i] = nextDouble(); return mas; } char[][] cmas2 (int n, int m) throws IOException { char[][] mas = new char[n][m]; for (int i =0 ; i<n; i++) mas[i] = nextLine().toCharArray(); return mas; } long[] larr(int n) throws IOException { long[] mas = new long[n]; for (int i =0 ; i<n; i++) mas[i] = nextLong(); return mas; } }
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; static inline void canhazfast() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } template <typename T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a / b; a %= b; swap(a, b); x0 -= q * x1; swap(x0, x1); y0 -= q * y1; swap(y0, y1); } x = x0; y = y0; return a; } static inline int ctz(unsigned x) { return __builtin_ctz(x); } static inline int ctzll(unsigned long long x) { return __builtin_ctzll(x); } static inline int clz(unsigned x) { return __builtin_clz(x); } static inline int clzll(unsigned long long x) { return __builtin_clzll(x); } static inline int popcnt(unsigned x) { return __builtin_popcount(x); } static inline int popcntll(unsigned long long x) { return __builtin_popcountll(x); } static inline int bsr(unsigned x) { return 31 ^ clz(x); } static inline int bsrll(unsigned long long x) { return 63 ^ clzll(x); } static multiset<int> car[100016], cde[100016]; static vector<pair<int, int> > arr[1000016], dep[1000016]; int n, m, k, tmx = 0; int in = 0, out = 0; long long cost = 0, ans = 1e18; bool ok = false; void add_arr(int i, int c) { if (car[i].empty()) ++in; else cost -= *car[i].begin(); car[i].insert(c); cost += *car[i].begin(); } void rem_dep(int i, int c) { assert(!cde[i].empty()); cost -= *cde[i].begin(); cde[i].erase(cde[i].find(c)); if (cde[i].empty()) --out; else cost += *cde[i].begin(); } int main() { canhazfast(); cin >> n >> m >> k; for (int i = 0; i < m; ++i) { int d, f, t, c; cin >> d >> f >> t >> c; tmx = max(tmx, d); if (t == 0) arr[d].emplace_back(f, c); else dep[d].emplace_back(t, c); } for (int t = tmx; t > k; --t) { for (const pair<int, int> &p : dep[t]) { int i = p.first, c = p.second; if (cde[i].empty()) ++out; else cost -= *cde[i].begin(); cde[i].insert(c); cost += *cde[i].begin(); } } for (int t = 1; t + k <= tmx; ++t) { if (in == n && out == n) { ok = true, ans = min(ans, cost); } for (pair<int, int> p : arr[t]) add_arr(p.first, p.second); for (pair<int, int> p : dep[t + k]) rem_dep(p.first, p.second); } cout << (ok ? ans : -1LL); 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 n, m, k, len, cnt1, cnt2; long long f[2000009], mn[2000009]; struct node { int t, x, val; } a[2000009], b[2000009]; bool cmp(node u, node v) { return u.t < v.t; } int main() { scanf("%d%d%d", &n, &m, &k); int i, j, x, y, z, t; for (i = 1; i <= m; i++) { scanf("%d%d%d%d", &t, &x, &y, &z); if (x) { a[++cnt1] = (node){t, x, z}; } else b[++cnt2] = (node){t, y, z}; } sort(a + 1, a + cnt1 + 1, cmp); sort(b + 1, b + cnt2 + 1, cmp); long long inf = (long long)2000001 * n; long long now = inf * n; for (i = 1; i <= n; i++) mn[i] = inf; for (i = 1000000, j = cnt2; i; i--) { for (; j && b[j].t == i; j--) { now -= mn[b[j].x]; ((mn[b[j].x] > (b[j].val)) ? mn[b[j].x] = (b[j].val) : 0); now += mn[b[j].x]; } f[i] = now; } long long ans = inf * n; now = ans; for (i = 1; i <= n; i++) mn[i] = inf; for (i = j = 1; i + k + 1 <= 1000000; i++) { for (; j <= cnt1 && a[j].t == i; j++) { now -= mn[a[j].x]; ((mn[a[j].x] > (a[j].val)) ? mn[a[j].x] = (a[j].val) : 0); now += mn[a[j].x]; } ans = min(ans, now + f[i + k + 1]); } 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
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx") using namespace std; struct edge { int first, t, c; }; vector<edge> a[1000001]; long long d[1000001]; long long rd[1000001]; long long mn[100500]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; ++i) { int d, first, t, c; cin >> d >> first >> t >> c; a[d].push_back(edge({first, t, c})); } for (int i = 1; i <= n; ++i) { mn[i] = 1e18; } long long sum = 0; for (int i = 1, cnt = 0; i <= 1e6; ++i) { if (cnt == n) { d[i] = sum; } else { d[i] = 1e18; } for (auto e : a[i]) { if (e.first) { if (mn[e.first] == 1e18) { cnt++; sum += e.c; mn[e.first] = e.c; } else if (mn[e.first] > e.c) { sum -= mn[e.first]; sum += e.c; mn[e.first] = e.c; } } } } for (int i = 1; i <= n; ++i) { mn[i] = 1e18; } sum = 0; for (int i = 1e6, cnt = 0; i >= 0; --i) { if (cnt == n) { rd[i] = sum; } else { rd[i] = 1e18; } for (auto e : a[i]) { if (e.first == 0) { if (mn[e.t] == 1e18) { cnt++; sum += e.c; mn[e.t] = e.c; } else if (mn[e.t] > e.c) { sum -= mn[e.t]; sum += e.c; mn[e.t] = e.c; } } } } long long ans = 1e18; for (int i = 1; i + k - 1 <= 1e6; ++i) { if (d[i] != 1e18) { if (rd[i] != 1e18) { ans = min(ans, d[i] + rd[i + k - 1]); } } } 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 N = 1e6 + 5; const long long inf = 1e12; int n, m, k; vector<pair<int, int> > a[N], b[N]; long long mn1[N], mn2[N]; long long an1[N], an2[N]; int main() { 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 == 0) { b[d].push_back({t - 1, c}); } else { a[d].push_back({f - 1, c}); } } for (int i = 0; i < N; ++i) { an1[i] = an2[i] = 1e18; mn1[i] = mn2[i] = inf; } int cnt = 0; long long cur_sum = 0; for (int i = 0; i < N; ++i) { for (auto it : a[i]) { int cost = it.second; int town = it.first; if (mn1[town] == inf) { cnt++; cur_sum += cost; mn1[town] = cost; } else if (mn1[town] > cost) { cur_sum -= mn1[town] - cost; mn1[town] = cost; } } if (cnt == n) { an1[i] = cur_sum; } } cnt = 0; cur_sum = 0; for (int i = N - 1; i >= 0; --i) { for (auto it : b[i]) { int cost = it.second; int town = it.first; if (mn2[town] == inf) { cnt++; cur_sum += cost; mn2[town] = cost; } else if (mn2[town] > cost) { cur_sum -= mn2[town] - cost; mn2[town] = cost; } } if (cnt == n) { an2[i] = cur_sum; } } long long ans = 1e18; for (int i = 0; i + k + 1 < N; ++i) { ans = min(ans, an1[i] + an2[i + k + 1]); } if (ans == 1e18) 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; int n, m, k, i, x, y, h[200010], t; long long ans, Ans, g[200010], f[200010]; struct mjj { int x, y, z; } a[200010]; bool cmp(mjj a, mjj b) { return a.x < b.x; } int main() { scanf("%d%d%d", &n, &m, &k); for (i = 1; i <= m; i++) { scanf("%d%d%d%d", &a[i].x, &x, &y, &a[i].z); if (x == 0) a[i].y = y; else a[i].y = -x; } Ans = 0; Ans = 1e18; sort(a + 1, a + m + 1, cmp); for (i = 1; i <= m; i++) h[i] = a[i].x; for (i = 1; i <= n; i++) f[i] = 1e12, ans += f[i]; g[0] = 1e18; for (i = 1; i <= m; i++) if (a[i].y < 0) { ans -= f[-a[i].y]; f[-a[i].y] = min(f[-a[i].y], 1ll * a[i].z); ans += f[-a[i].y]; g[i] = ans; } else g[i] = g[i - 1]; memset(f, 63, sizeof(f)); ans = 0; for (i = 1; i <= n; i++) f[i] = 1e12, ans += f[i]; for (i = m; i >= 1; i--) if (a[i].y > 0) { ans -= f[a[i].y]; f[a[i].y] = min(f[a[i].y], 1ll * a[i].z); ans += f[a[i].y]; t = lower_bound(h + 1, h + m + 1, a[i].x - k) - h - 1; if (t) Ans = min(Ans, ans + g[t]); } if (Ans >= 1e12) puts("-1"); else 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; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1}; int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2}; int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1}; long long bigmod(long long a, long long b, long long c) { if (b == 0) return 1 % c; long long x = bigmod(a, b / 2, c); x = (x * x) % c; if (b % 2 == 1) x = (x * a) % c; return x; } long long poww(long long a, long long b) { if (b == 0) return 1; long long x = poww(a, b / 2); x = x * x; if (b % 2 == 1) x = (x * a); return x; } long long mod_inverse(long long a, long long mod) { return bigmod(a, mod - 2, mod); } const int M = 1e6 + 5; vector<pair<long long, long long> > come[M], go[M]; long long bward[M], fward[M], sum[M]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, k; cin >> n >> m >> k; for (int i = 0; i < (m); i++) { int d, f, t, c; cin >> d >> f >> t >> c; if (t == 0) come[d].push_back({f, c}); else go[d].push_back({t, c}); } long long ltot = 0, rtot = 0; for (int i = 1; i <= n; i++) { bward[i] = fward[i] = 1e13; ltot += fward[i]; } rtot = ltot; for (int i = 1e6; i >= 1; i--) { for (auto x : go[i]) { int u = x.first; long long c = x.second; if (bward[u] > c) { rtot -= bward[u]; bward[u] = c; rtot += c; } } sum[i] = rtot; } long long res = 1e18; for (int i = 1; i <= 1e6; i++) { for (auto x : come[i]) { int u = x.first; long long c = x.second; if (fward[u] > c) { ltot -= fward[u]; fward[u] = c; ltot += c; } } if (i + k + 1 <= 1e6) res = min(res, ltot + sum[i + k + 1]); } if (res >= 1e13) 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; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } bool isin( const pair<long long, tuple<long long, long long, long long> >& elem) { return get<1>(elem.second) == 0; } bool isout( const pair<long long, tuple<long long, long long, long long> >& elem) { return get<0>(elem.second) == 0; } long long INF = 1e18; int main() { long long n, m, k; cin >> n >> m >> k; if (m == 0) { cout << -1; return 0; } vector<pair<long long, tuple<long long, long long, long long> > > flights; long long d, f, t, c; for (long long i = 0; i < m; i++) { cin >> d >> f >> t >> c; flights.push_back( make_pair(d, tuple<long long, long long, long long>(f, t, c))); } sort(flights.begin(), flights.end()); long long days_cnt = flights.back().first + 2; vector<bool> in(n + 1, false); vector<bool> out(n + 1, false); vector<long long> totalin(days_cnt, 0); vector<long long> totalout(days_cnt, 0); vector<long long> pre_min(days_cnt, INF); vector<long long> suf_min(days_cnt, INF); vector<long long> p_min(n + 1, INF); vector<long long> p_min_rev(n + 1, INF); long long cur = 0; for (long long i = 0; i < flights.size(); i++) { auto elem = flights[i]; long long d = elem.first; long long f = get<0>(elem.second); long long t = get<1>(elem.second); long long c = get<2>(elem.second); long long p = f; if (p == 0) { continue; } while (cur < d) { cur++; totalin[cur] = totalin[cur - 1]; pre_min[cur] = pre_min[cur - 1]; } if (!in[p]) { totalin[d]++; in[p] = true; p_min[p] = c; pre_min[d] = (pre_min[d] == INF ? 0 : pre_min[d]) + p_min[p]; } else { long long old_min = p_min[p]; p_min[p] = min(p_min[p], c); if (old_min > p_min[p]) { pre_min[d] -= old_min; pre_min[d] += p_min[p]; } } } while (cur < days_cnt) { cur++; if (cur == days_cnt) continue; pre_min[cur] = pre_min[cur - 1]; totalin[cur] = totalin[cur - 1]; } cur = days_cnt - 1; for (long long i = flights.size() - 1; i >= 0; i--) { auto elem = flights[i]; long long d = elem.first; long long f = get<0>(elem.second); long long t = get<1>(elem.second); long long c = get<2>(elem.second); long long p = t; if (p == 0) { continue; } while (cur > d) { cur--; totalout[cur] = totalout[cur + 1]; suf_min[cur] = suf_min[cur + 1]; } if (!out[p]) { totalout[d]++; out[p] = true; p_min_rev[p] = c; suf_min[d] = (suf_min[d] == INF ? 0 : suf_min[d]) + p_min_rev[p]; } else { long long old_min = p_min_rev[p]; p_min_rev[p] = min(p_min_rev[p], c); if (old_min > p_min_rev[p]) { suf_min[d] -= old_min; suf_min[d] += p_min_rev[p]; } } } while (cur >= 0) { cur--; if (cur < 0) continue; suf_min[cur] = suf_min[cur + 1]; totalout[cur] = totalout[cur + 1]; } long long ans = INF; for (long long i = 0; i < suf_min.size(); i++) { if (i + k + 1 >= totalout.size()) continue; if (totalin[i] >= n && totalout[i + k + 1] >= n) { ans = min(ans, pre_min[i] + suf_min[i + k + 1]); } } cout << (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; int n, m, k; long long dp[2][1000005]; long long need[2][1000005]; struct node { int d, f, t, c; } a[100005]; int cmp(node u, node v) { return u.d < v.d; } const long long inf = 0x3f3f3f3f3f3f3f3f; int main() { int i; cin >> n >> m >> k; for (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); memset(need, -1, sizeof(need)); memset(dp, inf, sizeof(dp)); long long sum = 0, now = 0; for (i = 0; i < m; i++) { if (a[i].f) { if (need[0][a[i].f] == -1) { now++; sum += a[i].c; need[0][a[i].f] = a[i].c; } else if (a[i].c < need[0][a[i].f]) { sum += a[i].c - need[0][a[i].f]; need[0][a[i].f] = a[i].c; } if (now == n) { dp[0][a[i].d] = min(dp[0][a[i].d], sum); } } } now = 0, sum = 0; for (i = m - 1; i >= 0; i--) { if (a[i].t) { if (need[1][a[i].t] == -1) { now++; sum += a[i].c; need[1][a[i].t] = a[i].c; } else if (a[i].c < need[1][a[i].t]) { sum += a[i].c - need[1][a[i].t]; need[1][a[i].t] = a[i].c; } if (now == n) { dp[1][a[i].d] = min(dp[1][a[i].d], sum); } } } for (i = 1; i <= 1000000; i++) { dp[0][i] = min(dp[0][i], dp[0][i - 1]); } for (i = 1000000; i >= 0; i--) { dp[1][i] = min(dp[1][i], dp[1][i + 1]); } long long ans = inf; for (i = 0; i + k <= 1000000; i++) { ans = min(ans, dp[0][i] + dp[1][i + k + 1]); } if (ans == inf) 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
R=lambda :map(int,input().split()) n,m,k=R() F,T=[],[] ans=int(1e12) for i in range(m): d,f,t,c=R() if f:F.append((d,f,c)) else:T.append((-d,t,c)) for p in [F,T]: cost=[ans]*(n+1) s=n*ans q=[] p.sort() for d,t,c in p: #print(p) if c<cost[t]: #print(c,cost[t]) s+=c-cost[t] #print(s) cost[t]=c if s<ans: q.append((s,d)) p.clear() #print(q) p+=q #print(p) s,t=ans,(0,0) #print(F,T) for f in F: while f: if f[1]+t[1]+k<0:s=min(s,f[0]+t[0]) elif T: #print(T) t=T.pop() #print(T) # print(t) continue #print(f) f=0 #print(f) print(s if s<ans else -1) # Made By Mostafa_Khaled
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.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Egor Kulikov ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public static final int DAYS = 1000000; public static final long INFTY = 1000_000_000_000L; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int m = in.readInt(); int k = in.readInt(); int[] d = new int[m]; int[] f = new int[m]; int[] t = new int[m]; int[] c = new int[m]; IOUtils.readIntArrays(in, d, f, t, c); MiscUtils.decreaseByOne(d, f, t); int[] first = ArrayUtils.createArray(DAYS, -1); int[] next = new int[m]; for (int i = 0; i < m; i++) { if (t[i] == -1) { next[i] = first[d[i]]; first[d[i]] = i; } } long[] inbound = new long[DAYS]; long[] current = ArrayUtils.createArray(n, INFTY); long result = INFTY * n; for (int i = 0; i < DAYS; i++) { for (int j = first[i]; j != -1; j = next[j]) { if (c[j] < current[f[j]]) { result -= current[f[j]]; current[f[j]] = c[j]; result += c[j]; } } inbound[i] = result; } Arrays.fill(first, -1); for (int i = 0; i < m; i++) { if (f[i] == -1) { next[i] = first[d[i]]; first[d[i]] = i; } } Arrays.fill(current, INFTY); result = INFTY * n; long[] outbound = new long[DAYS]; for (int i = DAYS - 1; i >= 0; i--) { for (int j = first[i]; j != -1; j = next[j]) { if (c[j] < current[t[j]]) { result -= current[t[j]]; current[t[j]] = c[j]; result += c[j]; } } outbound[i] = result; } long answer = Long.MAX_VALUE; for (int i = k + 1; i < DAYS; i++) { answer = Math.min(answer, inbound[i - k - 1] + outbound[i]); } if (answer >= INFTY) { answer = -1; } out.printLine(answer); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(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 readInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int 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 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; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class MiscUtils { public static void decreaseByOne(int[]... arrays) { for (int[] array : arrays) { for (int i = 0; i < array.length; i++) { array[i]--; } } } } static class IOUtils { public static void readIntArrays(InputReader in, int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = in.readInt(); } } } } static 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 close() { writer.close(); } public void printLine(long i) { writer.println(i); } } static class ArrayUtils { public static int[] createArray(int count, int value) { int[] array = new int[count]; Arrays.fill(array, value); return array; } public static long[] createArray(int count, long value) { long[] array = new long[count]; Arrays.fill(array, value); return array; } } }
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; using lint = long long; using ii = pair<int, int>; using il = pair<int, lint>; using ll = pair<lint, lint>; using ti = tuple<int, int, int>; const int N = 1e5 + 5; const int D = 1e6 + 5; const lint mod = 1e9 + 7; vector<il> in_air[D]; vector<il> out_air[D]; int n, m, k; void input() { scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < m; i++) { int x, y, z; scanf("%d%d%d", &x, &y, &z); lint c; scanf("%lld", &c); if (y == 0) { out_air[x].push_back(il(z, c)); } if (z == 0) { in_air[x].push_back(il(y, c)); } } } lint in_cost[N]; lint in_sum; int in_num; lint out_cost[N]; lint out_sum; int out_num; lint d1[D]; lint d2[D]; const lint inf = 1e18 + 5; void push(int day, vector<il> air[], lint cost[], lint& sum, int& num) { for (il p : air[day]) { int typ = p.first; lint cst = p.second; if (cost[typ] == inf) { num++; cost[typ] = cst; sum += cst; } else { sum -= cost[typ]; cost[typ] = min(cost[typ], cst); sum += cost[typ]; } } } void go() { fill(in_cost, in_cost + N, inf); fill(out_cost, out_cost + N, inf); fill(d1, d1 + D, inf); fill(d2, d2 + D, inf); for (int i = 1; i <= D - 2; i++) { push(i, in_air, in_cost, in_sum, in_num); if (in_num == n) { d1[i] = in_sum; } else { d1[i] = inf; } d1[i] = min(d1[i], d1[i - 1]); } for (int i = D - 2; i >= 1; i--) { push(i, out_air, out_cost, out_sum, out_num); if (out_num == n) { d2[i] = out_sum; } else { d2[i] = inf; } d2[i] = min(d2[i], d2[i + 1]); } lint ans = inf; for (lint i = D - 2; i >= 1; i--) { if (i + k + 1 < D) { ans = min(ans, d1[i] + d2[i + k + 1]); } } if (ans == inf) { puts("-1"); } else { printf("%lld\n", ans); } } int main() { input(); go(); 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 D854 { public static void main(String[] args) throws IOException { IO io = new IO(System.in); int n = io.nextInt(); int m = io.nextInt(); int k = io.nextInt(); ArrayList<F> arrive = new ArrayList<>(); ArrayList<F> depart = new ArrayList<>(); for (int i = 0; i < m; i++) { F f = new F(io.nextInt(),io.nextInt(),io.nextInt(),io.nextInt()); if (f.to == 0) { arrive.add(f); } else { depart.add(f); } } Collections.sort(arrive); Collections.sort(depart); Collections.reverse(depart); int[][] c = new int[n+1][2]; for (int i = 0; i <= n; i++) { Arrays.fill(c[i], Integer.MAX_VALUE); } int seen_a = 0; int seen_d = 0; ArrayList<P> cost_a = new ArrayList<>(); ArrayList<P> cost_d = new ArrayList<>(); // sum: cost of everyone arriving at cur time long sum = Long.MAX_VALUE; for(F f : arrive) { if (c[f.from][0] == Integer.MAX_VALUE) { seen_a++; c[f.from][0] = Math.min(f.cost, c[f.from][0]); if (seen_a==n) { sum = 0; for (int i = 1; i <= n; i++) { sum += c[i][0]; } } } sum -= c[f.from][0]; c[f.from][0] = Math.min(f.cost, c[f.from][0]); sum += c[f.from][0]; if (seen_a==n) { // can arrive at time f.t for cost sum cost_a.add(new P(f.t,sum)); } } // now build departures the same way... for (F f : depart) { if (c[f.to][1] == Integer.MAX_VALUE) { seen_d++; c[f.to][1] = Math.min(f.cost, c[f.to][1]); if (seen_d==n) { sum = 0; for (int i = 1; i <= n; i++) { sum += c[i][1]; } } } sum -= c[f.to][1]; c[f.to][1] = Math.min(c[f.to][1], f.cost); sum += c[f.to][1]; if (seen_d==n) { cost_d.add(new P(f.t,sum)); } } long best = Long.MAX_VALUE; Collections.reverse(cost_d); int ind = 0; // Match arrival with departure for (P pa : cost_a) { int tar = pa.time + k + 1; while (ind < cost_d.size() && cost_d.get(ind).time < tar) { ind++; } if (ind == cost_d.size()) break; P pb = cost_d.get(ind); // what is cost?? best = Math.min(best, pa.cost + pb.cost); } if (best == Long.MAX_VALUE) { io.println(-1); } else { io.println(best); } io.close(); } static class P { int time; long cost; P(int _t, long _c) { time = _t; cost = _c; } } static class F implements Comparable<F> { public int t, from, to, cost; F(int _t, int _f, int _to, int _c) { t = _t; from = _f; to = _to; cost = _c; } public int compareTo(F o) { return Integer.compare(this.t, o.t); } } static class IO extends PrintWriter { static BufferedReader r; static StringTokenizer t; public IO(InputStream i) { super(new BufferedOutputStream(System.out)); r = new BufferedReader(new InputStreamReader(i)); t = new StringTokenizer(""); } public String next() throws IOException { while (!t.hasMoreTokens()) { t = new StringTokenizer(r.readLine()); } return t.nextToken(); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public 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 long long int N = 1e6 + 5; const long long int M = 1e5 + 5; const long long int mod = 1e9 + 7; const long long int m1 = 1e9 + 7; const long long int m2 = 1e9 + 9; const long long int p1 = 402653189; const long long int p2 = 1610612741; const int LN = 61; long long int powermodm(long long int x, long long int n, long long int M) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long int power(long long int _a, long long int _b) { long long int _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a); _b /= 2; _a = (_a * _a); } return _r; } long long int gcd(long long int a, long long int b) { if (a == 0) return b; return gcd(b % a, a); } long long int lcm(long long int a, long long int b) { return (max(a, b) / gcd(a, b)) * min(a, b); } long long int dx[4] = {-1, 1, 0, 0}; long long int dy[4] = {0, 0, 1, -1}; long long int pw[LN], fact[N], invfact[N]; void pre() { fact[0] = 1; for (int i = 0; i < LN; i++) pw[i] = power(2, i); for (int i = 1; i < N; i++) fact[i] = (i * fact[i - 1]) % mod; for (int i = 0; i < N; i++) invfact[i] = powermodm(fact[i], mod - 2, mod); } long long int d[N], f[N], t[N], c[N]; long long int pref[N], suff[N]; vector<long long int> in[N], out[N]; long long int mi[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int i, j; long long int n, m, k; cin >> n >> m >> k; for (i = 0; i < m; i++) { cin >> d[i] >> f[i] >> t[i] >> c[i]; if (f[i] == 0) out[d[i]].push_back(i); else in[d[i]].push_back(i); } long long int currans = 0; for (i = 1; i <= n; i++) mi[i] = 1e12; currans = n * 1e12; for (i = 1; i < N; i++) { for (j = 0; j < in[i].size(); j++) { long long int idx = in[i][j]; currans -= mi[f[idx]]; mi[f[idx]] = min(mi[f[idx]], c[idx]); currans += mi[f[idx]]; } pref[i] = currans; } for (i = 1; i <= n; i++) mi[i] = 1e12; currans = n * 1e12; for (i = N - 1; i >= 1; i--) { for (j = 0; j < out[i].size(); j++) { long long int idx = out[i][j]; currans -= mi[t[idx]]; mi[t[idx]] = min(mi[t[idx]], c[idx]); currans += mi[t[idx]]; } suff[i] = currans; } long long int ans = 1e11 + 5; for (i = 2; i + k + 1 < N; i++) { ans = min(ans, pref[i - 1] + suff[i + k]); } if (ans == 1e11 + 5) 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; long long gcd(long long n1, long long n2) { if (n2 == 0) return n1; if (n1 % n2 == 0) return n2; return gcd(n2, n1 % n2); } long long powmod(long long base, long long exponent) { if (exponent < 0) exponent += 1000000007LL - 1; long long ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % 1000000007LL; base = (base * base) % 1000000007LL; exponent /= 2; } return ans; } vector<pair<long long, pair<long long, long long>>> v1, v2; long long arr[1005005], cost[1005005], cost2[1005005], size[1005005], size2[1005005], dep[1005005]; 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 < 1005005; i++) { arr[i] = 1000000007LL; cost[i] = n * 1000000007LL; cost2[i] = n * 1000000007LL; dep[i] = 1000000007LL; } for (int i = 0; i < m; i++) { long long d, f, t, c; cin >> d >> f >> t >> c; if (t == 0) { v1.push_back(make_pair(d, make_pair(f, c))); } else { v2.push_back(make_pair(d, make_pair(t, c))); } } int prev = 0; sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); reverse(v2.begin(), v2.end()); for (int i = 0; i < v1.size(); i++) { long long c = v1[i].second.second, f = v1[i].second.first, d = v1[i].first; if (arr[f] > c) { cost[d] = cost[prev] - arr[f] + c; if (arr[f] == 1000000007LL) { size[d] = size[prev] + 1; prev = d; } else size[d] = size[prev]; arr[f] = c; prev = d; } } prev = 0; for (int i = 0; i < v2.size(); i++) { long long c = v2[i].second.second, f = v2[i].second.first, d = v2[i].first; if (dep[f] > c) { cost2[d] = cost2[prev] - dep[f] + c; if (dep[f] == 1000000007LL) { size2[d] = size2[prev] + 1; prev = d; } else size2[d] = size2[prev]; dep[f] = c; prev = d; } } for (int i = 1; i <= 1000000; i++) { cost[i] = min(cost[i], cost[i - 1]); size[i] = max(size[i], size[i - 1]); } for (int i = 1000000; i > 0; i--) { cost2[i] = min(cost2[i], cost2[i + 1]); size2[i] = max(size2[i], size2[i + 1]); } long long ans = 1e12; for (int i = 1; i <= 1000000 - k + 5; i++) { if (size[i] == n && size2[i + k + 1] == n) ans = min(ans, cost[i] + cost2[i + k + 1]); } if (ans > 2000002LL * n) cout << -1 << '\n'; else 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
import java.util.*; import java.io.*; import java.math.*; public class Main implements Runnable{ FastScanner sc; PrintWriter pw; final 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 long nlo() { return Long.parseLong(next()); } 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 ni() { return Integer.parseInt(next()); } public String nli() { String line = ""; if (st.hasMoreTokens()) line = st.nextToken(); else try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } while (st.hasMoreTokens()) line += " " + st.nextToken(); return line; } public double nd() { return Double.parseDouble(next()); } } public static void main(String[] args) throws Exception { new Thread(null,new Main(),"codeforces",1<<28).start(); } public void run() { sc=new FastScanner(); pw=new PrintWriter(System.out); try{solve();} catch(Exception e) {System.out.println(e);} pw.flush(); pw.close(); } public long gcd(long a,long b) { return b==0L?a:gcd(b,a%b); } public long ppow(long a,long b,long mod) { if(b==0L) return 1L; long tmp=1; while(b>1L) { if((b&1L)==1) tmp*=a; a*=a; a%=mod; tmp%=mod; b>>=1; } return (tmp*a)%mod; } public int gcd(int x,int y) { return y==0?x:gcd(y,x%y); } ////////////////////////////////// ///////////// LOGIC /////////// //////////////////////////////// public int k; public class Pair{ int a; long c; Pair(int b,long z) { a=b; c=z; } } public void solve() throws Exception { int n=sc.ni(); int m=sc.ni(); int k=sc.ni(); HashMap<Integer,ArrayList<Pair>> arv,dep; arv=new HashMap(); dep=new HashMap(); for(int i=0;i<m;i++) { int a=sc.ni(); int b=sc.ni(); int c=sc.ni(); int d=sc.ni(); if(b==0) { if(!dep.containsKey(a)) dep.put(a,new ArrayList()); dep.get(a).add(new Pair(c,d)); } else { if(!arv.containsKey(a)) arv.put(a,new ArrayList()); arv.get(a).add(new Pair(b,d)); } } long[] prr=new long[1000001]; long[] srr=new long[1000001]; Arrays.fill(prr,-1); Arrays.fill(srr,-1); HashMap<Integer,Long> map=new HashMap(); long tmp=0; for(int i=1;i<=1000000;i++) { if(arv.containsKey(i)) { for(Pair p:arv.get(i)) { if(map.containsKey(p.a)) { if(map.get(p.a)>p.c) { tmp-=map.get(p.a); tmp+=p.c; map.put(p.a,p.c); } } else { map.put(p.a,p.c); tmp+=p.c; } } } if(map.size()==n) prr[i]=tmp; } map=new HashMap(); tmp=0; for(int i=1000000;i>0;i--) { if(dep.containsKey(i)) { for(Pair p:dep.get(i)) { if(map.containsKey(p.a)) { if(map.get(p.a)>p.c) { tmp-=map.get(p.a); tmp+=p.c; map.put(p.a,p.c); } } else { map.put(p.a,p.c); tmp+=p.c; } } } if(map.size()==n) srr[i]=tmp; } long ans=-1; for(int i=2;i<=1000000;i++) { if(i+k<=1000000&&prr[i-1]!=-1&&srr[i+k]!=-1) { if(ans==-1) ans=prr[i-1]+srr[i+k]; else ans=Math.min(ans,prr[i-1]+srr[i+k]); } } pw.println(ans); } }
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 maxn = 100005; struct node { int day, s, t, cost; bool operator<(const node &r) const { return day < r.day; } } e[maxn], g[maxn]; int vis[maxn]; long long cheap[1000000], ans = -1; int main() { int n, m, k, p1 = 0, p2 = 0; scanf("%d%d%d", &n, &m, &k); memset(cheap, -1, sizeof(cheap)); for (int i = 0; i < m; i++) { node tmp; scanf("%d%d%d%d", &tmp.day, &tmp.s, &tmp.t, &tmp.cost); if (tmp.s == 0 && tmp.t == 0) continue; if (tmp.t == 0) e[p1++] = tmp; else g[p2++] = tmp; } sort(e, e + p1); sort(g, g + p2); int sum = 0; long long play = 0, res = 0; for (int i = 0; i < p1; i++) { int d = e[i].day, s = e[i].s, t = e[i].t, c = e[i].cost; if (vis[s] == 0) { sum++; vis[s] = c; play += c; } else { if (c < vis[s]) { play -= vis[s] - c; vis[s] = c; } } if (sum == n) cheap[d] = play; } for (int i = 1; i < maxn * 10; i++) { if (cheap[i - 1] != -1) { if (cheap[i] == -1) cheap[i] = cheap[i - 1]; } } memset(vis, 0, sizeof(vis)); sum = 0; play = 0; for (int i = p2 - 1; i >= 0; i--) { int d = g[i].day, s = g[i].s, t = g[i].t, c = g[i].cost; if (d - k - 1 < 0 || cheap[d - k - 1] == -1) break; if (vis[t] == 0) { sum++; vis[t] = c; play += c; } else { if (c < vis[t]) { play -= vis[t] - c; vis[t] = c; } } if (sum == n) { if (ans == -1 || play + cheap[d - k - 1] < ans) ans = play + cheap[d - k - 1]; } } 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
import java.util.*; public class JuryMeeting { private ArrayList <Node> arriving, departing; private int n, m, k; private long [] costOfArriving, costOfDeparting; public void minCostsArriving() { Map<Integer,Integer> mp=new HashMap<Integer,Integer>(); long cost=0; for(int i=0; i<arriving.size(); i++) { if(mp.get(arriving.get(i).from)==null) { cost+=arriving.get(i).cost; mp.put(arriving.get(i).from, arriving.get(i).cost); }else if(mp.get(arriving.get(i).from)>arriving.get(i).cost) { cost-=mp.get(arriving.get(i).from); cost+=arriving.get(i).cost; mp.put(arriving.get(i).from, arriving.get(i).cost); } if(mp.size()==n) { costOfArriving[i]=cost; } } } public void minCostsDeparting() { Map<Integer,Integer> mp=new HashMap<Integer,Integer>(); long cost=0; for(int i=departing.size()-1; i>=0; i--) { if(mp.get(departing.get(i).to)==null) { cost+=departing.get(i).cost; mp.put(departing.get(i).to, departing.get(i).cost); }else if(mp.get(departing.get(i).to)>departing.get(i).cost) { cost-=mp.get(departing.get(i).to); cost+=departing.get(i).cost; mp.put(departing.get(i).to, departing.get(i).cost); } if(mp.size()==n) { costOfDeparting[i]=cost; } } } public long solve() { TreeMap<Integer,Integer> tree=new TreeMap<Integer,Integer>(); long answer=Long.MAX_VALUE; minCostsArriving(); minCostsDeparting(); /*for(int i=0; i<costOfDeparting.length;i++){ System.out.print(costOfDeparting[i]+" "); } System.out.println(); for(int i=0; i<costOfArriving.length;i++){ System.out.print(costOfArriving[i]+" "); } System.out.println();*/ for(int i=0; i<arriving.size(); i++) { tree.put(arriving.get(i).day, i); } for(int i=departing.size()-1; i>=0; i--) { if(costOfDeparting[i]!=Long.MAX_VALUE) { Map.Entry<Integer, Integer> e=tree.lowerEntry(departing.get(i).day-k); if(e!=null && costOfArriving[e.getValue()]!=Long.MAX_VALUE) { answer=Math.min(answer, costOfArriving[e.getValue()]+costOfDeparting[i]); } } } if(answer==Long.MAX_VALUE) { return -1; }else { return answer; } } public JuryMeeting() { Scanner sc=new Scanner(System.in); arriving=new ArrayList<Node>(); departing=new ArrayList<Node>(); n=sc.nextInt(); m=sc.nextInt(); k=sc.nextInt(); for(int i=0; i<m; i++) { int d, f, t, c; d=sc.nextInt(); f=sc.nextInt(); t=sc.nextInt(); c=sc.nextInt(); if(t==0) { arriving.add(new Node(d,f,t,c)); }else { departing.add(new Node(d,f,t,c)); } } costOfArriving = new long [m]; costOfDeparting = new long[m]; for(int i=0; i<m; i++) { costOfArriving[i]=Long.MAX_VALUE; costOfDeparting[i]=Long.MAX_VALUE; } Collections.sort(arriving); Collections.sort(departing); sc.close(); } public static void main(String[] args) { System.out.println(new JuryMeeting().solve()); } } class Node implements Comparable <Node>{ public int day, from, to, cost; public Node(int d, int f, int t, int c) { day=d; from=f; to=t; cost=c; } public int compareTo(Node o) { if(day<o.day) { return -1; }else { return 1; } } }
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> #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,avx,avx2,abm,mmx") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } ll n, m, k; struct edge { ll from, to, cost; edge() {} edge(ll _from, ll _to, ll _cost) { from = _from, to = _to, cost = _cost; } }; const ll MAXD = 1e6 + 10; vector<edge> have[MAXD]; ll cost[MAXD][2]; const ll INF = 1e18; void build() { vector<ll> used(n, INF); ll ans = INF; ll cnt = n; bool flag = false; for (ll i = 0; i < MAXD; i++) { for (auto ed : have[i]) { if (ed.to != 0) continue; if (used[ed.from - 1] == INF) cnt--; if (cnt == 0) ans -= used[ed.from - 1]; chkmin(used[ed.from - 1], ed.cost); if (cnt == 0) ans += used[ed.from - 1]; } if (cnt == 0 && !flag) { flag = true; ans = 0; for (auto i : used) ans += i; } cost[i][0] = ans; } used.assign(n, INF); ans = INF; cnt = n; flag = false; for (ll i = MAXD - 1; i >= 0; i--) { for (auto ed : have[i]) { if (ed.from != 0) continue; if (used[ed.to - 1] == INF) cnt--; if (cnt == 0) ans -= used[ed.to - 1]; chkmin(used[ed.to - 1], ed.cost); if (cnt == 0) ans += used[ed.to - 1]; } if (cnt == 0 && !flag) { flag = true; ans = 0; for (auto i : used) ans += i; } cost[i][1] = ans; } } void solve() { cin >> n >> m >> k; for (ll i = 0; i < m; i++) { ll d, f, t, c; cin >> d >> f >> t >> c; have[d].emplace_back(f, t, c); } build(); ll ans = INF; for (ll i = 0; i + k + 1 < MAXD; i++) { chkmin(ans, cost[i][0] + cost[i + k + 1][1]); } if (ans == INF) ans = -1; cout << ans << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1; while (t--) solve(); 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 = 1000000000000ll; int N, M, K; long long L[1000005], Min[1000005], R[1000005]; vector<pair<int, int>> Go[1000005], Return[1000005]; int main() { cin >> N >> M >> K; for (int i = (1); i <= (M); i++) { int a, b, c, d; scanf("%d %d %d %d", &d, &a, &b, &c); if (a == 0) Return[d].push_back({b, c}); else Go[d].push_back({a, c}); } long long best = INF * N; for (int i = (1); i <= (N); i++) Min[i] = INF; for (int i = (0); i <= (1000000); i++) { for (pair<int, int> p : Go[i]) { int jury = p.first, cost = p.second; if (cost < Min[jury]) { best -= Min[jury] - cost; Min[jury] = cost; } } L[i] = best; } best = INF * N; for (int i = (1); i <= (N); i++) Min[i] = INF; for (int i = (1000000); i >= (0); i--) { for (pair<int, int> p : Return[i]) { int jury = p.first, cost = p.second; if (cost < Min[jury]) { best -= Min[jury] - cost; Min[jury] = cost; } } R[i] = best; } long long ans = INF * N; for (int l = (1); l <= (1000000 - K); l++) { int r = l + K - 1; ans = min(ans, L[l - 1] + R[r + 1]); } if (ans < INF) cout << ans; else cout << "-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 int maxn = 1e6 + 10; const long long oo = 1e12; const double eps = 1e-9; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m, k; cin >> n >> m >> k; vector<vector<pair<long long, long long>>> A( n, vector<pair<long long, long long>>()); vector<vector<pair<long long, long long>>> B( n, vector<pair<long long, long long>>()); for (int i = 0; i < m; ++i) { long long d, f, t, c; cin >> d >> f >> t >> c; if (f == 0) B[t - 1].push_back({-d, c}); else A[f - 1].push_back({d, c}); } for (int i = 0; i < n; ++i) { sort(A[i].begin(), A[i].end()); sort(B[i].begin(), B[i].end()); } long long mn = 0; long long mx = maxn; for (int i = 0; i < n; ++i) { if (B[i].empty() || A[i].empty()) { cout << -1 << endl; return 0; } mn = max(mn, A[i][0].first); mx = min(mx, -B[i][0].first); } vector<long long> V(n); vector<long long> dp1(maxn); vector<long long> dp2(maxn); for (int i = 0; i < n; ++i) { long long cur = oo; int lastd = -1; for (auto x : A[i]) { int d = x.first; long long c = x.second; if (lastd == d) continue; if (c < cur) { dp1[d] += c; if (cur != oo) dp1[d] -= cur; cur = c; lastd = d; } } } for (int i = 0; i < n; ++i) { long long cur = oo; int lastd = -1; for (auto x : B[i]) { int d = -x.first; long long c = x.second; if (lastd == d) continue; if (c < cur) { dp2[d] += c; if (cur != oo) dp2[d] -= cur; cur = c; lastd = d; } } } for (int i = 1; i < maxn; ++i) dp1[i] += dp1[i - 1]; for (int i = maxn - 2; i >= 0; --i) dp2[i] += dp2[i + 1]; long long ans = oo; for (int i = mn; i + k + 1 < maxn && i + k + 1 <= mx; ++i) { ans = min(ans, dp1[i] + dp2[i + k + 1]); } if (ans >= oo) 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; long long add(long long a, long long b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } long long mul(long long a, long long b) { return ((a % 1000000007) * (b % 1000000007)) % 1000000007; } long long binary_expo(long long base, long long expo) { long long res = 1; while (expo) { if (expo % 2) { res = mul(res, base); expo /= 2; } base = mul(base, base); } return res; } vector<pair<long long, pair<long long, long long> > > v[1000002]; const long long N = 100002; long long Arr[N], dep[N]; multiset<long long> S1[N], S2[N]; long long n; long long BIT1[N], BIT2[N]; void update(long long BIT[], long long idx, long long val) { while (idx <= n) { BIT[idx] += val; idx += (idx & (-idx)); } } long long query(long long BIT[], long long idx) { long long sum = 0; while (idx > 0) { sum += BIT[idx]; idx -= (idx & (-idx)); } return sum; } void solve() { long long m, k; cin >> n >> m >> k; for (long long i = 1; i <= n; i++) { Arr[i] = 1e12; dep[i] = -1e12; } for (long long i = 0; i < m; i++) { long long d, f, t, c; cin >> d >> f >> t >> c; v[d].push_back({f, {t, c}}); if (t == 0) { Arr[f] = min(Arr[f], d); } else { dep[t] = max(dep[t], d); } } long long minn = 1e12; long long maxx = -1e12; for (long long i = 1; i <= n; i++) { if (Arr[i] == 1e12 or dep[i] == -1e12) { cout << "-1" << endl; return; } maxx = max(maxx, Arr[i]); minn = min(minn, dep[i]); } long long ms = maxx + 1; long long me = minn - 1; if (me - ms + 1 < k) { cout << "-1" << endl; return; } for (long long i = 1000000; i > (ms + k - 1); i--) { for (long long j = 0; j < v[i].size(); j++) { pair<long long, pair<long long, long long> > p = v[i][j]; if (p.first == 0) { long long val1 = 0; if (S1[p.second.first].size() != 0) { val1 = *(S1[p.second.first].begin()); } S1[p.second.first].insert(p.second.second); long long val = *(S1[p.second.first].begin()); update(BIT2, p.second.first, val - val1); } } } for (long long i = 1; i < ms; i++) { for (long long j = 0; j < v[i].size(); j++) { pair<long long, pair<long long, long long> > p = v[i][j]; if (p.second.first == 0) { long long val1 = 0; if (S2[p.first].size() != 0) { val1 = *(S2[p.first].begin()); } S2[p.first].insert(p.second.second); long long val = *(S2[p.first].begin()); update(BIT1, p.first, val - val1); } } } long long ans = query(BIT1, n) + query(BIT2, n); for (long long i = ms + k; i <= me; i++) { long long ar = i - k; long long de = i; for (long long j = 0; j < v[i].size(); j++) { pair<long long, pair<long long, long long> > p = v[i][j]; if (p.first == 0) { long long val1 = 0; if (S1[p.second.first].size() != 0) { val1 = *(S1[p.second.first].begin()); } S1[p.second.first].erase(S1[p.second.first].find(p.second.second)); long long val = *(S1[p.second.first].begin()); update(BIT2, p.second.first, val - val1); } } for (long long j = 0; j < v[ar].size(); j++) { pair<long long, pair<long long, long long> > p = v[ar][j]; if (p.second.first == 0) { long long val1 = 0; if (S2[p.first].size() != 0) { val1 = *(S2[p.first].begin()); } S2[p.first].insert(p.second.second); long long val = *(S2[p.first].begin()); update(BIT1, p.first, val - val1); } } ans = min(ans, query(BIT1, n) + query(BIT2, n)); } cout << ans << endl; } signed main() { long long t; ios_base::sync_with_stdio(false); t = 1; while (t--) solve(); 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 point { long long date, city, cost; }; bool cmp(point x, point y) { return (x.city < y.city || x.city == y.city && x.date > y.date); } point a[2000050], b[2000050]; long long n, m, k, c1, c2, j, mc[2000050], md[2000050], p, lf, rt, cnt, sum1, sum2, ans; long long nxt1[2000050], nxt2[2000050], h1[2000050], h2[2000050], e1[2000050], e2[2000050], to1[2000050], to2[2000050]; bool f; void push1(int x) { p++; to1[p] = a[x].city; e1[p] = a[x].cost; nxt1[p] = h1[a[x].date]; h1[a[x].date] = p; } void push2(int x) { p++; to2[p] = b[x].city; e2[p] = mc[b[x].city]; nxt2[p] = h2[b[x].date]; h2[b[x].date] = p; } void check1(int x) { for (int ii = h1[x]; ii; ii = nxt1[ii]) { int tmp = to1[ii]; if (md[tmp] == 1000000007) { cnt--; sum1 += e1[ii]; md[tmp] = e1[ii]; } else if (e1[ii] < md[tmp]) { sum1 += e1[ii] - md[tmp]; md[tmp] = e1[ii]; } } } void check2(int x) { for (int ii = h2[x]; ii; ii = nxt2[ii]) { int tmp = to2[ii]; sum2 += e2[ii] - mc[tmp]; mc[tmp] = e2[ii]; } } int main() { scanf("%d%d%d", &n, &m, &k); c1 = c2 = 0; p = 0; memset(nxt1, 0, sizeof(nxt1)); memset(nxt2, 0, sizeof(nxt2)); memset(h1, 0, sizeof(h1)); memset(h2, 0, sizeof(h2)); for (int i = 0; i < m; i++) { int u, v, w, z; scanf("%d%d%d%d", &u, &v, &w, &z); if (w == 0) { c1++; a[c1].date = u; a[c1].city = v; a[c1].cost = z; } else { c2++; b[c2].date = u; b[c2].city = w; b[c2].cost = z; } } for (int i = 1; i <= c1; i++) push1(i); p = 0; sort(b + 1, b + c2 + 1, cmp); j = 1; f = true; lf = 1; rt = 2000050 - 1; for (int i = 1; i <= n; i++) { mc[i] = 1000000007; if (b[j].city != i) f = false; else rt = min(rt, b[j].date); while (j <= c2 && b[j].city == i) { push2(j); mc[i] = min(mc[i], b[j].cost); j++; } } if (!f) { printf("-1\n"); return 0; } rt -= k; sum2 = sum1 = 0; for (int i = 1; i <= n; i++) { md[i] = 1000000007; sum2 += mc[i]; } lf = 1; cnt = n; ans = (long long)1000000007 * 1000000007; for (int i = 1; i <= k; i++) check2(i); while (lf < rt) { check1(lf); check2(lf + k); if (!cnt) ans = min(ans, sum2 + sum1); lf++; } if (cnt) 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
def main(): n, m, k = map(int, input().split()) ff, tt = [], [] for _ in range(m): d, f, t, c = map(int, input().split()) if f: ff.append((d, f, c)) else: tt.append((-d, t, c)) for ft in ff, tt: cnt, costs = n, [1000001] * (n + 1) ft.sort(reverse=True) while ft: day, city, cost = ft.pop() oldcost = costs[city] if oldcost > cost: costs[city] = cost if oldcost == 1000001: cnt -= 1 if not cnt: break else: print(-1) return total = sum(costs) - 1000001 l = [(day, total)] while ft: day, city, cost = ft.pop() oldcost = costs[city] if oldcost > cost: total -= oldcost - cost costs[city] = cost if l[-1][0] == day: l[-1] = (day, total) else: l.append((day, total)) if ft is ff: ff = l else: tt = l l, k = [], -k d, c = tt.pop() try: for day, cost in ff: while d + day >= k: d, c = tt.pop() if d + day < k: l.append(c + cost) except IndexError: pass print(min(l, default=-1)) if __name__ == '__main__': main()
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
#include <bits/stdc++.h> using namespace std; const int N = 100005; multiset<pair<int, int> > from0[N * 10], to0[N * 10]; multiset<int> fromn[N], ton[N]; int c1 = 0, c2 = 0; long long cb = 0, ca = 0, ans = 1e15; void add(int i) { if (to0[i].empty()) return; for (auto &x : to0[i]) { int id = x.first, ct = x.second; if (fromn[id].size()) cb -= (*fromn[id].begin()); fromn[id].insert(ct); cb += (*fromn[id].begin()); if (fromn[id].size() == 1) ++c1; } } void remove(int i) { if (from0[i].empty()) return; for (auto x : from0[i]) { int id = x.first, ct = x.second; ca -= (*ton[id].begin()); ton[id].erase(lower_bound(ton[id].begin(), ton[id].end(), ct)); if (ton[id].size()) ca += (*ton[id].begin()); else { --c2; return; } } } int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); while (m--) { int d, f, t, c; scanf("%d %d %d %d", &d, &f, &t, &c); if (f) { to0[d].insert(make_pair(f, c)); } else { from0[d].insert(make_pair(t, c)); ton[t].insert(c); } } for (int i = 1; i <= k + 1; ++i) { for (auto &x : from0[i]) { int id = x.first, ct = x.second; ton[id].erase(lower_bound(ton[id].begin(), ton[id].end(), ct)); } } for (auto &x : to0[1]) { int id = x.first, ct = x.second; fromn[id].insert(ct); } int f = 1, t = k + 2; for (int i = 1; i <= n; ++i) { if (fromn[i].size()) { ++c1; cb += (*fromn[i].begin()); } if (ton[i].size()) { ++c2; ca += (*ton[i].begin()); } } while (c2 == n) { if (c1 == n) ans = min(ans, cb + ca); remove(t++); add(++f); } if (ans != 1e15) printf("%lld", ans); else puts("-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 int MAXN = 200010; const int MAXM = 1000010; const long long INF = 1e12; const int mod = 1e9 + 7; long long read() { long long s = 0, f = 1; char ch = getchar(); while ('0' > ch || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { s = (s << 3) + (s << 1) + (ch - '0'); ch = getchar(); } return s * f; } int n, m, k; long long ans, cnt, tot; string s; struct Flight { long long d, f, t, c; } a[MAXN]; bool cmp(Flight x, Flight y) { return x.d < y.d; } long long f[MAXM], g[MAXM]; long long st[MAXN], ed[MAXN]; int main() { int T; n = read(); m = read(); k = read(); for (int i = 1; i <= m; i++) { a[i].d = read(); a[i].f = read(); a[i].t = read(); a[i].c = read(); } sort(a + 1, a + m + 1, cmp); int dl = 1e6; for (int i = 0; i <= dl + 1; i++) { f[i] = g[i] = 1ll * INF * n; } for (int i = 1; i <= n; i++) { st[i] = ed[i] = INF; } int j = 1; for (int i = 1; i <= dl - k + 1; i++) { f[i] = f[i - 1]; while (j <= m && a[j].d < i) { if (!a[j].t && st[a[j].f] > a[j].c) { f[i] -= st[a[j].f] - a[j].c; st[a[j].f] = a[j].c; } ++j; } } j = m; for (int i = dl - k + 1; i >= 1; i--) { g[i] = g[i + 1]; while (j >= 1 && a[j].d >= i + k) { if (!a[j].f && ed[a[j].t] > a[j].c) { g[i] -= ed[a[j].t] - a[j].c; ed[a[j].t] = a[j].c; } --j; } } ans = INF; for (int i = 1; i <= dl; i++) { ans = min(ans, f[i] + g[i]); } 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
g = lambda: map(int, input().split()) n, m, k = g() F, T = [], [] e = int(3e11) for i in range(m): d, f, t, c = g() if f: F.append((d, f, c)) else: T.append((-d, t, c)) for p in [F, T]: C = [e] * (n + 1) s = n * e q = [] p.sort() for d, t, c in p: if C[t] > c: s += c - C[t] C[t] = c if s < e: q.append((s, d)) p.clear() p += q s, t = e, (0, 0) for f in F: while f: if t[1] + f[1] + k < 0: s = min(s, f[0] + t[0]) elif T: t = T.pop() continue f = 0 print(s if s < e else -1)
PYTHON3