The 2025 ICPC South America - Brazil First Phase
The 2025 ICPC South America - Brazil First Phase
- L题
- I题
- F题
L题
模拟题目
#include <bits/stdc++.h>
using namespace std;
struct WordInfo {int x, y;int order; // 记录在字典中的顺序,用于平局判断!!!
};int main() {ios::sync_with_stdio(false),cin.tie(0),cout.tie(0),cout.tie(0);int n;cin >> n;unordered_map<string, WordInfo> dict;vector<string> dict_order; // 保存字典单词的顺序,用于后续遍历for (int i = 0; i < n; ++i) {string p;int x, y;cin >> p >> x >> y;dict[p] = {x, y, i};dict_order.push_back(p);}int m;cin >> m;vector<string> knowledge_base;int _ = 0;while (_ < m) {string s;cin>>s;knowledge_base.push_back(s);_++;}int Q, win_k;cin >> Q >> win_k;while (Q--) {int file;cin >> file;vector<string> query_words(file);for (int i = 0; i < file; ++i) {cin >> query_words[i];}vector<string> word_need;//符合要求的候选词int current_K = win_k;// 尝试从 current_K 递减到 1,寻找候选词while (current_K >= 1) {vector<string> target_window(query_words.end() - current_K, query_words.end());// 遍历知识库,暴力匹配知识库与窗口中连续且顺序完全一致的位置————按照题目要求//注意:这里knowledge_base.size()要用int强转,否则会有unsigned减到0之下变成大数的bugfor (int i = 0; i < (int)knowledge_base.size() - current_K; ++i) {bool match = true;for (int j = 0; j < current_K; ++j) {if (knowledge_base[i + j] != target_window[j]) {match = false;break;}}if (match) {word_need.push_back(knowledge_base[i + current_K]);}}if (!word_need.empty()) {break;}current_K--;}string ans = "*";if (!word_need.empty()) {long long maxh = -1e18; // 初始化为极小值string best_word;// 将上面从知识库中找到的单词进行比较向量积for (const string& d : dict_order) {auto& d_info = dict[d];long long val = 0;for (const string& ci : word_need) {int ci_x = 0, ci_y = 0;if (dict.count(ci)) {ci_x = dict[ci].x;ci_y = dict[ci].y;}val += (long long)d_info.x * ci_x + (long long)d_info.y * ci_y;// 注意类型转换开long long,防止溢出}if (val > maxh) {maxh = val;best_word = d;}}ans = best_word;}// 输出结果:查询单词 + 预测词for (int i = 0; i < file; ++i) {cout << query_words[i]<<" ";}cout << ans << "\n";}return 0;
}
I题
从后往前缩小范围
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int N = 1e5 + 5;pii stars[N];
signed main() {int N;cin >> N;for (int i = 0; i < N; i++) {cin >> stars[i].first >> stars[i].second;}vector<int> dist(N - 1);for (int i = 0; i < N - 1; i++) {dist[i] = abs(stars[i].first - stars[i + 1].first) + abs(stars[i].second - stars[i + 1].second);}vector<pii> range(N);range[N - 1] = {1, LLONG_MAX};for (int i = N - 2; i >= 0; i--) {int min_R = dist[i] - range[i + 1].second;int max_R = dist[i] - range[i + 1].first;min_R = max(min_R, 1LL);max_R = min(max_R, dist[i] - 1);// 如果范围无效,则任务不可能完成if (min_R > max_R) {cout << -1 << endl;return 0;}range[i] = {min_R, max_R};}cout << range[0].second << '\n';return 0;
}
作者本来用的二分,但是一直会卡第7个。不知为什么。
#include <iostream>
#include <vector>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int dist[N], n;
bool check(int mid) {int tence = mid;for (int i = 1; i < n; i++) {int t = dist[i] - tence;tence = t;if (t <= 0) {return false;}}return true;
}
signed main() {ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);cin>>n;vector<vector<int>> a(n + 1,vector<int>(2,0));for(int i = 1; i <= n; i++) {cin>>a[i][0]>>a[i][1];}int maxh = 0;for(int i = 2; i <= n; i++) {dist[i - 1] = abs(a[i][0] - a[i - 1][0] + a[i][1] - a[i - 1][1]);maxh = max(maxh, dist[i - 1]);}int l = 1, r = maxh;while (l < r) {int mid = l + (r - l) / 2;if (check(mid)) {l = mid + 1;}else {r = mid;}}if (l == 1) {cout<<-1<<'\n';return 0;}cout<<l - 1<<'\n';return 0;
}
F题
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const int N = 1e5 + 5;
int X[N];
ll qmi(ll a, ll b) {ll res = 1;a %= mod;while (b) {if (b & 1) res = res * a % mod;a = a * a % mod;b >>= 1;}return res;
}int main() {ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);ll inv2 = qmi(2, mod - 2);int N, Q;cin >> N >> Q;for (int i = 0; i < Q; i++) {cin >> X[i];}vector<ll> pow2(Q + 2), pow_inv2(Q + 2);//预处理pow2[0] = 1;pow_inv2[0] = 1;for (int i = 1; i <= Q + 1; i++) {pow2[i] = pow2[i - 1] * 2 % mod;pow_inv2[i] = pow_inv2[i - 1] * inv2 % mod;}ll S1 = 0;for (int j = 0; j < Q; j++) {S1 = (S1 + X[j] * pow_inv2[j]) % mod;}vector<ll> S(Q + 2, 0);for (int j = Q - 1; j >= 0; j--) {S[j + 1] = (X[j] * pow_inv2[j + 1] + S[j + 2]) % mod;}vector<vector<int>> table_num(N + 1);for (int t = 1; t <= Q; t++) {table_num[X[t - 1]].push_back(t);}vector<ll> ans(N + 1, 0);for (int i = 1; i <= N; i++) {ll S2 = 0;for (int t : table_num[i]) {S2 = (S2 + pow2[t] * S[t + 1]) % mod;}if (i == 1) {ans[i] = inv2 * (S1 + S2) % mod;} else {ans[i] = inv2 * S2 % mod;}}for (int i = 1; i <= N; i++) {cout << ans[i] << '\n';}return 0;
}