A Healthy Breakfast
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
const int N = 210;
#define int long long
signed main()
{
bool flag = false;
string s; cin >> s;
if (s[0] == 'R' || (s[1] == 'R' && s[2] == 'M')) flag = true;
if (flag) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
B - Vertical Reading
每次选择一个长度为len的区间每个区间的相同位置进行选点,而不是每隔len的长度跳一步
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
const int N = 210;
#define int long long
signed main()
{
string s, t; cin >> s >> t;
bool flag = false;
for (int len = 1; len < s.size(); len ++)
{
for (int i = 0; i < len; i ++)
{
string p = "";
for (int j = i; j < s.size(); j += len) p += s[j];
if (p == t)
{
flag = true;
break;
}
}
}
if (flag) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}
C - Move It
每次选择同组内最小的即可
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
const int N = 1e5 + 10;
#define int long long
struct Node
{
int a, w;
bool operator<(const Node& T) const
{
return w < T.w;
}
}q[N];
signed main()
{
int n; cin >> n;
map<int,int> mp;
for (int i = 0; i < n; i ++) cin >> q[i].a, mp[q[i].a] ++;
for (int i = 0; i < n; i ++) cin >> q[i].w;
sort(q, q + n);
int sum = 0;
for (int i = 0; i < n; i ++)
{
if (mp[q[i].a] > 1)
{
sum += q[i].w;
mp[q[i].a] --;
}
}
cout << sum << endl;
return 0;
}
D - Ghost Ants
首先考虑最简单的情况:两只蚂蚁相向而行,如果二者的距离在2t的时间内则能碰面。于是我们可以先将两个方向的蚂蚁分别进行存储,然后进行匹配。但是时间复杂度不能超过O(n^2),所以我们需要进行优化:将两个方向的蚂蚁进行排序,然后使用二分查找得到二者距离在[0,2t]之内的蚂蚁即为答案。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
const int N = 2e5 + 10;
#define int long long
int a[N], b[N];
int n1, n2;
signed main()
{
int n, t; cin >> n >> t;
string s; cin >> s;
for (int i = 0; i < n; i ++)
if (s[i] == '1') cin >> a[n1 ++];
else cin >> b[n2 ++];
sort(a, a + n1);
sort(b, b + n2);
int sum = 0;
for (int i = 0; i < n1; i ++)
{
int l = lower_bound(b, b + n2, a[i]) - b;
int r = upper_bound(b, b + n2, a[i] + 2 * t) - b;
sum += r - l;
}
cout << sum << endl;
return 0;
}