新年快乐! 做个简单的(感觉几天没打,康复训练,新的一年,希望自己每天做题吧。 还是快乐就好
A - 12435 思路: 模拟,尝试交换每两个相邻数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <bits/stdc++.h> using namespace std;int main () { int a[5 ] = {1 , 2 , 3 , 4 , 5 }; int b[5 ]; for (int i = 0 ; i < 5 ; i++) { cin >> b[i]; } for (int i = 0 ; i < 4 ; i++) { swap (b[i], b[i + 1 ]); int f = 0 ; for (int j = 0 ; j < 5 ; j++) { if (a[j] != b[j]) { f = 1 ; break ; } } if (!f) { cout << "Yes\n" ; return 0 ; } swap (b[i], b[i + 1 ]); } cout << "No\n" ; return 0 ; }
B - Geometric Sequence 思路:a[i] * a[i + 2] != a[i + 1] * a[i + 1] 表示等比的,小于3直接输出 Yes 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #include <bits/stdc++.h> using namespace std;#define int long long signed main () { int n; cin >> n; vector<int > a (n) ; for (int i = 0 ; i < n; i++) cin >> a[i]; if (n < 3 ) { cout << "Yes\n" ; return 0 ; } for (int i = 0 ; i < n - 2 ; i++) { if (a[i] * a[i + 2 ] != a[i + 1 ] * a[i + 1 ]){ cout << "No\n" ; return 0 ; } } cout << "Yes\n" ; return 0 ; }
C - Paint to make a rectangle 思路:找到四个角的 ‘#‘, 如果这个矩形里面存在 ‘ . ’就输出No。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <bits/stdc++.h> using namespace std;#define int long long signed main () { int n, m; cin >> n >> m; vector<string> a (n) ; for (int i = 0 ; i < n; i++) cin >> a[i]; int x1 = INT_MAX, x2 = INT_MIN, y1 = INT_MAX, y2 = INT_MIN; for (int i = 0 ; i < n; i++) { for (int j = 0 ; j < m; j++) { if (a[i][j] == '#' ) { x1 = min (x1, i), x2 = max (x2, i); y1 = min (y1, j), y2 = max (y2, j); } } } for (int i = x1; i <= x2; i++) { for (int j = y1; j <= y2; j++) if (a[i][j] == '.' ) { cout << "No\n" ; return 0 ; } } cout << "Yes\n" ; }
D - Stone XOR 思路:这道题就是暴搜索,也是给我教育了,也不能·太暴,搜索的时候记录三个状态,选择到哪一个数了 u, 数组记录当前分了的块 cur,used分了几块。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #include <bits/stdc++.h> using namespace std;#define int long long signed main () { int n; cin >> n; vector<int > a (n) ; for (int i = 0 ; i < n; i++) cin >> a[i]; unordered_map<int , int > mp; auto dfs = [&](auto && dfs, int u, auto & cur, int used)->void { if (u == n) { int p = 0 ; for (auto x : cur) p ^= x; mp[p] = 1 ; return ; } for (int i = 0 ; i < used; i++) { int lst = cur[i]; cur[i] += a[u]; dfs (dfs, u + 1 , cur, used); cur[i] = lst; } if (used < n) { cur[used] = a[u]; dfs (dfs, u + 1 , cur, used + 1 ); cur[used] = 0 ; } }; vector<int > cur (n) ; dfs (dfs, 0 , cur, 0 ); cout << mp.size () << endl; return 0 ; }
一开始自己写的,还是太暴力了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #include <bits/stdc++.h> using namespace std;#define int long long signed main () { int n; cin >> n; vector<int > a (n) ; for (int i = 0 ; i < n; i++) cin >> a[i]; map<int , int > mp; vector<int > st (n) ; auto dfs = [&](auto && dfs, int u, int t)->void { if (u == n) { mp[t]++; return ; } int x = n - u; for (int i = 1 ; i <= x; i++) { auto dfs1 = [&](auto && dfs1, int cnt, int p)->void { if (cnt == 0 ) { dfs (dfs, u + i, t ^ p); return ; } for (int i = 0 ; i < n; i++) { if (st[i]) continue ; st[i] = 1 ; dfs1 (dfs1, cnt - 1 , p + a[i]); st[i] = 0 ; } }; dfs1 (dfs1, i, 0 ); } }; dfs (dfs, 0 , 0 ); cout << mp.size () << endl; return 0 ; }
E - Vitamin Balance 思路,有单调性的,可以二分,check函数背包dp找满足条件所需的价格(维生素)最小值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 #include <bits/stdc++.h> using namespace std;#define int long long int n, m;int cal (int x, auto vec) { vector<int > dp (m + 1 ) ; for (auto [b, c] : vec) { for (int i = m; i >= c; i--){ dp[i] = max (dp[i], dp[i - c] + b); } } for (int i = 0 ; i <= m; i++) { if (dp[i] >= x) return i; } return -1 ; } signed main () { cin >> n >> m; vector<array<int , 2>> vec[3 ]; for (int i = 0 ; i < n; i++) { int a, b, c; cin >> a >> b >> c; vec[a - 1 ].push_back ({b, c}); } auto check = [&](int x)->bool { int res = 0 ; for (int i = 0 ; i < 3 ; i++) { int val = cal (x, vec[i]); if (val == -1 ) return false ; res += val; } return res <= m; }; int l = 0 , r = 1e9 ; while (l < r) { int mid = l + r + 1 >> 1 ; if (check (mid)) l = mid; else r = mid - 1 ; } cout << l << endl; return 0 ; }