A - Adjacent Product
大意
给定一些数,输出它们两两之间的乘积
思路
直接模拟即可。
代码
#include <iostream>
#include <vector>
using namespace std;
int main(){int n;cin>>n;vector<int> a(n);for(auto &x: a) cin >> x;for(int i = 0; i< n - 1; i++) cout << a[i] * a[i+1] << " ";return 0;
}
B - Piano
大意
给定一个由
无限拼接的字符串。
给定,问是否由一个子串,其有
个
, 个
。
思路
枚举子串的起点,只有12种情况。
对于每一个可能的起点,统计其后的个字符,看看是否满足上述需求即可。
时间复杂度。
代码
#include<iostream>
#include<unordered_map>
using namespace std;int main(){int w, b;cin >> w >> b;int len = w + b;const string s = "wbwbwwbwbwbw";bool flag = false;for(int i = 0; i < s.size(); i++){unordered_map<char, int> mp;for(int j = i, cnt = 0; cnt < len; j = (j + 1) % s.size(), cnt++) mp[s[j]]++;if(mp['w'] == w && mp['b'] == b){flag = true;break;}}if(flag) cout << "Yes" << endl;else cout << "No" << endl;return 0;
}
C - Σ
大意
给定序列,求
中所有数的和,但要排除
中出现的数字。
思路
首先,算出后减去
中出现过的数即可。
注意要对去重,或者使用set。
代码
#include<iostream>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;int main(){ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);LL n, k;cin >> n >> k;vector<LL> a(n);for(auto &x: a) cin >> x;set<LL> b(a.begin(), a.end());LL ans = k * (k + 1) / 2;for(auto &x: b){if(x <= k) ans -= x;}cout << ans << endl;return 0;
}
D - Gomamayo Sequence
大意
给定一个字符串
,对第
位取反需要
的代价。
定义一个好的字符串,当且仅当只有一个相邻位置上的数字是相同的。
问将字符串变成好的字符串的最小代价。
思路
长度为的好串只有
个,我们可以枚举所有的情况。
我们枚举相邻数字相同的位置,然后计算变成和
这两种情况的代价,取最小值。
可以设前缀和与后缀和数组,事先计算所有前缀和后缀变成
和
的代价,通过前缀代价+后缀代价,就可以得到当前枚举的情况的代价。
代码
#include<iostream>
#include<vector>
using namespace std;
#define int long long
typedef long long LL;
const int INF = 1e18 + 10;template<class T>
T min(T a, T b, T c){return min(min(a, b), c);
}signed main(){LL n;string s, t="01";cin >> n >> s;vector<LL> c(n);for(auto &x: c) cin>>x;vector<LL> pre1(n), pre2(n), suf1(n), suf2(n);for(int i = 0; i < n; i++){pre1[i] = (s[i] == t[i & 1]? c[i]: 0) + (i > 0? pre1[i - 1]: 0);pre2[i] = (s[i] == t[~i & 1]? c[i]: 0) + (i > 0? pre2[i - 1]: 0);}for(int i = n - 1; i >= 0; i--){suf1[i] = (s[i] == t[i & 1]? c[i]: 0) + (i < n - 1? suf1[i + 1]: 0);suf2[i] = (s[i] == t[~i & 1]? c[i]: 0) + (i < n - 1? suf2[i + 1]: 0);}LL ans = INF;for(int i = 0; i < n - 1; i++){ans = min(ans, pre1[i] + suf2[i + 1], pre2[i] + suf1[i + 1]);}cout << ans << endl;return 0;
}