AtCoder Beginner Contest 178题解
A_-_Not
实现思路
直接就是异或一下就可以了,或者直接判断
代码实现
/*
* @Description: 电影和人生不一样,电影太仁慈了,人生太辛苦了
* @CSDN: https://blog.csdn.net/godhandsjoker?spm=1000.2115.3001.5343
* @Github: https://github.com/godhandsjoker
* @QQ: 3124406837
* @Author: godhands
* @LastEditTime: 2022-04-13 18:20:38
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
cin >> n;
cout << (n ^ 1) << "\n";
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
B_-_Product_Max
实现思路
其实我们可以想当然的想到这么一个问题,就是这个最大值一定是出现在这几个区间的端点处,我们直接判断这四种情况哪种最优就可以了
代码实现
/*
* @Description: 电影和人生不一样,电影太仁慈了,人生太辛苦了
* @CSDN: https://blog.csdn.net/godhandsjoker?spm=1000.2115.3001.5343
* @Github: https://github.com/godhandsjoker
* @QQ: 3124406837
* @Author: godhands
* @LastEditTime: 2022-04-13 18:21:39
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int a, b, c, d;
cin >> a >> b >> c >> d;
int res1 = a * c, res2 = a * d, res3 = b * c, res4 = b * d;
cout << max({res1, res2, res3, res4}) << "\n";
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
C_-_Ubiquity
实现思路
这个其实就是一个组合问题,我们可以考虑,我们每一位是 10 10 10种情况,然后我们找不可能的情况
- 不选0 – 每一位9种情况
- 不选9 – 每一位9种情况
- 不选0并且不选9 – 每一位8种情况
然后我们可以发现情况三在我们情况一和情况二都包含了,所以我们用总数减去情况一和情况二之后再加上一个情况三就是我们最后的答案
代码实现
/*
* @Description: 电影和人生不一样,电影太仁慈了,人生太辛苦了
* @CSDN: https://blog.csdn.net/godhandsjoker?spm=1000.2115.3001.5343
* @Github: https://github.com/godhandsjoker
* @QQ: 3124406837
* @Author: godhands
* @LastEditTime: 2022-04-13 19:35:37
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int mod = 1e9 + 7;
int qpow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
int qpow(int a, int b, int mod) {
int res = 1 % mod;
a %= mod;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
void solve() {
int n;
cin >> n;
int res = qpow(10, n, mod) + qpow(8, n, mod) - 2 * qpow(9, n, mod) % mod;
cout << (res + mod) % mod << '\n';
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
D_-_Redistribution
实现思路
这个有三种做法,第一种是最简单的二重循环的动态规划,第二种是优化的动态规划,第三种是利用多项式的答案求解,然后我们直接用数学公式求取出来答案,然后进行一个计算
第一种解法:我们可以从搜索的角度来讲,比如我们一个数可以拆成很多个数,那么我们这一个数就可以由很多的之前拆出来的数相加而得到,也就是说我们的状态表示是 d p [ i ] dp[i] dp[i]代表的是我们序列和为 i i i的序列数量,然后我们的状态转移方程就是 d p [ i ] = d p [ i − 3 ] + d p [ i − 4 ] + . . . + d p [ 0 ] dp[i] = dp[i - 3] + dp[i - 4] + ... + dp[0] dp[i]=dp[i−3]+dp[i−4]+...+dp[0]
然后我们的第二种解法就是在第一种解法上面进行的优化,我们把我们的 i i i变成 i − 1 i - 1 i−1,然后我们错位相减,我们就可以得到 d p [ i ] = d p [ i − 1 ] + d p [ i − 3 ] dp[i] = dp[i - 1] + dp[i - 3] dp[i]=dp[i−1]+dp[i−3]
代码实现
/*
* @Description: 电影和人生不一样,电影太仁慈了,人生太辛苦了
* @CSDN: https://blog.csdn.net/godhandsjoker?spm=1000.2115.3001.5343
* @Github: https://github.com/godhandsjoker
* @QQ: 3124406837
* @Author: godhands
* @LastEditTime: 2022-04-13 22:36:57
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int mod = 1e9 + 7;
void solve() {
int n;
cin >> n;
vector<int> dp(n + 1, 0);
dp[0] = 1;
for (int i = 0; i <= n; i++) {
for (int j = 3; j + i <= n; j++) {
dp[i + j] = (dp[i + j] + dp[i]) % mod;
}
}
cout << dp[n] << "\n";
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
E_-_Dist_Max
实现思路
曼哈顿距离有一个相对应的结论:
∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ |x1 - x2| + |y1 - y2| ∣x1−x2∣+∣y1−y2∣, 我们可以假设 x 1 − x 2 > = 0 x1 - x2 >= 0 x1−x2>=0,根据 y 1 − y 2 y1 - y2 y1−y2的符号分成两种情况:
- ( y 1 − y 2 > = 0 ) − > x 1 + y 1 − ( x 2 + y 2 ) (y1 - y2 >= 0) -> x1 + y1 - (x2 + y2) (y1−y2>=0)−>x1+y1−(x2+y2)
- ( y 1 − y 2 < 0 ) − > x 1 − y 1 − ( x 2 − y 2 ) (y1 - y2 < 0) -> x1 - y1 - (x2 - y2) (y1−y2<0)−>x1−y1−(x2−y2)
代码实现
/*
* @Description: 电影和人生不一样,电影太仁慈了,人生太辛苦了
* @CSDN: https://blog.csdn.net/godhandsjoker?spm=1000.2115.3001.5343
* @Github: https://github.com/godhandsjoker
* @QQ: 3124406837
* @Author: godhands
* @LastEditTime: 2022-04-13 19:00:48
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n, x, y, maxx = INT_MIN, minx = INT_MAX, maxy = INT_MIN, miny = INT_MAX;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x >> y;
minx = min(minx, x + y), maxx = max(maxx, x + y);
miny = min(miny, x - y), maxy = max(maxy, x - y);
}
cout << max(maxx - minx, maxy - miny) << "\n";
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}
F_-_Contrast
实现思路
我们可以把我们的 B B B数组反转,这样可以保证我们的这个 A A A序列是非递减, B B B是非递增,然后我们可以保证最多只会有一个区间 A [ i ] = = B [ i ] A[i] == B[i] A[i]==B[i],然后我们去这个区间外面找是否有足够的数目严格大于等于这个区间里面的元素,如果可以就是 Y E S YES YES,否则就是 N O NO NO
代码实现
/*
* @Description: 电影和人生不一样,电影太仁慈了,人生太辛苦了
* @CSDN: https://blog.csdn.net/godhandsjoker?spm=1000.2115.3001.5343
* @Github: https://github.com/godhandsjoker
* @QQ: 3124406837
* @Author: godhands
* @LastEditTime: 2022-04-13 23:43:41
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
void solve() {
int n;
cin >> n;
vector<int> a(n), b(n);
for (auto &it : a) cin >> it;
for (auto &it : b) cin >> it;
reverse(b.begin(), b.end());
int l = -1, r = -1, c = -1;
for (int i = 0; i < n; i++) {
if (a[i] == b[i] and l == -1) {
l = i, c = a[i];
}
if (a[i] != b[i] and l != -1) {
r = i - 1;
break;
}
}
if (r == -1 and l != -1) r = n;
for (int i = 0; i < n; i++)
if (a[i] != c and b[i] != c and l <= r and l != -1) swap(b[i], b[l++]);
if (l <= r and l != -1) {
cout << "No\n";
return;
}
cout << "Yes\n";
for (const auto &it : b) {
cout << it << " ";
}
cout << "\n";
}
signed main() {
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
solve();
return 0;
}