不要问我前三场哪去了,被wwb次掉了
| 题目编号 | T1 | T2 | T3 | T4 | 总结 |
|---|---|---|---|---|---|
| 分数 | 100 | 60 | 10 | 20 | 190 |
| 耗时 | 30min | 25min | 5min | 2.5h | 3.5h |
赛时思考过程:
按顺序开题T1,花了约20min写题,发现大样例没过调了10min,往后一翻,欸,这T4不水吗,随开码,约1.5h后发现不对,我怎么写了100行?换题打了T2暴力赛后发现是正解,数组开小了,挂了40,qwq后一直调无果,骗了T3的样例,下线了
T1
算出每个牧场本来的音量,并对其每次跑一遍完全背包,统计答案即可
代码:
#include <bits/stdc++.h>
using namespace std;
int n, b, ans;
int v[105], m[105], dp[100005];
int solve(int x) {
for (int i = 0; i <= x; i++) dp[i] = 0x3f3f3f3f;
dp[0] = 0;
for (int i = 1; i <= b; i++)
for (int j = v[i]; j <= x; j++)
dp[j] = min(dp[j], dp[j - v[i]] + 1);
return dp[x];
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> b;
for (int i = 1; i <= b; i++) cin >> v[i];
for (int i = 1; i <= n; i++) cin >> m[i];
for (int i = n; i >= 2; i--) {
m[i] -= max(m[i - 1] - 1, 0);
if(m[i]<0){
cout<<-1;
return 0;
}
}
for (int i = 1; i <= n; i++) {
int p = solve(m[i]);
if (p >= 100000) {
cout << -1;
return 0;
}
ans += p;
}
cout << ans;
return 0;
}T2
我数组为什么开小力!!!
实际上由于数据过水,直接排序后模拟即可,注意剪枝。
正解也十分的简单,依旧要排序,直接使用二分找到第一个大于等于输入的字符串,并给他加上 ,判断是否成立即可。
暴力代码:
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 3e4 + 5;
int n, q;
struct node {
string str;
int id;
} s[N];
bool cmp(node x, node y) {
return x.str < y.str;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> s[i].str;
s[i].id = i;
}
sort(s + 1, s + 1 + n, cmp);
while (q--) {
int x;
string p;
cin >> x >> p;
int cnt = 0;
for (int i = 1; i <= n; i++) {
bool flag = 0;
for (int j = 0; j < p.size(); j++) {
if (p[j] != s[i].str[j]) {
flag = 1;
break;
}
}
cnt += (!flag);
if (cnt == x) {
cout << s[i].id << endl;
break;
}
}
if (cnt < x) {
cout << -1 << endl;
}
}
return 0;
}正解代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e4 + 5;
int n, q;
pair<string, int> s[N];
bool check(string x, string y) {
if (y.size() > x.size()) return 0;
return x.substr(0, y.size()) == y;
}
signed main() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> s[i].first;
s[i].second = i;
}
sort(s + 1, s + 1 + n);
while (q--) {
int pos;
string p;
cin >> pos >> p;
int id = lower_bound(s + 1, s + 1 + n, make_pair(p, 0)) - s - 1 + pos;
if (id > n || !check(s[id].first, p)) {
cout << -1 << endl;
continue;
}
cout << s[id].second << endl;
}
return 0;
}T3:
考场没时间写暴力了qwq,也是本场唯一一道一点思路都没有的题。
代码实际上不难,难在将 W 与 S 转化成 与 ,统计前缀和后进行分类讨论。思维题做少了
因为白牛可以转化成斑点牛,所以一段区间内的白牛数量一定要大于等于斑点牛数量,且牛的总数一定要为偶数。在排序后,我们定义为在第 位之前,白牛的数量 斑点牛的数量,再将白牛看做 ,斑点牛看做 ,做一遍前缀和。此时会有以下几种情况
-
此时会有两种情况,一是 ,即 为奇数的情况,此时我们要选择排序后的第二头牛,否则白牛与斑点牛的数量会不相等;二是 ,即 为偶数的情况,此时我们要选择直接选择第一头牛,这样一定最优
-
此时在前 只牛中, 斑点牛数量大于白牛数量,则无法平衡两牛数量,需要找到一个在它前面的 ,使 , 即找到一个位置,使从第 位到第 位的白牛数量不小于斑点牛数量,而我们则直接找到最早的使 的位置 即可,原因如下:
若 因为一只奶牛贡献为 所以在 之前一定出现过 ,且位置更小答案更优。
对于第一种情况,我们直接进行分讨即可;对于第二种情况我们开一个桶,在 第一次出现时记录下标,之后再遇见时便直接调用即可,但注意下标要 ,因为直接调用会使斑点牛的数量多 ,所以就要跳到下一头牛来计算,因为在前缀和数组中计算 到 的公式是 ,所以当 时,实际上是原数组的第 位到第 位的和为
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int n, ans;
struct node {
int id, x;
} a[N];
bool cmp(node x, node y) {
return x.id < y.id;
}
map<int, int> p;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
char c;
cin >> a[i].id >> c;
if (c == 'W')
a[i].x = 1;
else
a[i].x = -1;
}
sort(a + 1, a + 1 + n, cmp);
p[0] = 0;
for (int i = 1; i <= n; i++) {
a[i].x += a[i - 1].x;
if (p[a[i].x] == 0 && a[i].x < 0)
p[a[i].x] = i;
else if (a[i].x < 0)
ans = max(ans, a[i].id - a[p[a[i].x] + 1].id);
else if (a[i].x >= 0 && a[i].x % 2 == 0)
ans = max(ans, a[i].id - a[1].id);
else if (a[i].x >= 0 && a[i].x % 2 == 1)
ans = max(ans, a[i].id - a[2].id);
}
cout << ans;
return 0;
}T4:
思路正确,代码全是bug,原写法写了100多行还是错的qwq
阅读理解题+细节题,一句话做法:
对两种边权各建一个反图,并跑一遍最短路,后记录每条边经过次数,再建新图跑最短路,输出答案即可。
代码:
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int N = 5e4 + 5, inf = 1e18;
int n, m, dis[N][2], new_dis[N], vis[N];
vector<pii> e[N][2], new_[N];
priority_queue<pii, vector<pii>, greater<pii>> q;
struct edge {
int u, v, w1, w2;
} Edge[N];
void dij(int p) {
while (!q.empty()) q.pop();
q.emplace(0, n);
for (int i = 1; i <= n; i++) dis[i][p] = 0x3f3f3f3f;
memset(vis, 0, sizeof(vis));
dis[n][p] = 0;
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (auto [v, w] : e[u][p]) {
if (dis[v][p] > dis[u][p] + w) {
dis[v][p] = dis[u][p] + w;
q.emplace(dis[v][p], v);
}
}
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1, u, v, w1, w2; i <= m; i++) {
cin >> u >> v >> w1 >> w2;
e[v][0].emplace_back(u, w1);
e[v][1].emplace_back(u, w2);
Edge[i] = {u, v, w1, w2};
}
dij(0);
dij(1);
for (int i = 1; i <= m; i++) {
int u = Edge[i].u, v = Edge[i].v, w = (dis[v][0] + Edge[i].w1 != dis[u][0]) + (dis[v][1] + Edge[i].w2 != dis[u][1]);
new_[u].emplace_back(v, w);
}
while (!q.empty()) q.pop();
for (int i = 1; i <= n; i++) new_dis[i] = 0x3f3f3f3f;
memset(vis, 0, sizeof(vis));
new_dis[1] = 0;
q.emplace(0, 1);
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (auto [v, w] : new_[u]) {
if (new_dis[v] > new_dis[u] + w) {
new_dis[v] = new_dis[u] + w;
q.emplace(new_dis[v], v);
}
}
}
cout << new_dis[n];
return 0;
}后记:
考后15min调出T4崩溃了ε=ε=ε=(#>д<)ノ