AtCoder-Beginner-Contest-391
Lucky Direction
map直接映射就好
#include <bits/stdc++.h>
using namespace std;
int main(){
map<string, string> mp;
mp["N"] = "S";
mp["S"] = "N";
mp["W"] = "E";
mp["E"] = "W";
mp["NE"] = "SW";
mp["SW"] = "NE";
mp["NW"] = "SE";
mp["SE"] = "NW";
string s;
cin >> s;
cout << mp[s] << endl;
return 0;
}
Seek Grid
思路: 数据较小,直接枚举比较即可
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
vector<string> a(n), b(m);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < m; i++) {
cin >> b[i];
}
auto check = [&](int x, int y)->bool {
for(int i = x; i < x + m; i++) {
for(int j = y; j < y + m; j++) {
if(a[i][j] != b[i - x][j - y]) {
return false;
}
}
}
return true;
};
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(n - i < m || n - j < m) continue;
if(check(i, j)){
cout << i + 1 << ' ' << j + 1 << endl;
return 0;
}
}
}
return 0;
}
Pigeonhole Query
思路: 记录每一个鸽子在哪个巢中, cnt记录巢中鸽子数量大于1的个数
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, q;
cin >> n >> q;
vector<int> a(n + 1, 1);
vector<int> mp(n + 1);
for(int i = 1; i <= n; i++) mp[i] = i;
int cnt = 0;
while(q--) {
int x, p, k;
cin >> x;
if(x == 2) {
cout << cnt << endl;
}else{
cin >> p >> k;
int tp = p;
p = mp[p];
a[p]--;
a[k]++;
mp[tp] = k;
if(a[p] == 1) cnt--;
if(a[k] == 2) cnt++;
}
}
return 0;
}
Gravity
思路: 只需要预处理每一个块在哪一个时间点消失即可,可以用一个优先队列数组记录每一列的方块。每次取出每一列最小的块,求所有的最大值,这一系列块的消失时间就是最大值。 如果某一列为空,这表示剩下的块不可能消失。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
int main() {
int n, w;
cin >> n >> w;
vector<priority_queue<pii, vector<pii>, greater<pii>>> vec(w + 1);
for(int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
vec[x].push({y, i});
}
vector<int> cnt(n + 1, INT_MAX);
while(1) {
int f = 0;
int mx = 0;
for(int i = 1; i <= w; i++) {
if(vec[i].empty()) {
f = 1;
break;
}
mx = max(mx, vec[i].top().first);
}
if(f) break;
for(int i = 1; i <= w; i++) {
cnt[vec[i].top().second] = mx - 1;
vec[i].pop();
}
}
int q;
cin >> q;
while(q--) {
int t, p;
cin >> t >> p;
if(cnt[p] >= t) {
cout << "Yes\n";
}else{
cout << "No\n";
}
}
}
Hierarchical Majority Vote
思路: 观察数据范围,暴搜。
#include <bits/stdc++.h>
using namespace std;
#define int long long
int check(string x) {
int t = x.size() / 3;
if(t == 1) {
int p = x[0] + x[1] + x[2] - 3 * '0';
if(p >= 2) return 1;
else return 0;
}
int x1 = check(x.substr(0, t));
int x2 = check(x.substr(t, t));
int x3 = check(x.substr(t + t));
int p = x1 + x2 + x3;
if(p >= 2) return 1;
else return 0;
}
signed main(){
int n;
cin >> n;
string s;
cin >> s;
int f = check(s);
auto dfs = [&](auto&& dfs, string x)->int {
int t = x.size() / 3;
if(t == 1) {
int p = x[0] + x[1] + x[2] - 3 * '0';
if(p >= 2) p = 1;
else p = 0;
if(p != f) return 0;
if(x[0] == x[1] && x[1] == x[2])
return 2;
return 1;
}
int x1 = dfs(dfs, x.substr(0, t));
int x2 = dfs(dfs, x.substr(t, t));
int x3 = dfs(dfs, x.substr(t + t));
// int p = x1 + x2 + x3;
// cout << x1 << ' ' << x2 << ' ' << x3 << endl;
int p1 = check(x.substr(0, t));
int p2 = check(x.substr(t, t));
int p3 = check(x.substr(t + t));
if(p1 == p2 && p2 == p3) {
return min({x1 + x2, x1 + x3, x2 + x3});
}else{
if(p1 == p2) return min(x1, x2);
if(p2 == p3) return min(x2, x3);
if(p1 == p3) return min(x1, x3);
}
};
cout << dfs(dfs, s) << endl;
return 0;
}
K-th Largest Triplet
思路: 暂无
浙公网安备 33010602011771号