A. Nastya and Rice
判断一下[n*(a-b),n*(a+b)]和[c-d,c+d]两个区间有没有相交
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, a, b, c, d; int main() { int t; cin >> t; while (t--) { cin >> n >> a >> b >> c >> d; int flag = 0; if (n * (a + b) >= c - d && n * (a - b) <= c + d) flag = 1; puts(flag ? "YES" : "NO"); } }
B. Nastya and Door
滑动窗口统计一下最大个数,注意一下区间两端的peak不计入ans
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, k, a[200010], ispeak[200010]; int cnt, l, ans; int main() { int t; cin >> t; while (t--) { cin >> n >> k; rep(i, 1, n) cin >> a[i]; rep(i, 2, n - 1) ispeak[i] = a[i] > a[i - 1] && a[i] > a[i + 1]; cnt = 0; l = 1; rep(i, 1, k) cnt += ispeak[i]; ans = cnt - ispeak[k]; rep(i, 2, n - k + 1) { cnt += ispeak[i + k - 1] - ispeak[i - 1]; if (cnt - ispeak[i + k - 1] - ispeak[i] > ans) { ans = cnt - ispeak[i + k - 1] - ispeak[i]; l = i; } } cout << ans + 1 << " " << l << endl; } }
C. Nastya and Strange Generator
从1-n填数,在i位置填数,下一个要么在 i+1 的位置填,要么在[1,i-1]之间填
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, a[200010]; int main() { int t; cin >> t; while (t--) { cin >> n; rep(i, 1, n) cin >> a[i]; int tmp = 1, flag = 1; for (int i = n; i; i--) { if (tmp == a[i]) tmp++; else if (tmp < a[i]) tmp = a[i]; else if (a[i + 1] - a[i] != 1) { flag = 0; break; } } puts(flag ? "YES" : "NO"); } }
D. Nastya and Scoreboard
dfs+剪枝,标记一下当前位置不可行的操作数
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n, k; char a[2010][10]; char b[10][10] = {"1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011"}; pair<int, int> c[2010][10]; int id[2010]; int vis[2010][2010]; int chos[2010]; bool isanswerd = 0; void dfs(int step, int num) { if (num > k || isanswerd || vis[step][num]) return; if (step == n + 1) { if (num != k) return; rep(i, 1, n) cout << chos[i]; isanswerd = 1; } for (int i = id[step]; i; i--) { chos[step] = c[step][i].first; dfs(step + 1, num + c[step][i].second); } vis[step][num] = 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; int cnt, flag; rep(i, 1, n) { cin >> a[i]; rep(j, 0, 9) { cnt = 0; flag = 1; rep(z, 0, 6) if (a[i][z] == '1' && b[j][z] == '0') { flag = 0; break; } else if (a[i][z] == '0' && b[j][z] == '1') cnt++; if (flag) c[i][++id[i]] = make_pair(j, cnt); } } dfs(1, 0); if (!isanswerd) cout << "-1"; }