模拟赛#1 | USACO19 DEC Bronze
题目链接:
A https://www.luogu.com.cn/problem/P5831
B https://www.luogu.com.cn/problem/P5832
C https://www.luogu.com.cn/problem/P5832
A
分析: 范围很小, 直接暴力枚举即可, 每一对奶牛枚举看看是不是一致, 考察循环程序设计, 时间复杂度\(O(n^2k)\)
代码:
/*
Author: SJ
*/
#include<bits/stdc++.h>
const int N = 1e2 + 10;
using ll = long long;
using ull = unsigned long long;
int k, n, re[N][N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> k >> n;
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= n; j++) {
int x;
std::cin >> x;
re[x][i] = j;
}
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i != j) {
bool flag = 1;
for (int p = 1; p <= k; p++) {
if (re[i][p] < re[j][p]) {
flag = 0;
break;
}
}
if (flag) ans++;
}
}
}
std::cout << ans;
return 0;
}
B
分析: 继续暴力, 考察string相关函数, 字符串操作, 时间复杂度\(O(n^3)\)
代码
/*
Author: SJ
*/
#include<bits/stdc++.h>
const int N = 1e5 + 10;
using ll = long long;
using ull = unsigned long long;
int n;
std::string s1;
std::vector<std::string> v1;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> s1;
for (int k = n; k >= 1; k--) {
for (int i = 0; i + k <= s1.size(); i++) {
std::string s3 = s1.substr(i, k);
// std::cout << s3 << ' ' << k << "\n";
for (auto i : v1) {
if (s3 == i) {
std::cout << k + 1;
return 0;
}
}
v1.push_back(s3);
}
}
return 0;
}
C
分析: 用next_permutation可以直接秒的, 莫名其妙挖掘了一堆没用的性质, 然后上了个贪心(强行加大代码难度bushi
代码:
/*
Author: SJ
*/
#include<bits/stdc++.h>
const int N = 1e5 + 10;
using ll = long long;
using ull = unsigned long long;
std::string s[10] = {
"Zeka", "Bessie", "Buttercup", "Belinda", "Beatrice", "Bella", "Blue", "Betsy", "Sue"
};
std::vector<int> g[10];
bool vis[N];
std::queue<int> q;
int query() {
int head = 0;
for (int i = 1; i <= 8; i++) {
if (!vis[i] && s[i] < s[head] && g[i].size() <= 1) {
head = i;
}
}
return head;
}
bool cmp(int a, int b) {
return s[a] < s[b];
}
void dfs(int x) {
std::cout << s[x] << "\n";
vis[x] = 1;
for (auto i : g[x]) {
if (!vis[i]) {
dfs(i);
}
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
for (int i = 1; i <= n; i++) {
std::string s1, s2;
std::cin >> s1;
int mark = 5;
while (mark--) std::cin >> s2;
// std::cout << s1 << ' ' << s2 << "\n";
int mark1, mark2;
for (int i = 1; i <= 8; i++) {
if (s[i] == s1) mark1 = i;
else if (s[i] == s2) mark2 = i;
}
g[mark1].push_back(mark2);
g[mark2].push_back(mark1);
}
for (int i = 1; i <= 8; i++) std::sort(g[i].begin(), g[i].end(), cmp);
for (int i = 1; i <= 8; i++) {
int head = query();
if (!head) break;
dfs(head);
}
return 0;
}
这个贪心很显然是对的
用next_permutation的代码:
咕咕咕

浙公网安备 33010602011771号