当前位置:   article > 正文

2024睿抗机器人开发者大赛CAIP编程赛题解(c++)_2024caip

2024caip

题目地址(补题)

PTA | 程序设计类实验辅助教学平台

RC-u1 热҈热҈热҈

简单模拟题,没什么好说的 : 

  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. const int N = 55 ;
  4. int a[N] ;
  5. int main(){
  6. int n , w ; cin >> n >> w ;
  7. for(int i=1;i<=n;i++) cin >> a[i] ;
  8. int t = w ;
  9. int x = 0 , y = 0 ;
  10. for(int i=1;i<=n;i++){
  11. if(a[i]>=35){
  12. if(t!=4) x++ ;
  13. else y++ ;
  14. }
  15. t = (t + 8) % 7 ;
  16. }
  17. cout << x << " " << y << endl ;
  18. }

RC-u2 谁进线下了?

简单模拟题,没什么好说的 : 

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define endl '\n'
  4. int yss[21] = {0, 12, 9, 7, 5, 4, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0};
  5. int get(int x){
  6. return yss[x] ;
  7. }
  8. int main(){
  9. int n ; cin >> n ;
  10. vector<int> a(21,0) ;
  11. for(int i=1;i<=n;i++){
  12. for(int j=1;j<=20;j++){
  13. int p , k ; cin >> p >> k ;
  14. a[j] += k + get(p) ;
  15. }
  16. }
  17. for(int i=1;i<=20;i++){
  18. cout << i << " " << a[i] << endl ;
  19. }
  20. return 0;
  21. }

RC-u3 暖炉与水豚

