西北大学集训队选拔赛(重现赛)
A.坐飞机
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, t; int a[maxn], b[maxn]; int p[maxn]; int main() { scanf("%d%d", &n, &t); for(int i = 1; i <= n; i ++) { scanf("%d%d", &a[i], &b[i]); } for(int i = 2; i <= n; i ++) { p[i - 1] = a[i] - b[i - 1] + 1; } n --; sort(p + 1, p + n + 1); int ans = 0; for(int i = 1; i <= n; i ++) { if(t >= p[i]) { ans ++; t -= p[i]; } } printf("%d\n", ans); return 0; }
B.饱和式救援
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, m, k; double x[maxn]; double dp[2100][2100]; int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= m; i ++) x[i] = 1.0; for(int i = 1; i <= n; i ++) { int a; double p; scanf("%d%lf", &a, &p); x[a] = x[a] * (1.0 - p); } for(int i = 1; i <= m; i ++) x[i] = 1.0 - x[i]; dp[1][1] = x[1]; dp[1][0] = 1.0 - x[1]; for(int i = 2; i <= m; i ++) { dp[i][0] = dp[i - 1][0] * (1.0 - x[i]); for(int j = 1; j <= i; j ++) { dp[i][j] = dp[i - 1][j] * (1.0 - x[i]) + dp[i - 1][j - 1] * x[i]; } } double ans = 0.0; for(int j = k; j <= m; j ++) { ans += dp[m][j]; } printf("%.3f\n", ans); return 0; }
C.晾衣服
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int N; long long L; struct Node { int wet; int lena; int va; int tima; int lenb; int vb; int timb; }node[maxn]; bool check(int x) { for(int i = 0; i < N; i ++) if(min(node[i].tima, node[i].timb) > x) return false; long long sum = 0; for(int i = 0; i < N; i ++) { if(node[i].tima <= x && node[i].timb <= x) { sum += min(node[i].lena, node[i].lenb); } else if(node[i].tima <= x){ sum += node[i].lena; } else sum += node[i].lenb; } if(sum <= L) return true; else return false; } int main() { scanf("%d%lld", &N, &L); int T = 0; for(int i = 0; i < N; i ++) { scanf("%d%d%d%d%d", &node[i].wet, &node[i].lena, &node[i].va, &node[i].lenb, &node[i].vb); node[i].tima = node[i].wet / node[i].va; if(node[i].wet % node[i].va) node[i].tima ++; node[i].timb = node[i].wet / node[i].vb; if(node[i].wet % node[i].vb) node[i].timb ++; T = max(T, max(node[i].tima, node[i].timb)); } int l = 0, r = T, mid, p = -1; while(l <= r) { mid = (l + r) / 2; if(check(mid)) { p = mid; r = mid - 1; } else l = mid + 1; } printf("%d\n", p); return 0; }
D.温暖的签到题
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int n, m; long long s[4 * maxn], f[maxn * 4], Q; long long cal(long long first, int len) { return (first + first + len - 1) * len / 2; } void pushUp(int rt) { s[rt] = s[2 * rt] + s[2 * rt + 1]; } void pushDown(int rt, int l, int r) { if(f[rt] == 0) return; int mid = (l + r) / 2; // [l, mid] f[2 * rt] = f[rt]; s[2 * rt] = cal(f[rt], mid - l + 1); // [mid + 1, r] f[2 * rt + 1] = f[rt] + mid + 1 - l; s[2 * rt + 1] = cal(f[rt] + mid + 1 - l, r - (mid + 1) + 1); f[rt] = 0; } void Input(int rt, int l, int r) { if(l == r) { s[rt] = r; return; } int mid = (l + r) / 2; Input(rt * 2, l, mid); Input(rt * 2 + 1, mid + 1, r); pushUp(rt); } long long Sum(int L, int R, int rt, int l, int r) { if(L <= l && r <= R) { return s[rt]; } pushDown(rt, l, r); int mid = (l + r) / 2; long long left = 0; long long right = 0; if(L <= mid) left = Sum(L, R, 2 * rt, l, mid); if(R > mid) right = Sum(L, R, 2 * rt + 1, mid + 1, r); pushUp(rt); return left + right; } void Update(int L, int R, int rt, int l, int r) { if(L <= l && r <= R) { f[rt] = Q; s[rt] = cal(Q, r - l + 1); Q += (r - l + 1); return; } pushDown(rt, l, r); int mid = (l + r) / 2; if(L <= mid) Update(L, R, 2 * rt, l, mid); if(R > mid) Update(L, R, 2 * rt + 1, mid + 1, r); pushUp(rt); } int main() { scanf("%d%d", &n, &m); Input(1, 1, n); while(m --) { int op, L, R; scanf("%d%d%d", &op, &L, &R); if(op == 1) { Q = 1; Update(L, R, 1, 1, n); } else { printf("%lld\n", Sum(L, R, 1, 1, n)); } } return 0; }
E.西湖奇遇记
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 10; long long INF = 1e9; int n, m, k; long long dis[maxn][7]; int h[maxn], a[maxn], b[maxn], cost[maxn], nx[maxn], sz; void add(int x, int y, long long z) { a[sz] = x; b[sz] = y; cost[sz] = z; nx[sz] = h[x]; h[x] = sz ++; } void dij() { for(int i = 1; i <= n; i ++) { for(int j = 0; j <= k; j ++) { dis[i][j] = INF; } } for(int j = 0; j <= k; j ++) { dis[1][0] = 0; } priority_queue<pair<long long, int> > q; q.push(make_pair(0LL, 1 * 10 + 0)); while(!q.empty()) { pair<long long, int> tp = q.top(); q.pop(); int d = -tp.first; int v = tp.second / 10; int f = tp.second % 10; for (int i = h[v]; i != -1; i = nx[i]) { if (dis[v][f] + cost[i] < dis[b[i]][f]) { dis[b[i]][f] = dis[v][f] + cost[i]; q.push(make_pair(-dis[b[i]][f], b[i] * 10 + f)); } if (f + 1 <= k && dis[v][f] + 1 < dis[b[i]][f + 1]) { dis[b[i]][f + 1] = dis[v][f] + 1; q.push(make_pair(-dis[b[i]][f + 1], b[i] * 10 + f + 1)); } } } } int main() { INF = INF * INF; scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= n; i ++) h[i] = -1; for(int i = 1; i <= m; i ++) { int x, y; long long c; scanf("%d%d%lld", &x, &y, &c); add(x, y, c); add(y, x, c); } dij(); long long ans = INF; for(int j = 0; j <= k; j ++) { ans = min(ans, dis[n][j]); } printf("%lld\n", ans); return 0; }
F.三生三世
代码:
#include <bits/stdc++.h> using namespace std; int N; string s, t; map<char, int> mp; int main() { scanf("%d", &N); cin >> s >> t; bool flag = true; if(s == t || s.length() != t.length()) flag = false; else { for(int i = 0; s[i]; i ++) mp[s[i]] ++; for(int i = 0; t[i]; i ++) mp[t[i]] --; for(int i = 0; i < 26; i ++) { if(mp['a' + i] < 0 || mp['A' + i] < 0) { flag = false; break; } } } if(flag) printf("yes\n"); else printf("no\n"); return 0; }
G.序列长度Ⅰ
代码:
#include <bits/stdc++.h> using namespace std; const int maxn = 2e6 + 10; int n, k; int m; int b[maxn], st[maxn], top; int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 1; i <= n; i ++) { scanf("%d", &b[i]); } top = -1; long long ans = 0; long long sum = 0; for(int i = 1; i <= k - 1; i ++) { sum += b[i]; if(b[i] != -m) { st[++ top] = i; } } for(int i = k; i <= n; i ++) { sum += b[i]; if(b[i] != -m) { st[++ top] = i; } while(sum >= 0) { int pos = st[top]; top --; if(sum - (b[pos] + m) < 0) { b[pos] = (int)(1LL * b[pos] - (sum + 1)); ans += (sum + 1); sum = -1LL; if(b[pos] != -m) st[++ top] = i; } else { sum -= (1LL * b[pos] + m); ans += (1LL * b[pos] + m); b[pos] = -m; } } sum -= b[i - k + 1]; } printf("%lld\n", ans); return 0; }
H.偶数计算
代码:
#include <iostream> using namespace std; long long N, L, R; long long b[100], len, one; long long get(long long x) { long long res = 0LL; long long p = one - 1LL; for (long long i = len - 1; i >= 0; i--) { long long act = b[i]; if (act == 1LL) { act = (x&(1LL << p)) ? 1LL : 0LL; p--; } res = res * 2LL + act; } return res; } long long cal(long long x) { long long left = 0LL; long long right = (1LL << one) - 1LL; long long pos = 0; while (left <= right) { long long mid = (left + right) / 2LL; if (get(mid) <= x) { pos = mid; left = mid + 1LL; } else { right = mid - 1LL; } } return pos; } int main() { cin >> N >> L >> R; len = one = 0; long long t = N; while (t) { if (t % 2LL == 1LL) { one++; } b[len++] = t % 2LL; t = t / 2LL; } cout << R - L + 1LL - (cal(R) - cal(L - 1LL)) << endl; return 0; }
J.西湖奇遇记Ⅱ
代码:
#include <bits/stdc++.h> using namespace std; const int INF = 1e8; const int maxn = 310; int n, m, k; int a[maxn][maxn]; int v[maxn][maxn], id; int dis[maxn][maxn]; int cost[30][30]; int dp[100000][30]; int dir[4][2] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1} }; int out(int cx, int cy) { if(cx < 0 || cx > n - 1 || cy < 0 || cy > m - 1) return 1; return 0; } void bfs(int sx, int sy) { for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { dis[i][j] = INF; } } dis[sx][sy] = 0; queue<int> q; q.push(sx * m + sy); while(!q.empty()) { int tp = q.front(); q.pop(); int cx = tp / m; int cy = tp % m; for(int i = 0; i < 4; i ++) { int nx = cx + dir[i][0]; int ny = cy + dir[i][1]; if(out(nx, ny)) continue; if(a[nx][ny] == 1) continue; if(dis[cx][cy] + 1 < dis[nx][ny]) { dis[nx][ny] = dis[cx][cy] + 1; q.push(nx * m + ny); } } } /* printf("------\n"); for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { printf("%d ", dis[i][j]); } cout << endl; } */ int op = v[sx][sy]; if(sx == 0 && sy == 0) op = 0; for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { if(i == 0 && j == 0) cost[op][0] = dis[i][j]; if(v[i][j] != 0) cost[op][v[i][j]] = dis[i][j]; } } } int lowbit(int x) { return x & (-x); } int main() { scanf("%d%d%d", &n, &m, &k); for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { scanf("%d", &a[i][j]); if(a[i][j] == 2) { id ++; v[i][j] = id; } } } for(int i = 0; i <= id; i ++) { for(int j = 0; j <= id; j ++) { cost[i][j] = INF; } } for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { if(i == 0 && j == 0) bfs(i, j); if(v[i][j] != 0) bfs(i, j); } } /* cout << "!!!" << endl; for(int i = 0; i < n; i ++) { for(int j = 0; j < m; j ++) { cout << v[i][j] << " "; } cout << endl; } for(int i = 0; i <= id; i ++) { for(int j = 0; j <= id; j ++) { printf("%d -> %d: %d\n", i, j, cost[i][j]); } } */ // dp for(int i = 0; i < (1 << (id + 1)); i ++) { for(int j = 0; j <= id; j ++) { dp[i][j] = INF; } } dp[1][0] = 0; for(int i = 2; i < (1 << (id + 1)); i ++) { for(int j = 0; j <= id; j ++) { if(((1 << j) & i) == 0) continue; int pre = i ^ (1 << j); for(int k = 0; k <= id; k ++) { if(((1 << k) & pre) == 0) continue; dp[i][j] = min(dp[i][j], dp[pre][k] + cost[k][j]); } } } int ans = 0; for(int i = 1; i < (1 << (id + 1)); i ++) { for(int j = 0; j <= id; j ++) { if(dp[i][j] > k) continue; int t = 0; int temp = i; while(temp) { t ++; temp -= lowbit(temp); } ans = max(ans, t - 1); } } printf("%d\n", ans); return 0; }