Educational Codeforces Round 175 (Rated for Div. 2)
题目链接:https://codeforces.com/contest/2070
A. FizzBuzz Remixed
思路:
要使得x mod 3 == x mod 5,很明显当x是15的倍数时满足
再观察可以知道,当x = 15*k + m (m为0 1 2) 时都满足条件
故可写码了
代码:
//#include <iostream>
//#include <algorithm>
//#include<cstring>
//#include<cctype>
//#include<string>
//#include <set>
//#include <vector>
//#include <cmath>
//#include <queue>
//#include <unordered_set>
//#include <map>
//#include <unordered_map>
//#include <stack>
//#include <memory>
//using namespace std;
//#define ll long long
//#define yes cout << "YES" << endl
//#define no cout << "NO" << endl
//
(0 1 3 4 6 7 9) * 3
//
//void solve()
//{
// int n;
// cin >> n;
// ll ans = 0;
// ans += (n / 15+1) * 3;
// for (int j = 0; j < 3; j++)
// {
// if (15 * (n / 15) + j > n)
// {
// ans--;
// }
// }
// cout << ans << endl;
//}
//
//int main()
//{
// //for (int i = 0; i <= 1000; i++)
// //{
// // if (i >= 0 && i % 3 == i % 5)
// // cout << i << endl;
// //}
// cin.tie(0)->sync_with_stdio(false);
// int t = 1;
// cin >> t;
// while (t--)
// {
// solve();
// }
// return 0;
//}
B. Robot Program
思路:
模拟一下即可,先判断能不能回到0点,如果可以会到0点,那么再判断一下能不能再次回到0点,如果能,就能得到步数k,之后直接除即可
代码:
//#include <iostream>
//#include <algorithm>
//#include<cstring>
//#include<cctype>
//#include<string>
//#include <set>
//#include <vector>
//#include <cmath>
//#include <queue>
//#include <unordered_set>
//#include <map>
//#include <unordered_map>
//#include <stack>
//#include <memory>
//using namespace std;
//#define ll long long
//#define yes cout << "YES" << endl
//#define no cout << "NO" << endl
//
//void solve()
//{
// ll n, x, k;
// cin >> n >> x >> k;
// string s;
// cin >> s;
// int time1 = 0;
// for (int i = 0; i < n; i++)
// {
// x += (s[i] == 'L') ? -1 : 1;
// if (x == 0)
// {
// time1 = 1+i;
// break;
// }
// }
// int time2 = 0;
// for (int i = 0; i < n; i++)
// {
// x += (s[i] == 'L') ? -1 : 1;
// if (x == 0)
// {
// time2 = 1 + i;
// break;
// }
// }
// ll ans = 0;
// if (time1 != 0)
// {
// ans++;
// k -= time1;
// if(time2)
// ans += k / time2;
// }
// cout << ans << endl;
//}
//
//int main()
//{
// cin.tie(0)->sync_with_stdio(false);
// int t = 1;
// cin >> t;
// while (t--)
// {
// solve();
// }
// return 0;
//}
C. Limited Repainting
思路:
一道二分+贪心的题目
我们可以二分答案,然后check一下是否满足,那么难点就在于check函数了
现在我们假设答案为mid,那么就有以下几种情况
① s[i] = B
如果a[i] < mid,如果我们还没开始涂,那我们跳过即可,因为最后的答案是取所有a[i]的最大值,此时不涂对答案也没影响,如果我们前面已经涂了,那么再涂这个也无妨,因为涂的越多肯定越好,比如有
BBB
mid mid-1 mid
那我们如果不涂第二个的话,我们就至少需要两次才能满足,但是涂了第二个一次就能连续涂完
否则我们必须涂
② s[i] = R
如果我们还没开始涂,那么直接跳过即可
否则,如果a[i] > mid,我们就不能涂,因为此时答案就不为mid了,否则直接涂就行,理由同上,多涂更好
代码:
//#include <iostream>
//#include <algorithm>
//#include<cstring>
//#include<cctype>
//#include<string>
//#include <set>
//#include <vector>
//#include <cmath>
//#include <queue>
//#include <unordered_set>
//#include <map>
//#include <unordered_map>
//#include <stack>
//#include <memory>
//using namespace std;
//#define ll long long
//#define yes cout << "YES" << endl
//#define no cout << "NO" << endl
//
//void solve()
//{
// int n, k;
// cin >> n >> k;
// string s;
// cin >> s;
// vector<int> a(n);
// for (int i = 0; i < n; i++)
// {
// cin >> a[i];
// }
// int l = 0, r = 1e9+1;
// auto check = [&](int mid) -> bool
// {
// int cnt = 0;
// for (int i = 0; i < n; i++)
// {
// if (a[i] > mid && s[i] !='R')
// {
// int j = i + 1;
// while (j < n && (s[j] == 'B' || a[j] <= mid))
// {
// j++;
// }
// i = j - 1;
// cnt++;
// }
// }
// return cnt <= k;
// };
// while (l < r)
// {
// int mid = (l+r) / 2;
// if (check(mid))
// {
// r = mid;
// }
// else
// {
// l = mid+1;
// }
// }
// cout << r << endl;
//}
//
//int main()
//{
// cin.tie(0)->sync_with_stdio(false);
// int t = 1;
// cin >> t;
// while (t--)
// {
// solve();
// }
// return 0;
//}
D. Tree Jumps
思路:
首先要理解题意,我们只能从 当前节点 跳至 非自身子节点 的 节点
那我们可以反过来考虑,对于任意一个节点,有多少方式跳过来
我们可以分层考虑
对于根节点(第一层)只有一种
对于第二层也是只有一种,因为有且只能从根节点跳过来(题目中的特例)
对于第三层的节点,任意一个节点的跳法有 node[i].val = sum[2] - node[node[i].father].val 种
其中sum[i]为第i层所有节点的跳法总和,node[i].val为节点i的跳法
同理,对于第n层也是这样
那么最后的答案就为每层sum之和
(这里要注意大数取模)
代码:
//#include <iostream>
//#include <algorithm>
//#include <cstring>
//#include <cctype>
//#include <string>
//#include <set>
//#include <vector>
//#include <cmath>
//#include <queue>
//#include <unordered_set>
//#include <map>
//#include <unordered_map>
//#include <stack>
//#include <memory>
//using namespace std;
//
//const int MOD = 998244353;
//
//struct Z {
// long long val;
// Z(long long v = 0) : val(v% MOD) {
// if (val < 0) val += MOD;
// }
// Z& operator+=(const Z& other) {
// val = (val + other.val) % MOD;
// return *this;
// }
// Z& operator-=(const Z& other) {
// val = (val - other.val + MOD) % MOD;
// return *this;
// }
// Z& operator*=(const Z& other) {
// val = (val * other.val) % MOD;
// return *this;
// }
// friend Z operator+(Z a, const Z& b) { return a += b; }
// friend Z operator-(Z a, const Z& b) { return a -= b; }
// friend Z operator*(Z a, const Z& b) { return a *= b; }
// friend ostream& operator<<(ostream& os, const Z& z) {
// return os << z.val;
// }
//};
//
//struct MyStruct
//{
// int father = -1;
// int dep = 1;
// Z val = 0; // 修改为Z类型
// int self = 1;
//};
//
//void solve()
//{
// int n;
// cin >> n;
// vector<MyStruct> node(n + 1);
// int maxdep = 0;
// node[1].val = 1; // 隐式转换为Z
// for (int i = 2; i <= n; i++)
// {
// cin >> node[i].father;
// node[i].dep = node[node[i].father].dep + 1;
// node[i].self = i;
// maxdep = max(maxdep, node[i].dep);
// }
// vector<vector<MyStruct>> depnode(maxdep + 1);
// vector<Z> depSum(maxdep + 1, 0); // 改为vector<Z>
// for (int i = 1; i <= n; i++)
// {
// depnode[node[i].dep].push_back(node[i]);
// }
// depSum[1] = 1;
// for (int i = 2; i <= maxdep; i++)
// {
// for (size_t j = 0; j < depnode[i].size(); j++)
// {
// if (i == 2)
// node[depnode[i][j].self].val = 1;
// else
// node[depnode[i][j].self].val = depSum[i - 1] - node[depnode[i][j].father].val;
// depSum[i] += node[depnode[i][j].self].val;
// }
// }
// Z ans = 0; // 改为Z类型
// for (int i = 1; i <= maxdep; i++)
// {
// ans += depSum[i];
// }
// cout << ans << endl;
//}
//
//int main()
//{
// cin.tie(0)->sync_with_stdio(false);
// int t = 1;
// cin >> t;
// while (t--)
// {
// solve();
// }
// return 0;
//}