Summer Vacation Friendship Contest No.2 Solutions
Problem A: Rain
Read four integers a, b, c, d and one target value x. For each of the four values, output 0 if it's greater than x, otherwise output x - value.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int a, b, c, d, x;
cin >> a >> b >> c >> d >> x;
cout << (a > x ? 0 : x - a) << ' '
<< (b > x ? 0 : x - b) << ' '
<< (c > x ? 0 : x - c) << ' '
<< (d > x ? 0 : x - d);
return 0;
}
Problem B: Kiss
Calcluate (n + n*(n-1)) % mod where mod = 998244353. Note that input n should also be taken modulo mod before computation.
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
const int MOD = 998244353;
int n;
cin >> n;
n %= MOD;
cout << (n % MOD + n % MOD * (n - 1) % MOD) % MOD << endl;
return 0;
}
Problem C: Loss
Given a string s and a list of n strings, find all strings that match s at maximum posisions. Output these strings in lexicographical order.
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
string s;
int n;
cin >> s >> n;
vector<string> candidates(n);
for(auto &i : candidates) cin >> i;
vector<int> matches(n);
for(int i = 0; i < n; i++) {
if(candidates[i].size() != s.size()) {
matches[i] = 0;
} else {
for(int j = 0; j < s.size(); j++)
matches[i] += (candidates[i][j] == s[j]);
}
}
vector<string> result;
int max_matches = *max_element(matches.begin(), matches.end());
for(int i = 0; i < n; i++) {
if(matches[i] == max_matches)
result.emplace_back(candidates[i]);
}
sort(result.begin(), result.end());
for(const auto& str : result)
cout << str << endl;
return 0;
}
Problem D: Blow
Dynamic programming approach to maximize the "cuteness" of a sequence. dp[i][0/1] represents the maximum cuteness when the i-th element is either 1 or its original value.
State transitions:
- If current element is
1:dp[i][0] = max(dp[i-1][0], dp[i-1][1] + |a[i-1] - 1|) - If current element is original:
dp[i][1] = max(dp[i-1][0] + |a[i] - 1|, dp[i-1][1] + |a[i-1] - a[i]|)
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n;
cin >> n;
vector<int> arr(n);
for(auto &i : arr) cin >> i;
vector<vector<int>> dp(n, vector<int>(2, 0));
for(int i = 1; i < n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + abs(arr[i - 1] - 1));
dp[i][1] = max(dp[i - 1][0] + abs(arr[i] - 1), dp[i - 1][1] + abs(arr[i - 1] - arr[i]));
}
cout << max(dp[n - 1][0], dp[n - 1][1]) << endl;
return 0;
}
Problem E: Call
Determine whether a given area can accommodate paper pieces of different sizes according to specific placement rules.
- Each size 4, 5, 6 piece requires a separate square frame.
- Each frame can hold up to 4 size 3 pieces.
- Size 2 and 1 pieces can be placed inside.
- Compute required frames based on optimal packing and check remaining space.
#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int T;
cin >> T;
while(T--) {
int s;
cin >> s;
vector<int> count(7);
for(int i = 1; i <= 6; i++) cin >> count[i];
int total_frames = 0;
total_frames += count[6] + count[5] + count[4] + (count[3] + 3) / 4;
int available_space_for_2 = 5 * count[4] + (count[3] % 4 == 0 ? 0 : (count[3] % 4 == 1 ? 5 : (count[3] % 4 == 2 ? 3 : 1)));
if(count[2] > available_space_for_2)
total_frames += (count[2] - available_space_for_2 + 8) / 9;
int used_area = total_frames * 36 - count[6] * 36 - count[5] * 25 - count[4] * 16 - count[3] * 9 - count[2] * 4;
if(count[1] > used_area)
total_frames += (count[1] - used_area + 35) / 36;
cout << (total_frames > s ? "No" : "Yes") << endl;
}
return 0;
}