usingnamespacestd; typedeflonglong ll; typedefstd::pair<ll, ll> P; constint N = 1e6 + 5; constint INF = 0x3f3f3f3f; const ll llINF = 0x3f3f3f3f3f3f3f3f; constint mod = 998244353; ll read(){ ll x = 0, w = 1;char ch = 0; while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar(); } return x * w; }
intmain(){ int n, k; n = read(), k = read(); if (n == k || k == 0){ cout << "0 0\n"; return0; } int mx = 2 * k; mx = min(mx, n - k); cout << "1 " << mx << "\n";
usingnamespacestd; typedeflonglong ll; typedefstd::pair<ll, ll> P; constint N = 3e5 + 5; constint INF = 0x3f3f3f3f; const ll llINF = 0x3f3f3f3f3f3f3f3f; constint mod = 998244353; ll read(){ ll x = 0, w = 1;char ch = 0; while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar(); } return x * w; }
structnode { int c, j; // c 是花费, j 是起飞时间 booloperator < (const node &x) const{ return c < x.c; } }a[N]; int ans[N]; // 第i个飞机的实际起飞时间 ll res; priority_queue <node> q; intmain(){ int n, k; n = read(), k = read(); for (int i = 1; i <= n; ++i){ a[i].c = read(); a[i].j = i; } for (int i = 1; i <= k + 1; ++i){ if (i > n) break; q.push(a[i]); } int cnt = k + 1; while(!q.empty()){ res += (ll)(q.top().c) * (cnt - q.top().j); // 计算总花费 ans[q.top().j] = cnt++; // 第q.top().j个飞机的实际起飞时间 q.pop(); if (cnt <= n) q.push(a[cnt]); } cout << res << "\n"; for (int i = 1; i <= n; ++i){ cout << ans[i]; if (i != n) cout << " "; elsecout << "\n"; }
return0; }
D CodeForces 854D
1. 题目大意
一共有n+1个城市(0 - n), 然后 1 - n 城市每个城市一个人, 要到0号城市去会晤, 这n个人至少要一起在0号城市待k天, 现给出 1 - n 城市到0号城市以及 0号城市到 1- n 城市的机票价钱,以及航班开始时间, 并且这一天一定能到达, 问如何安排能使总花费最少。
ll read(){ ll x = 0, w = 1; char ch = 0; while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar(); } return x * w; }
structnode{ int d, f, t, c; // 题意中的d, f, t, c }a[N];
boolcmp(node x, node y){ return x.d < y.d; }
ll sum1[M], sum2[M]; ll val[N]; intmain(){ int n, m, k, d, f, t, c, en = 0; n = read(), m = read(), k = read(); for (int i = 1; i <= m; ++i){ d = read(), f = read(), t = read(), c = read(); a[i] = {d, f, t, c}; en = max(en, d); } sort(a + 1, a + m + 1, cmp); // 先按照飞机起飞时间排序 ms(sum1, llINF); ms(sum2, llINF); // 下面3个是辅助维护的 ms(val, llINF); ll sum = 0; int len = 0; // 下面是维护sum1 for (int i = 1; i <= m; ++i){ if (a[i].f == 0) continue; // 剔除出发地是0号城市的 // val[i]是储存,出发地是i的飞机票的最小价值 if (val[a[i].f] == llINF){ val[a[i].f] = a[i].c; sum += a[i].c; // 总价值 len++; } elseif (val[a[i].f] > a[i].c){ sum += -val[a[i].f] + a[i].c; val[a[i].f] = a[i].c; } if (len == n){ // 如果n个城市都有航班飞向0城市了, 那就可以安排了 sum1[a[i].d] = sum; } } // 由于上面循环并没有照顾到每一天的航班, 就是有的天, 没有航班, 不是真正的 sum1, 下面就是维护没有航班的天 for (int i = 1; i <= en; ++i){ if (sum1[i - 1] != llINF){ sum1[i] = min(sum1[i - 1], sum1[i]); } } // 维护sum2, 同理 len = 0; ms(val, llINF); sum = 0; for (int i = m; i >= 1; --i){ if (a[i].t == 0) continue; if (val[a[i].t] == llINF){ val[a[i].t] = a[i].c; sum += a[i].c; len++; } elseif (val[a[i].t] > a[i].c){ sum += -val[a[i].t] + a[i].c; val[a[i].t] = a[i].c; } if (len == n){ sum2[a[i].d] = sum; } } for (int i = en - 1; i >= 1; --i){ if (sum2[i + 1] != llINF){ sum2[i] = min(sum2[i + 1], sum2[i]); } }
ll ans = llINF; for (int i = 1; i <= en - k - 1; ++i){ ans = min(ans, sum1[i] + sum2[i + k + 1]); } if (ans == llINF) puts("-1"); elsecout << ans << "\n";
inlinevoidadd(int u, int v, int w){ ed[cnt] = {u, v, w, head[u]}; head[u] = cnt++; }
queue<int> q;
voidspfa(int u){ vis[u] = 1; dis[u] = 0; q.push(u); while (!q.empty()) { u = q.front(); q.pop(); vis[u] = 0; for (int i = head[u]; ~i; i = ed[i].ne) { int v = ed[i].v; if (dis[v] < dis[u] + ed[i].w) { dis[v] = dis[u] + ed[i].w; if (!vis[v]) { q.push(v); vis[v] = 1; } } } } }
int a, b, c, n;
intmain(){ while (~scanf("%d", &n)) { init(); int mx = 0, mi = INF; for (int i = 0; i < n; ++i) { a = read(), b = read(), c = read(); add(a - 1, b, c); mx = max(mx, b); mi = min(mi, a); } for (int i = mi - 1; i < mx; ++i) { add(i + 1, i, -1); add(i, i + 1, 0); } spfa(mi - 1); printf("%d\n", dis[mx]); } return0; }
voidread(int &ans){ ll x = 0, w = 1; char ch = 0; while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + (ch - '0'); ch = getchar(); } ans = x * w; } int n; structmatrix{ int m[205][205]; matrix(){ ms(m, INF); } matrix operator * (const matrix & x) const{ matrix c; for (int i = 1; i <= n; ++i){ for (int j = 1; j <= n; ++j){ for (int k = 1; k <= n; ++k){ c.m[i][j] = min(c.m[i][j], m[i][k] + x.m[k][j]); } } } return c; } }x;
matrix qpow(matrix a, int b){ matrix ans = a; b--; while(b){ if (b & 1) ans = ans * a; b >>= 1; a = a * a; } return ans; } map<int, int>ma; int k, t, s, e, a, b, c;
intmain(){ int cnt = 0; scanf("%d%d%d%d", &k, &t, &s, &e); while(t--){ scanf("%d%d%d", &c, &a, &b); if (ma[a]) a = ma[a]; else a = ma[a] = ++cnt; if (ma[b]) b = ma[b]; else b = ma[b] = ++cnt; x.m[a][b] = x.m[b][a] = c; } n = cnt; x = qpow(x, k); printf("%d\n", x.m[ma[s]][ma[e]]); return0; }