简单模拟题,没什么好说的 (注意看清题目就ok): 

  1. #include<bits/stdc++.h>
  2. using namespace std ;
  3. const int N = 1010 ;
  4. int n , m ;
  5. char a[N][N] ;
  6. bool st[N][N] ;
  7. /**
  8. wm....mw
  9. .w..ww..
  10. ..wm.wwm
  11. w.w....w
  12. .m.c.m..
  13. w.....w.
  14. */
  15. void f(int i , int j){
  16. for(int x=-1;x<=1;x++){
  17. for(int y=-1;y<=1;y++){
  18. int xx = i+x , yy = j + y ;
  19. if(xx>=1&&xx<=n&&yy>=1&&yy<=m) {
  20. st[xx][yy] = true ;
  21. }
  22. }
  23. }
  24. }
  25. bool pd(int i , int j){
  26. for(int x=-1;x<=1;x++){
  27. for(int y=-1;y<=1;y++){
  28. int xx = i+x , yy = j + y ;
  29. if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[xx][yy]=='c') {
  30. return false ;
  31. }
  32. }
  33. }
  34. return true ;
  35. }
  36. int main(){
  37. cin >> n >> m ;
  38. for(int i=1;i<=n;i++){
  39. for(int j=1;j<=m;j++){
  40. cin >> a[i][j] ;
  41. if(a[i][j]=='m') f(i,j) ;
  42. }
  43. }
  44. int cnt = 0 ;
  45. for(int i=1;i<=n;i++){
  46. for(int j=1;j<=m;j++){
  47. if(a[i][j]=='w' && !st[i][j]){
  48. for(int x=-1;x<=1;x++){
  49. for(int y=-1;y<=1;y++){
  50. int xx = i+x , yy = j + y ;
  51. if(a[xx][yy]=='.'&&pd(xx,yy)){
  52. cout << xx << " " << yy << '\n' ;
  53. cnt ++ ;
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. if(cnt==0){
  61. cout << "Too cold!" ;
  62. }
  63. return 0 ;
  64. }

RC-u4 章鱼图的判断

1 . 先用并查集求每一个连通块的环的个数 : 

        ps : 对于每一对点 , 如果祖先相同 , 那么必定存在环,环数++ , 不同,进行union操作

2 . 对于输出yes的答案 , 记录环中一对相邻点 , 用bfs求两点距离 ,则为环的大小 ;

  1. #include<bits/stdc++.h>
  2. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) ;
  3. using namespace std ;
  4. const int N = 1e5+10 ;
  5. #define endl '\n'
  6. vector<int> e[N] ;
  7. int n , m ;
  8. int p[N]; //存储每个点的祖宗节点
  9. int sz[N] ;// 维护联通图中环的数量
  10. int find(int x) {return x == p[x] ? x : p[x] = find(p[x]);}
  11. int a , b ;
  12. int d[N] ;// 存距离
  13. // 所在连通图只有一个环的时候才满足题意
  14. // 假设只有一个章鱼环 , a , b为环中相邻的两个点
  15. inline void solve(){
  16. cin >> n >> m ;
  17. for(int i=1;i<=n;i++) e[i].clear() , p[i] = i , d[i] = 0 , sz[i] = 0 ;
  18. for(int i=1;i<=m;i++){
  19. int u , v ; cin >> u >> v ;
  20. e[u].push_back(v) ; e[v].push_back(u) ;
  21. int fu = find(u) , fv = find(v) ;
  22. if(fu==fv) sz[fu]++ , a = u , b = v ; // 必定存在环
  23. else p[fu]=fv ,sz[fv]+=sz[fu] ;// union两个点
  24. }
  25. int ans = 0 ; // 求章鱼环的数量
  26. for(int i=1;i<=n;i++) if(find(i)==i&&sz[i]==1) ans ++ ;
  27. if(ans != 1) {
  28. cout << "No" << " " << ans << endl ;
  29. return ;
  30. }
  31. // 求a->b中这个环中点的数量-->题目所求
  32. // 章鱼子图 点数>=3 , 直接bfs来求 a->b的距离(不是直接两点两边的距离,这个用continue跳过)
  33. queue<int> q ;
  34. q.push(a) ;
  35. while(!q.empty()){
  36. int u = q.front() ; q.pop() ;
  37. for(int v : e[u]){
  38. if(u==a&&v==b) continue ;
  39. if(!d[v]) d[v] = d[u] + 1,q.push(v) ;
  40. }
  41. }
  42. cout << "Yes" << " " << d[b]+1 << endl ;
  43. }
  44. int main(){
  45. IOS
  46. int _ ;
  47. cin >> _ ;
  48. while(_--) solve() ;
  49. return 0 ;
  50. }

RC-u5 工作安排

先看赛时代码   : 

先按截止时间排序 ,然后dp ;

dp[i][j] 代表前i个任务,截止时间为j的最大值 ;

转移方程 : dp[i][j] = max(dp[i][j],dp[i-1][k]+a[i].p) ;

  1. #include<bits/stdc++.h>
  2. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) ;
  3. using namespace std ;
  4. #define endl '\n'
  5. int n ;
  6. struct Node{
  7. int t,d,p ;
  8. };
  9. bool cmp(Node& x , Node& y){
  10. return x.d < y.d ;
  11. }
  12. inline void solve(){
  13. cin >> n ;
  14. int ma = 0 ;
  15. int tx = 1 ;
  16. vector<Node> a(n+1) ;
  17. for(int i=1;i<=n;i++){
  18. int x , y , z ; cin >> x >> y >> z ;
  19. if(x>y) continue ;
  20. a[tx].t = x ;
  21. a[tx].d = y ;
  22. a[tx++].p = z ;
  23. ma = max(ma , y) ;
  24. }
  25. tx -= 1 ;
  26. // cout << tx << endl ;
  27. sort(a.begin()+1,a.begin()+1+tx,cmp) ;
  28. vector<vector<int>> dp(tx+1,vector<int>(ma+1,0)) ;
  29. for(int i=1;i<=tx;i++){
  30. for(int j=1;j<=a[i].d;j++){
  31. dp[i][j] = max(dp[i-1][j] , dp[i][j-1]) ;
  32. int k = j - a[i].t ;
  33. if(k>=0){
  34. dp[i][j] = max(dp[i][j],dp[i-1][k]+a[i].p) ;
  35. }
  36. }
  37. }
  38. cout << dp[tx][ma] << endl ;
  39. }
  40. int main(){
  41. IOS
  42. int _ ;
  43. cin >> _ ;
  44. while(_--){
  45. solve() ;
  46. }
  47. return 0 ;
  48. }

ps : 这个代码爆空间了 , 丢了5分 , 正解应该是一个01背包 ; 

欢迎交流!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/944910
推荐阅读
相关标签
  

闽ICP备14008679号