2个小时5道编程题,只做出了3道,感觉人无了
第一题
大意是有n个物品,可以按满减和折扣的方式购买,求前k个物品最划算的购买方式。直接模拟即可,细节没注意浪费了好久。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
LL p1[50010], p2[50010];
vector<PII> rule;
LL t[50010];
LL idx;
int main()
{
int n, m;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> p1[i];
for (int i =1; i<= n; i++)
cin >> p2[i];
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> t[i];
}
for (int i = 1; i <= m; i++) {
LL x;
cin >> x;
rule.push_back({t[i], x});
}
sort(rule.begin(), rule.end());
LL sum = 0, sum1 = 0;
LL now = 0;
for (int i = 1; i <= n; i++) {
sum += p1[i];
sum1 += p2[i];
while(idx < rule.size() && rule[idx].first <= sum) {
now = rule[idx].second;
idx++;
}
// cout << sum << " " << sum1 << " " << now << endl;
LL sum2 = sum - now;
if (sum1 < sum2) cout << "Z";
else if (sum1 > sum2) cout << "M";
else cout << "B";
}
return 0;
}
第二题
给定一个字符串,求出其加密的结果或原先的字符串,加密方式为每次选取当前字符串第n/2个字符(下取整)加入空串中,直至取完。
#include <bits/stdc++.h>
using namespace std;
string code(string s) {
string ans = "";
int l, r;
if (s.size() % 2 == 0) l = s.size() / 2 - 1, r = s.size() /2;
else l = r = s.size() / 2;
while(ans.size() < s.size()) {
// cout << l << " " << r << endl;
if (l >= 0) ans += s[l];
if (r < s.size()) ans += s[r];
l--, r++;
}
return ans;
}
string decode(string s) {
string ans = "";
int l = 0, r = 1;
while(ans.size() < s.size())
{
if (l < s.size()) ans = s[l] + ans;
if (r < s.size()) ans += s[r];
l += 2, r += 2;
}
return ans;
}
int main()
{
int n, t;
cin >> n >> t;
string str;
cin >> str;
if (t == 1) cout << code(str);
else cout << decode(str);
return 0;
}
第三题
题意提炼后大意为,A给出若干个区间,B也给出若干个区间,求A和B中重复的点的个数。分别区间合并后对A,B区间两两枚举求公共点,结果过了
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
vector<PII> block1, block2, temp;
int temp_l[30010], temp_r[30010];
long long get_common(PII x1, PII x2)
{
if (x1.first > x2.second || x2.first > x1.second) return 0;
int l = max(x1.first, x2.first);
int r = min(x1.second, x2.second);
return r - l + 1;
}
int main()
{
int n, m1, m2;
cin >> n >> m1 >> m2;
for (int i = 1; i <= m1; i++)
cin >> temp_l[i];
for (int i = 1; i <= m1; i++)
{
cin >> temp_r[i];
temp.push_back({temp_l[i], temp_r[i]});
}
sort(temp.begin(), temp.end());
for (int i = 0; i < temp.size(); i++) {
PII now = temp[i];
while(i + 1 < temp.size() && temp[i + 1].first <= now.second) {
now = {now.first, max(now.second, temp[i + 1].second)},
i++;
}
block1.push_back(now);
}
// -------------------------
temp.clear();
for (int i = 1; i <= m2; i++)
cin >> temp_l[i];
for (int i = 1; i <= m2; i++)
{
cin >> temp_r[i];
temp.push_back({temp_l[i], temp_r[i]});
}
sort(temp.begin(), temp.end());
for (int i = 0; i < temp.size(); i++) {
PII now = temp[i];
while(i + 1 < temp.size() && temp[i + 1].first <= now.second) {
now = {now.first, max(now.second, temp[i + 1].second)},
i++;
}
block2.push_back(now);
}
long long res = 0;
for (int i = 0; i < block1.size(); i++)
for (int j = 0; j < block2.size(); j++)
res += get_common(block1[i], block2[j]);
cout << res << endl;
return 0;
}