牛客周赛 Round 31
文章目录
- 牛客周赛 Round 31
- A 小红小紫替换
- B 小红的因子数
- C 小红的字符串中值
- D 小红数组操作
- E 小红的子集取反
A 小红小紫替换
语法
#include <bits/stdc++.h>using namespace std;int main()
{string s;cin >> s;if(s == "kou"){cout << "yukari";}else{cout << s;}return 0;
}
B 小红的因子数
分解质因数
#include <bits/stdc++.h>using namespace std;int main()
{long long n;cin >> n;if(n == 1){cout << 0;return 0;}int ans = 0;for(int i = 2 ; i <= n / i ; i ++){if(n % i == 0){ans ++;while(n % i == 0){n /= i;} }}if(n > 1)ans ++;cout << ans;return 0;
}
C 小红的字符串中值
看左右两边最短的
#include <bits/stdc++.h>using namespace std;int main()
{int n;char k;cin >> n >> k;string s;cin >> s;long long ans = 0;int len = s.size();for(int i = 0 ; i < len; i ++){if(s[i] == k){ans += min(i, (len - i - 1)) + 1;}}cout << ans;return 0;
}
D 小红数组操作
模拟双链表
#include <bits/stdc++.h>using namespace std;map<int,int> l , r;//l[i]为i的左节点 , r[i]为i的右节点int main()
{int n;cin >> n;int start = -1e9-1 , end = 1e9+1;r[start] = end;l[end] = start;while(n --){int op;cin >> op;if(op == 1){int a , b;cin >> a >> b;int ll , rr; // a插在ll , rr之间if(b == 0){ll = start;rr = r[start];}else{ll = b;rr = r[b];}r[ll] = a;r[a] = rr;l[a] = ll;l[rr] = a;}else{int t;cin >> t;int ll = l[t] , rr = r[t];r[ll] = rr;l[rr] = ll;}}int u = start , cnt = 0;while(r[u] != end){cnt ++;u = r[u];}u = start;cout << cnt << endl;while(r[u] != end){cout << r[u] << " ";u = r[u];}return 0;
}
E 小红的子集取反
变种的背包dp
dp[i][j] //表示前i个数字得到j要取反的最小次数
#include <bits/stdc++.h>using namespace std;int dp[202][80005];int main()
{int n;cin >> n;memset(dp , 0x3f , sizeof dp);dp[0][40000] = 0;for(int i = 1 ; i <= n ; i ++){int x;cin >> x;for(int j = 0 ; j <= 80000 ; j ++){if(j - x >= 0 && j - x <= 80000)dp[i][j] = min(dp[i][j] , dp[i - 1][j - x] + 1);if(j + x >= 0 && j + x <= 80000)dp[i][j] = min(dp[i][j] , dp[i - 1][j + x]);}}if(dp[n][40000] == 0x3f3f3f3f){cout << "-1";}else{cout << dp[n][40000];}return 0;
}