• 22/6/27


    1,acwing 920 最优乘车;2,acwing 903.昂贵的聘礼;3,cf Restricted RPS


    1,最优乘车; 

     思路:

    同条线路的所有站点之间的距离为1,所以给每条线路的站点之间互连距离为1的路径,求1好点到s的最短距离,再减去1即是最少换乘次数;

    这道题有个注意的地方是它每行给出的线路车站数是不确定的,所以采用行读取,用到了stringstream;简单写下用法:

    行的输入用getline,(如果在你要读入的数据之前还有输入,那就要吃个回车,用了快读不能用getchar,用cin.get();),

    将行的数据放到stringstream里有两种写法:

    一种是stringstream ssin(line);

    一种是 stringstream ssin;ssin<<line;

    将stringstream的数据取出来的方法:ssin>>();

    注意区分和cin>> ; cout<< 的方向的区别;

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef pair<int,int> PII;
    26. typedef pair<int,PII> PIII;
    27. typedef long long LL;
    28. typedef double dob;
    29. const int N = 510;
    30. int m,n;
    31. int g[N][N];
    32. int dist[N];
    33. int stop[N];
    34. void bfs()
    35. {
    36. queue<int>q;
    37. memset(dist,0x3f,dist);
    38. q.push(1);
    39. dist[1]=0;
    40. while(q.size())
    41. {
    42. int t=q.front();q.pop();
    43. rep2(i,1,n)
    44. {
    45. if(g[t][i]&&dist[i]>dist[t]+1)
    46. {
    47. dist[i]=dist[t]+1;
    48. q.push(i);
    49. }
    50. }
    51. }
    52. }
    53. signed main()
    54. {
    55. quick_cin();
    56. cin>>m>>n;
    57. string line;
    58. cin.get();
    59. while(m--)
    60. {
    61. getline(cin,line);
    62. stringstream ssin(line);
    63. int cnt=0,p;
    64. while(ssin>>p)stop[cnt++]=p;
    65. rep1(j,0,cnt)
    66. rep1(k,j+1,cnt)
    67. g[stop[j]][stop[k]]=1;
    68. }
    69. bfs();
    70. if(dist[n]==0x3f3f3f3f)NO;
    71. else cout<<max(dist[n]-1,0);
    72. return 0;
    73. }

     2,昂贵的聘礼;

     题目有些长,需要耐心分析题目;

    首先看不考虑等级制度的建图方式,在得到每个物品的路径上,都有一条原价直接买到的,不妨设个虚拟源点S,都由S连该路径;

    拿样例来看:

    我们可以从该图中得到最短的路径,几所需的金币总数,花50买4物品,在拿200 换3物品,在加5000即可得到1;

    那有了等级制度后,我们只可以在等级区间内进行交易,一共100等级,可以遍历整个等级区间,来得到最优解;

     code

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef pair<int,int> PII;
    26. typedef pair<int,PII> PIII;
    27. typedef long long LL;
    28. typedef double dob;
    29. const int N=110,INF=0x3f3f3f3f;
    30. int n,m;
    31. int w[N][N],level[N];
    32. int dist[N];
    33. int st[N];
    34. int dijkstra(int down,int up)
    35. {
    36. memset(dist,0x3f,dist);
    37. memset(st,0,st);
    38. dist[0]=0;
    39. rep2(i,1,n+1)
    40. {
    41. int t=-1;
    42. rep2(j,0,n)
    43. if(!st[j]&&(t==-1||dist[t]>dist[j]))
    44. t=j;
    45. st[t]=1;
    46. rep2(j,1,n)
    47. if(level[j]>=down&&level[j]<=up)
    48. dist[j]=min(dist[j],dist[t]+w[t][j]);
    49. }
    50. return dist[1];
    51. }
    52. signed main()
    53. {
    54. quick_cin();
    55. cin>>m>>n;
    56. memset(w,0x3f,w);
    57. rep2(i,1,n)w[i][i]=0;
    58. rep2(i,1,n)
    59. {
    60. int price,cnt;
    61. cin>>price>>level[i]>>cnt;
    62. w[0][i]=min(price,w[0][i]);
    63. while (cnt--)
    64. {
    65. int id,cost;
    66. cin>>id>>cost;
    67. w[id][i]=min(w[id][i],cost);
    68. }
    69. }
    70. int res=INF;
    71. rep2(i,level[1]-m,level[1])
    72. res=min(res,dijkstra(i,i+m));
    73. cout<<res;
    74. return 0;
    75. }

    3,cf Restricted RPS

    题意:a和b在玩石头剪刀布游戏,有n次回合,a赢的条件是至少n/2上取整次赢;

    现给出a的石头次数,剪刀次数,布次数;和b的出手序列RPPS....(R是石头,P是布,S是剪刀)

    如果a能赢,输出yes和使a能赢的出手序列(任意,但是长度和b相同);否则no;

    思路:统计b的R,P,S的次数,a赢得对局的次数是min(P,R),min(S,P),min(R,S)的和;符合条件就赢,对应位置给它安排赢的出手,最后遍历没有安排到的位置给它安排出手;

    1. #pragma GCC optimize(2)
    2. #include<bits/stdc++.h>
    3. #define rep1(i,a,n) for( int i=a;i<n;++i)
    4. #define rep2(i,a,n) for( int i=a;i<=n;++i)
    5. #define per1(i,n,a) for( int i=n;i>a;i--)
    6. #define per2(i,n,a) for( int i=n;i>=a;i--)
    7. #define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
    8. #define memset(a,i,b) memset((a),(i),sizeof (b))
    9. #define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
    10. #define pro_q priority_queue
    11. #define pb push_back
    12. #define pf push_front
    13. #define endl "\n"
    14. #define lowbit(m) ((-m)&(m))
    15. #define YES cout<<"YES\n"
    16. #define NO cout<<"NO\n"
    17. #define Yes cout<<"Yes\n"
    18. #define No cout<<"No\n"
    19. #define yes cout<<"yes\n"
    20. #define no cout<<"no\n"
    21. #define yi first
    22. #define er second
    23. using namespace std;
    24. typedef pair<long long,long long>PLL;
    25. typedef pair<int,int> PII;
    26. typedef pair<int,PII> PIII;
    27. typedef long long LL;
    28. typedef double dob;
    29. unordered_map<char,int>acs;
    30. unordered_map<char,int>bcs;
    31. char trans(char c)
    32. {
    33. if(c=='R')return 'P';
    34. else if(c=='P')return 'S';
    35. else return 'R';
    36. }
    37. char ans[1010];
    38. void solve()
    39. {
    40. acs.clear();
    41. bcs.clear();
    42. memset(ans,0,ans);
    43. int n;
    44. cin>>n;
    45. int mid=n+1>>1;
    46. int a,b,c;
    47. cin>>a>>b>>c;
    48. acs['R']=a;
    49. acs['P']=b;
    50. acs['S']=c;
    51. string s;
    52. cin>>s;
    53. int siz=s.size();
    54. rep1(i,0,siz)bcs[s[i]]++;
    55. int wins=0;
    56. wins+=min(a,bcs['S']);
    57. wins+=min(b,bcs['R']);
    58. wins+=min(c,bcs['P']);
    59. if(wins>=mid)
    60. {
    61. YES;
    62. rep1(i,0,siz)
    63. {
    64. if(acs[trans(s[i])])
    65. {
    66. ans[i]=trans(s[i]);
    67. acs[trans(s[i])]--;
    68. }
    69. }
    70. rep1(i,0,siz)
    71. {
    72. if(!ans[i])
    73. {
    74. if(acs['R'])ans[i]='R',acs['R']--;
    75. else if(acs['P'])ans[i]='P',acs['P']--;
    76. else ans[i]='S',acs['S']--;
    77. }
    78. }
    79. rep1(i,0,siz)cout<<ans[i];
    80. cout<<endl;
    81. }
    82. else NO;
    83. }
    84. signed main()
    85. {
    86. quick_cin();
    87. int T;
    88. cin>>T;
    89. while(T--)solve();
    90. return 0;
    91. }

  • 相关阅读:
    python离线环境下安装第三方模块的方法
    一个查询优化
    非零基础自学Java (老师:韩顺平) 第15章 泛型 15.3 泛型介绍 && 15.4 泛型的语法
    DIGIX比赛1
    基于VUE + Echarts 实现可视化数据大屏文化大数据
    Python基于生成树机制实验的内容
    PHY6230低成本遥控灯控芯片国产蓝牙BLE5.2 2.4G SoC
    8.Ribbon负载均衡服务调用
    解决——》CommunicationsException:Communications link failure
    scrum|敏捷开发之任务看板
  • 原文地址:https://blog.csdn.net/aidaqiudeaichao/article/details/125492093