Files
python/GESP/Level6/20240302.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

33 lines
1.0 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
const int N = 10;
PII a[N];
int ans = INT_MAX;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i].first;
for (int i = 0; i < n; i++)
cin >> a[i].second;
do {
// Q: 为什么res初始化为n
// A: 因为每个奶牛需要一个牛棚所以初始化为n
// Q:怎么想到的用全排列?
// A: 因为题目要求的是所有奶牛的组合,所以需要枚举所有组合,而且题目的数据范围极小N最大才是9所以用全排列是可行的。
int res = n, k = a[0].second; // 排序第一的牛它的右手安全距离a[0].second
for (int i = 1; i < n; i++) {
res += max(k, a[i].first); // 当前牛的左手安全距离加上1才是真正的安全距离
k = a[i].second;
}
ans = min(ans, res); // 每次组合取最小值
} while (next_permutation(a, a + n)); // 枚举所有组合
cout << ans << endl;
return 0;
}