2025年河南工业大学2025新生周赛(5)
A:A-B
简单的的高精度减法,数组模拟算术减法即可,注意范围。
#include<bits/stdc++.h> using namespace std; const int N = 1e6; int a[N], b[N], c[N]; int main() { string s1, s2; cin >> s1 >> s2; if (s1.size() < s2.size() || (s1.size() == s2.size() && s1 < s2)) { string s = s1; s1 = s2; s2 = s; cout << '-'; } reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int lena = s1.size(), lenb = s2.size(); int lenc = max(lena, lenb) + 1; for (int i = 0; i < lena; i++) a[i] = s1[i] - '0'; for (int i = 0; i < lenb; i++) b[i] = s2[i] - '0'; for (int i = 0; i < lenc; i++) { if (a[i] < b[i]) a[i + 1]--, a[i] += 10; c[i] = a[i] - b[i]; } while (lenc > 1 && !c[lenc - 1]) lenc--; for (int i = lenc - 1; i >= 0; i--) cout << c[i]; return 0; }
B:圣诞节前前前夕
签到题,注意int范围不够,开long long 即可,两种解法。
#include<bits/stdc++.h> using namespace std; const int N = 2e5+10; typedef long long ll; int main() { ll ans = 0; for(ll i =1;i<=20251125;i++) ans+=i; cout<<ans; return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 2e5+10; typedef long long ll; int main() { ll n = 20251125; ll res = (n + 1) * n / 2; cout << res << endl; return 0; }
C:NASA的食物补给
01背包题
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define ull unsigned long long #define ll long long #define PII pair<int,int> const int N = 2e3+10; const int mod = 1e9+7; const int P = 1; int n, m; int h, t; int f[N][N], c[N]; int v[N], w[N]; void solve() { cin >> h >> t; cin >> n; for (int i = 1; i <= n; i++) { int hh, tt, kk; cin >> hh >> tt >> kk; for (int j = h; j >= hh; j--) for (int k = t; k >= tt; k--) f[j][k] = max(f[j][k], f[j - hh][k - tt] + kk); } cout << f[h][t]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; //cin >> T; while (T--) solve(); return 0; }
D:接水
模拟题,s数组充当水龙头,模拟接水过程即可。
#include<iostream> #include<algorithm> using namespace std; int a[10005]; int n, m; int s[105]; int t; int main(void) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { t = 1; for (int j = 2; j <= m; j++) if (s[t] > s[j]) t = j; s[t] += a[i]; } int mx = 0; for (int i = 1; i <= m; i++) { mx = max(s[i], mx); } cout << mx; }
E:阳台接雨
线性dp,两个数组分别存储前后最大值,两边最大值的最小值与当前值相减即可。
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define ull unsigned long long #define ll long long #define PII pair<int,int> const ll N = 1e4+10; const int mod = 80112002; const int P = 1; void solve() { int n; cin>>n; vector<int> h(n); for(int i=0;i<n;i++) cin>>h[i]; vector<int> ma1(n+1,0),ma2(n+1,0); int sum1=0,sum2=0; for(int i=0;i<n;i++) { if(!i) ma1[i]=h[i]; else ma1[i]=max(ma1[i-1],h[i]); } for(int i=n-1;i>=0;i--) { ma2[i]=max(ma2[i+1],h[i]); } for(int i=0;i<n;i++) { sum1+=min(ma1[i],ma2[i]); sum2+=h[i]; } cout<< sum1-sum2<<endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; //cin >> T; while (T--) solve(); return 0; }
F:领导领导
lca板子题,有兴趣可以去了解,这里提供一个倍增法。
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define ull unsigned long long #define ll long long #define PII pair<int,int> const ll N = 1e4+10; const int mod = 80112002; const int P = 1; int n, m, s, a, b; vector<int> e[N]; int dep[N], fa[N][22]; void dfs(int u, int father) { dep[u] = dep[father] + 1; fa[u][0] = father; for (int i = 1; i <= 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1]; for (int v : e[u]) if (v != father) dfs(v, u); } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); for (int i = 20; i >= 0; i--) if (dep[fa[u][i]] >= dep[v]) u = fa[u][i]; if (u == v) return v; for (int i = 20; i >= 0; i--) { if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i]; } return fa[u][0]; } void solve() { cin >> n >> m >> s; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; e[x].push_back(y); e[y].push_back(x); } dfs(s, 0); while (m--) { int u, v; cin >> u >> v; cout << lca(u, v) << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; //cin >> T; while (T--) solve(); return 0; }
G:校园文化节宣传标语
双指针,剔除无关元素,前后对比即可。
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define ull unsigned long long #define ll long long #define PII pair<int,int> const ll N = 5e5 + 10; const ll Mod = 1e9 + 7; const int P = 1; bool check(const string& s) { int l = 0, r = s.size() - 1; while (l < r) { while (l < r && !isalpha(s[l])) l++; while (l < r && !isalpha(s[r])) r--; if (tolower(s[l++]) != tolower(s[r--])) return false; } return true; } void solve() { string s; getline(cin,s); cout<<(check(s)?"true":"false"); }
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T;
while (T--) solve();
return 0;
}
H:合并果子
贪心加堆,可用两个数组模拟顺序队列,每次最小的两个相加,加完重新插入排序,重复过程到只剩一个元素,总值即为答案。
#include<bits/stdc++.h> using namespace std; #define endl '\n' #define ull unsigned long long #define ll long long #define PII pair<int,int> const ll N = 1e5 + 10; const ll Mod = 1e9 + 7; const int P = 1; void solve() { priority_queue<int, vector<int>, greater<int>> pq; int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; pq.push(x); } int sum = 0; while (pq.size() > 1) { int a = pq.top(); pq.pop(); int b = pq.top(); pq.pop(); int temp = a + b; sum += temp; pq.push(temp); } cout << sum << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; //cin >> T; while (T--) solve(); return 0; }

浙公网安备 33010602011771号