ZJCPC 2020 浙江省赛
K. Killing the Brute-force
题目大意
给定两个数组,问右侧的\(a_i*3<b_i\)的位置
解题思路
按题意模拟即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int tt;
std::cin >> tt;
while (tt--) {
int m, ans = -1;
std::cin >> m;
std::vector<int> a(m), b(m);
for (int i = 0; i < m; i++) {
std::cin >> a[i];
}
for (int i = 0; i < m; i++) {
std::cin >> b[i];
}
for (int i = 0; i < m; i++) {
if (a[i] * 3 < b[i]) {
ans = i + 1;
break;
}
}
std::cout << ans << "\n";
}
}
A. AD 2020
题目大意
给定两个日期,问中间有多少日期包涵“202”
解题思路
预处理后对字典序二分即可
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
std::vector<int> m = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
std::vector<std::string> ans;
for (int year = 2000; year <= 9999; year++) {
for (int month = 1; month <= 12; month++) {
int f = ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) && (month == 2);
for (int day = 1; day <= m[month - 1] + f; day++) {
std::string s = std::to_string(year);
if (month <= 9) {
s += "0";
}
s += std::to_string(month);
if (day <= 9) {
s += "0";
}
s += std::to_string(day);
if (s.find("202") != -1) {
ans.push_back(s);
}
}
}
}
std::sort(ans.begin(), ans.end());
int tt;
std::cin >> tt;
while (tt--) {
int y1, m1, d1, y2, m2, d2;
std::cin >> y1 >> m1 >> d1 >> y2 >> m2 >> d2;
std::string s1 = std::to_string(y1), s2 = std::to_string(y2);
if (m1 <= 9) {
s1 += "0";
}
s1 += std::to_string(m1);
if (d1 <= 9) {
s1 += "0";
}
s1 += std::to_string(d1);
if (m2 <= 9) {
s2 += "0";
}
s2 += std::to_string(m2);
if (d2 <= 9) {
s2 += "0";
}
s2 += std::to_string(d2);
std::cout << std::upper_bound(ans.begin(), ans.end(), s2) - std::lower_bound(ans.begin(), ans.end(), s1) << "\n";
}
}
I. Invoking the Magic
题目大意
给你n对不一定颜色匹配的袜子,将它们重新正确分组,问袜子数量最多的组有多少袜子
解题思路
对袜子颜色编号离散化,再映射颜色和编号并查集维护,答案就是最大的连通块大小
代码实现
#include <bits/stdc++.h>
using i64 = long long;
class DSU {
public:
int cnt;
std::vector<int> fa, rank, siz;
DSU(int n) : cnt(n), fa(n + 1), rank(n + 1), siz(n + 1, 1) {
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
}
int find(int x) {
if (fa[x] != x) {
fa[x] = find(fa[x]);
}
return fa[x];
}
void merge(int x, int y) {
int X = find(x), Y = find(y);
if (X != Y) {
if (rank[X] >= rank[Y]) {
fa[Y] = X;
siz[X] += siz[Y];
if (rank[X] == rank[Y]) {
rank[X]++;
}
} else {
fa[X] = Y;
siz[Y] += siz[X];
}
cnt--;
}
}
int size() {
return cnt;
}
int count(int x) {
return siz[find(x)];
}
};
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int tt;
std::cin >> tt;
while (tt--) {
int n, ans = 0, cnt = 1;
std::cin >> n;
std::set<int> st;
std::map<int, int> mp;
std::vector<std::array<int, 2>> g1, g2;
for (int i = 0; i < n; i++) {
int u, v;
std::cin >> u >> v;
st.insert(u);
st.insert(v);
g1.push_back({u, v});
}
for (auto x : st) {
mp[x] = cnt++;
}
for (auto [u, v] : g1) {
g2.push_back({mp[u], mp[v]});
}
DSU dsu(n);
for (auto [u, v] : g2) {
dsu.merge(u, v);
}
for (int i = 1; i <= n; i++) {
ans = std::max(ans, dsu.count(i));
}
std::cout << ans << "\n";
}
}
C. Crossword Validation
题目大意
给你一个字符矩阵要求计算所有匹配串的权值之和,无法匹配输出-1
解题思路
trie来记录m个单词以及它们的权值,之后遍历矩阵查找。(直接umap好像也跑的飞快)
代码实现
#include <bits/stdc++.h>
using i64 = long long;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int tt;
std::cin >> tt;
while (tt--) {
int n, m;
std::cin >> n >> m;
std::vector<std::string> g(n), S;
for (int i = 0; i < n; i++) {
std::cin >> g[i];
std::string s;
for (auto ch : g[i]) {
if (ch != '#') {
s += ch;
} else {
if (s.size()) {
S.push_back(s);
}
s.clear();
}
}
if (s.size()) {
S.push_back(s);
}
}
for (int j = 0; j < n; j++) {
std::string s;
for (int i = 0; i < n; i++) {
if (g[i][j] != '#') {
s += g[i][j];
} else {
if (s.size()) {
S.push_back(s);
}
s.clear();
}
}
if (s.size()) {
S.push_back(s);
}
}
std::unordered_map<std::string, int> ump;
for (int i = 0; i < m; i++) {
int x;
std::string s;
std::cin >> s >> x;
ump[s] = x;
}
i64 ans = 0;
for (auto s : S) {
if (ump.count(s)) {
ans += ump[s];
} else {
ans = -1;
break;
}
}
std::cout << ans << "\n";
}
}