20240729训练记录
20240729训练记录
- 若有多组数据,时空复杂度以单次为主
上午:暑期训练 6——限时训练
上午比较简单,就简略一点
A、CodeForces - 1399B
题意
有两个长为
对于
- 1、将
a_i 减一 - 2、将
b_i 减一 - 3、将
a_i 和b_i 减一
求将
思路
此题
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int T, n, a[55], b[55];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
for(int i = 1; i <= n; i++){
cin >> b[i];
}
int m1 = *min_element(a + 1, a + 1 + n);
int m2 = *min_element(b + 1, b + 1 + n);
long long ans = 0;
for(int i = 1; i <= n; i++){
ans += max(abs(a[i] - m1), abs(b[i] - m2));
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
B、CodeForces - 1385A
题意
给定三个整数
思路
可以想到,三元组
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int T, a[4];
void solve(){
cin >> a[1] >> a[2] >> a[3];
sort(a + 1, a + 4);
if(a[3] != a[2]){
cout << "NO\n";
}else{
cout << "YES\n" << a[3] << ' ' << a[1] << ' ' << 1 << '\n';
}
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
C、CodeForces - 1374A
题意
找到最大
思路
直接上公式:
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int T, x, y, n;
void solve(){
cin >> x >> y >> n;
cout << (n - y) / x * x + y << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
D、CodeForces - 1399C
题意
有
思路
既然要保证所有队伍两人体重之和相等,容易想到从两端往中间走,用双指针来找最大组数,由于
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int T, n, a[55];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + 1 + n);
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
int sum = 0, op = a[i] + a[j];
for(int l = 1, r = n; l < r; ){
if(a[l] + a[r] > op) {
r--;
}else if(a[l] + a[r] < op){
l++;
}else{
sum++, l++, r--;
}
}
ans = max(ans, sum);
}
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
E、CodeForces - 1385C
题意
有一个数组
-
从
a 的头或尾取出元素插入b 的末尾 -
重复
|a| 次后b 单调不减
则
求要使
思路
既然只能删前缀,那么从后往前找肯定能找到“好序列”。
可以动手推一推,就能发现“好序列”呈一段上坡加一段下坡,按此特性从后往前找最长的后缀即可即可
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int T, n, a[200010];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
int k = n;
while(k > 1 && a[k] <= a[k - 1]){
k--;
}
while(k > 1 && a[k] >= a[k - 1]){
k--;
}
cout << k - 1 << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
F、CodeForces - 1374C
题意
有长为
- 选择恰好一个括号 并将其移动到字符串的开头或末尾
思路
看到括号序列,肯定可以想到括号序列匹配,此题的操作又不需要考虑位置,所以可以直接将可匹配的括号去掉,剩下的右括号个数便是答案。
此题不需要栈维护,用一个变量
-
若当前为左括号,
cnt + 1 ; -
若当前为右括号,
cnt - 1 ; -
当
cnt 为负数,ans + 1, cnt = 0
输出
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int T, n;
string s;
void solve(){
cin >> n >> s;
stack<char> stk;
int sum = 0, f = 0;
for(int i = 0; i < n; i++){
sum += ((s[i] == ')') ? -1 : 1);
if(sum < 0){
sum = 0, f++;
}
}
cout << f <<'\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
G、CodeForces - 1399D
题意
给定一个由
思路
用两个队列维护
如果当前为
-
若之前有可接上的串,就直接接上
-
没有就新开一段,把这一段塞队列里
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
struct node{
int x, p, n;
}a[200010];
int T, n, ans[200010], h, t;
string s;
void solve(){
cin >> n >> s;
int op = 0;
queue<int> q[2];
for(int i = 0; i < n; i++){
int k = s[i] - '0';
if(q[k].empty()){
ans[i + 1] = ++op;
}else{
ans[i + 1] = ans[q[k].front() + 1], q[k].pop();
}
q[k ^ 1].push(i);
}
cout << op << '\n';
for(int i = 1; i <= n; i++){
cout << ans[i] << ' ';
}
cout << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
H、CodeForces - 1385D
题意
有一个长
思路
可以将
时空复杂度
时间:递归树有
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int T, n;
string s;
int dfs(int l, int r, char c){
if(l == r){
return s[l] != c;
}
int op = 0, s1 = 0, s2 = 0;
int mid = (l + r) / 2;
for(int i = l; i <= mid; i++){
s1 += s[i] != c;
}
for(int i = mid + 1; i <= r; i++){
s2 += s[i] != c;
}
return min(s1 + dfs(mid + 1, r, c + 1), s2 + dfs(l, mid, c + 1));
}
void solve(){
cin >> n >> s; s = ' ' + s;
cout << dfs(1, n, 'a') << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
I、CodeForces - 1374D
题意
给定一个由
-
选择从
1 到n 中的一个整数i 并将a_i 增加x (a_i = a_i + x) ,然后将x 增加1 (x = x + 1) -
仅将
x 增加1 (x = x + 1) 。
第一种操作可以应用在从
你的任务是求出获得这样一个数组所需的最小操作次数,使得数组中每个元素都能被
思路
看到整除可以想到同余,看到最多一次可以想到同余元素相差为
时空复杂度
时间:由于用了 map,所以为
空间:
代码
#include<bits/stdc++.h>
using namespace std;
long long T, n, m, a[200010];
map<int, int> cnt;
void solve(){
cnt.clear();
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
cnt[a[i] % m]++;
}
long long ans = 0;
for(auto [i, c] : cnt){
if(i > 0 && c > 0){
ans = max(ans, (c - 1) * m + m - i + 1);
}
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}
J、CodeForces - 1374E1
题意
有
- 有
k 本a 喜欢的和k 本b 喜欢的
求最小代价
思路
首先,可以想到如下贪心:
-
把所有都喜欢的书先买掉最小
k 本 -
再用只被一人喜欢的补充或代替较大的都喜欢的书
此贪心显然可行。
于是可以先分开记录三种书,从小到大排序,然后去求总价值即可
时空复杂度
时间:排序
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<int> v[4];
long long sum[4][200010];
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1, x, f1, f2; i <= n; i++){
cin >> x >> f1 >> f2;
v[f1 + 2 * f2].push_back(x);
}
for(int i = 1; i <= 3; i++){
sort(v[i].begin(), v[i].end());
for(int j = 1; j <= v[i].size(); j++){
sum[i][j] = sum[i][j - 1] + v[i][j - 1];
}
}
long long ans = ((v[3].size() >= m) ? sum[3][m] : 1e18);
for(int i = 1; i <= min(v[1].size(), v[2].size()); i++){
if(v[3].size() >= m - i){
ans = min(ans, sum[3][m - i] + sum[2][i] + sum[1][i]);
}
}
cout << ((ans == 1e18) ? -1 : ans);
return 0;
}
下午:训练
挑难的来讲
D - AtCoder - abc364_d K-th Nearest
题意
数轴上有
思路
这题显然可以用二分来求第
注:此题不要去重但要排序!!!
时空复杂度
时间:排序 + 二分 + check 总共
空间:
代码
#include<bits/stdc++.h>
using namespace std;
int n, m, a[100010], b, c;
bool check(int x){
return upper_bound(a + 1, a + 1 + n, b + x) - lower_bound(a + 1, a + 1 + n, b - x) >= c;
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a + 1, a + 1 + n);
while(m--){
cin >> b >> c;
int l = 0, r = 2e8, ans = 0;
while(l <= r){
int mid = (l + r) >> 1;
if(check(mid)){
ans = mid, r = mid - 1;
}else{
l = mid + 1;
}
}
cout << ans << '\n';
}
return 0;
}
E - AtCoder - abc364_e Maximum Glutton
题意
有
思路
显然是道DP。
先来考虑搜索:
状态:
转移:
接下来转DP:
状态:
转移:
可以发现
时空复杂度
时间:
空间:
代码
#include<bits/stdc++.h>
using namespace std;
struct node{
int x, y;
}a[85];
int n, u, v, dp[2][85][10010];
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> u >> v;
for(int i = 1; i <= n; i++){
cin >> a[i].x >> a[i].y;
}
for(int i = 1; i <= n; i++){
for(int j = 0; j <= max(u, v); j++){
dp[0][i][j] = dp[1][i][j] = 1e9;
}
}
for(int i = 1; i <= n; i++){
for(int j = i; j >= 1; j--){
for(int k = v; k >= a[i].y; k--){
dp[0][j][k] = min(dp[0][j][k], dp[0][j - 1][k - a[i].y] + a[i].x);
}
}
}
for(int i = 1; i <= n; i++){
for(int j = i; j >= 1; j--){
for(int k = u; k >= a[i].x; k--){
dp[1][j][k] = min(dp[1][j][k], dp[1][j - 1][k - a[i].x] + a[i].y);
}
}
}
int ans = 0;
for(int i = 0; i < 2; i++){
for(int j = 1; j <= n; j++){
for(int k = 0; k <= ((!i) ? v : u); k++){
if(dp[i][j][k] <= ((!i) ? u : v)){
ans = max(ans, j);
}
}
}
}
cout << min(ans + 1, n);
return 0;
}
F - CodeForces - 1399E1 Weights Division (easy version)
题意
给定一棵带权重的根树,顶点
树是一个没有环的连通图。有一个特殊的顶点叫做根。顶点
路径的权重是该路径上所有边的权重之和。从顶点到自身的路径的权重是
你可以进行零次或多次移动。在每次移动中,你选择一条边,将其权重除以
你的任务是找到使得从根到每个叶子的路径权重之和最多为
思路
可以先算出不做任何操作的所有权重,然后去尽量对影响权重大的边做操作即可
注:向下取整的问题一定要特别注意
时空复杂度
时间:算基础权重
空间:
代码
#include<bits/stdc++.h>
using namespace std;
struct edge{
long long x, w;
};
struct node{
long long x, sz, w;
};
bool operator < (const node &a, const node &b){
return a.w < b.w;
}
long long s, T, n, x, y, w, sum, sz[200010];
priority_queue<node> q, init;
vector<edge> v[200010];
void dfs(int x, int f){
for(auto [i, w] : v[x]){
if(i != f){
dfs(i, x);
sz[x] += sz[i], sum += w * sz[i];
q.push({w, sz[i], (w + 1) / 2 * sz[i]});
}
}
sz[x] = ((sz[x] == 0) ? 1 : sz[x]);
}
void solve(){
for(int i = 1; i <= n; i++){
v[i].clear(), sz[i] = 0;
}
cin >> n >> s;
sum = 0, q = init;
for(int i = 1; i < n; i++){
cin >> x >> y >> w;
v[x].push_back({y, w});
v[y].push_back({x, w});
}
dfs(1, 0);
int ans = 0;
while(sum > s && !q.empty()){
node t = q.top(); q.pop(), sum -= t.w, ans++;
q.push({t.x / 2, t.sz, (t.x / 2 + 1) / 2 * t.sz});
}
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> T;
while(T--){
solve();
}
return 0;
}