• 2022ICPC济南站


    K

    Stack Sort

    题意:给你一个长度为n的排列,设有m个栈,你需要将这n个数按出现顺序入栈,每次入栈操作从m个栈中选择一个栈从栈顶入栈。当所有元素入栈完成后,需要不断选择栈,将栈中元素弹空。需满足出栈顺序为1 2 3 ... n,问完成上述任务所需最少栈的个数为多少。

    思路:遍历数组,设当前元素为x,我们就看是否存在栈顶为x+1的栈,若存在则入该栈;否则新开一个栈将x入栈。

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. typedef long long ll;
    5. const int N=1e6+10;
    6. const int inf=0x3f3f3f3f;
    7. using namespace std;
    8. /*
    9. 1 4 2 5 3
    10. 1 34 2 5
    11. 找x+1存在否 存在
    12. 不存在 ++
    13. */
    14. int n;
    15. bool st[N];
    16. void solve()
    17. {
    18. cin>>n;
    19. for(int i=1;i<=n+1;i++) st[i]=0;
    20. int ans=0;
    21. for(int i=1;i<=n;i++)
    22. {
    23. int x;
    24. cin>>x;
    25. if(st[x+1])
    26. {
    27. st[x+1]=0;
    28. st[x]=1;
    29. }
    30. else
    31. {
    32. st[x]=1;
    33. ans++;
    34. }
    35. }
    36. cout<'\n';
    37. }
    38. signed main()
    39. {
    40. //freopen("input.txt","r",stdin);
    41. //freopen("output.txt","w",stdout);
    42. ios;
    43. int _t=1;
    44. cin>>_t;
    45. while(_t--) solve();
    46. system("pause");
    47. return 0;
    48. }

    M

    Best Carry Player

    题意:给定n个元素的数组a[],起始sum=0,不断执行sum+=a[i],问加法过程中的总进位次数为多少?

    思路:模拟

    1. #include
    2. #define ios ios::sync_with_stdio(0), cin.tie(0)
    3. #define PII pair
    4. typedef long long ll;
    5. const int N = 1e5 + 10;
    6. const int inf = 0x3f3f3f3f;
    7. using namespace std;
    8. int n;
    9. string a[N];
    10. void solve()
    11. {
    12. cin >> n;
    13. for (int i = 1; i <= n; i++)
    14. {
    15. string x;
    16. cin >> x;
    17. reverse(x.begin(), x.end());
    18. a[i] = x;
    19. }
    20. string ret = "000000000000000";
    21. int ans = 0;
    22. int del = 0;
    23. for (int i = 1; i <= n; i++)
    24. {
    25. for (int j = 0; j < 15; j++)
    26. {
    27. int c1 = int(ret[j] - '0');
    28. int c2 = 0;
    29. if(jsize()) c2=int(a[i][j] - '0');
    30. int t = c1 + c2 + del;
    31. ret[j] = char(t % 10 + '0');
    32. if (t >= 10)
    33. {
    34. ans++;
    35. del = 1;
    36. }
    37. else
    38. del = 0;
    39. }
    40. }
    41. cout << ans << '\n';
    42. }
    43. signed main()
    44. {
    45. // freopen("input.txt","r",stdin);
    46. // freopen("output.txt","w",stdout);
    47. ios;
    48. int _t = 1;
    49. cin >> _t;
    50. while (_t--)
    51. solve();
    52. system("pause");
    53. return 0;
    54. }

    E

    Identical Parity

    题意:一个序列的值定义为所有数字的和,给定n,k问是否存在长度为n的排列满足所有长度为k的子段的值奇偶性都相同。

    思路:当k=1时,若n=1 Yes;k=1, n不为1 No

    当k为偶数时,我们可以奇偶交替的放,Yes

    当k为奇数时,我们可以分成k组,每组的下标满足a, a+k, a+2k...a属于[1,k],同一组我们放相同奇偶的数。我们发现这样的序列就满足条件。

    设n/k=b余c,我们将k/2个组,这些组的位置放偶数;其余k/2+1个组,这些组的位置上放奇数。那么还剩下c个数没放,判断剩下的数是否满足奇偶个数。

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. #define int long long
    5. typedef long long ll;
    6. const int N=1e6+10;
    7. const int inf=0x3f3f3f3f;
    8. using namespace std;
    9. int n,k;
    10. void solve()
    11. {
    12. cin>>n>>k;
    13. if(k==1)
    14. {
    15. if(n==1) cout<<"Yes\n";
    16. else cout<<"No\n";
    17. }
    18. else if(k%2==0) cout<<"Yes\n";
    19. else
    20. {
    21. int tot_o=n/2,tot_j=(n+1)/2;//总的奇偶个数
    22. int k_o=k/2,k_j=k/2+1;//k组中有多少组奇数偶数
    23. int b=n/k,c=n%k;
    24. int r_o=tot_o-b*k_o,r_j=tot_j-b*k_j;//放完k组,还剩多少奇偶数没放
    25. if(r_o>=0&&r_j>=0&&r_o+r_j==c)
    26. {
    27. if(r_o<=k_o&&r_j<=k_j) cout<<"Yes\n";
    28. else cout<<"No\n";
    29. }
    30. else cout<<"No\n";
    31. }
    32. }
    33. signed main()
    34. {
    35. //freopen("input.txt","r",stdin);
    36. //freopen("output.txt","w",stdout);
    37. ios;
    38. int _t=1;
    39. cin>>_t;
    40. while(_t--) solve();
    41. system("pause");
    42. return 0;
    43. }

    A

    Tower

    题意:有n座不同高度的塔,第i座塔的高度为a[i]

    你可以将其中的m座塔移除,然后进行下面的操作:

    1.选择一座塔,将其高度 a[i]增加 1

    2.选择一座塔,将其高度 a[i]减少 1

    3.选择一座塔,将其高度 a[i]/=2,向下取整

    不允许操作后塔的高度为0。问将剩余n-m座塔的高度搞成相同所需最少的操作次数。

    思路:最后变成的相同高度一定是某个塔的高度不断/2的结果。所以我们可以通过枚举最后高度,来计算塔到这个高度的最小操作次数。

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. #define int long long
    5. typedef long long ll;
    6. const int N=1e6+10;
    7. const int inf=0x3f3f3f3f;
    8. using namespace std;
    9. int n,m;
    10. int a[N];
    11. int get(int now,int x)
    12. {
    13. int ret=0;
    14. if(now
    15. else if(now==x) ret=0;
    16. else
    17. {
    18. while(now>x)
    19. {
    20. if(now>x&&now/2<=x)
    21. {
    22. ret+=min(now-x,1+x-now/2);
    23. break;
    24. }
    25. now/=2;
    26. ret++;
    27. }
    28. }
    29. return ret;
    30. }
    31. void solve()
    32. {
    33. cin>>n>>m;
    34. for(int i=1;i<=n;i++)
    35. cin>>a[i];
    36. set<int>s;
    37. for(int i=1;i<=n;i++)
    38. {
    39. int x=a[i];
    40. s.insert(x);
    41. while(x>=2)
    42. {
    43. x/=2;
    44. s.insert(x);
    45. }
    46. }
    47. int ans=1e18;
    48. for(auto x:s)
    49. {
    50. vector<int>v;
    51. for(int i=1;i<=n;i++)
    52. {
    53. int t=get(a[i],x);
    54. v.push_back(t);
    55. }
    56. sort(v.begin(),v.end());
    57. int tem=0;
    58. for(int i=0;i
    59. tem+=v[i];
    60. ans=min(ans,tem);
    61. }
    62. cout<'\n';
    63. }
    64. signed main()
    65. {
    66. //freopen("input.txt","r",stdin);
    67. //freopen("output.txt","w",stdout);
    68. ios;
    69. int _t=1;
    70. cin>>_t;
    71. while(_t--) solve();
    72. system("pause");
    73. return 0;
    74. }

    D

    Frozen Scoreboard

    题意:给你封榜前的情况和最终通过的题数和罚时,问最终的榜单

    思路:将封榜前的过题数和罚时减去,再对封榜后的题进行二进制枚举,判断合法

    1. #include
    2. #define ios ios::sync_with_stdio(0),cin.tie(0)
    3. #define PII pair
    4. #define int long long
    5. typedef long long ll;
    6. const int N=1e6+10;
    7. const int inf=0x3f3f3f3f;
    8. using namespace std;
    9. int n,m;
    10. int a[N],b[N];
    11. struct node{
    12. char op;
    13. int x,y;
    14. int num;
    15. }P[N];
    16. void solve()
    17. {
    18. cin>>n>>m;
    19. for(int i=1;i<=n;i++)
    20. {
    21. cin>>a[i]>>b[i];
    22. int nowt=0,nowcnt=0;
    23. vectoruk;
    24. for(int i=0;i
    25. {
    26. char op;
    27. cin>>op;
    28. if(op=='+')
    29. {
    30. int u,v;char _;
    31. cin>>u>>_>>v;
    32. nowcnt++;
    33. nowt+=(u-1)*20+v;
    34. P[i]={'+',u,v};
    35. }
    36. else if(op=='-')
    37. {
    38. int u;
    39. cin>>u;
    40. P[i]={'-',u};
    41. }
    42. else if(op=='?')
    43. {
    44. int u,v;
    45. cin>>u>>v;
    46. P[i]={'?',u,v,i};
    47. uk.push_back({'?',u,v,i});
    48. }
    49. else P[i]={'.'};
    50. }
    51. if(nowcnt==a[i]&&nowt==b[i])
    52. {
    53. cout<<"Yes\n";
    54. for(int j=0;j
    55. {
    56. if(P[j].op=='+') printf("+ %d/%d\n",P[j].x,P[j].y);
    57. else if(P[j].op=='-') printf("- %d\n",P[j].x);
    58. else if(P[j].op=='?') printf("- %d\n",P[j].y);
    59. else printf(".\n");
    60. }
    61. }
    62. else if(nowcnt
    63. {
    64. int cnt=a[i]-nowcnt;
    65. int t=b[i]-nowt;
    66. bool f=0;
    67. for(int j=0;j<(1<size());j++)
    68. {
    69. int mi=0,ma=0;
    70. for(int k=0;ksize();k++)
    71. {
    72. if((j>>k)&1)
    73. {
    74. mi+=240+(uk[k].y-uk[k].x)*20;
    75. ma+=299+(uk[k].y-1)*20;
    76. }
    77. }
    78. if(t>=mi&&t<=ma)
    79. {
    80. map<int,node>ans;
    81. int remt=t-mi;
    82. int finish=0;
    83. for(int k=0;ksize();k++)
    84. {
    85. if((j>>k)&1)
    86. {
    87. int num=uk[k].num;
    88. int cishu=min(remt/20,uk[k].x-1);//封榜后wa了多少次
    89. ans[num].x=(uk[k].y-uk[k].x+cishu+1);
    90. remt-=cishu*20;
    91. int r1=min(remt,59ll);
    92. remt-=r1;
    93. ans[num].y=240+r1;
    94. finish++;
    95. }
    96. }
    97. if(remt==0&&finish==cnt)
    98. {
    99. f=1;
    100. cout<<"Yes\n";
    101. for(int k=0;k
    102. {
    103. if(P[k].op=='+') printf("+ %d/%d\n",P[k].x,P[k].y);
    104. else if(P[k].op=='-') printf("- %d\n",P[k].x);
    105. else if(P[k].op=='?'&&ans[k].x) printf("+ %d/%d\n",ans[k].x,ans[k].y);
    106. else if(P[k].op=='?') printf("- %d\n",P[k].y);
    107. else printf(".\n");
    108. }
    109. break;
    110. }
    111. }
    112. }
    113. if(!f) cout<<"No\n";
    114. }
    115. else cout<<"No\n";
    116. }
    117. }
    118. signed main()
    119. {
    120. //freopen("input.txt","r",stdin);
    121. //freopen("output.txt","w",stdout);
    122. //ios;
    123. int _t=1;
    124. //cin>>_t;
    125. while(_t--) solve();
    126. system("pause");
    127. return 0;
    128. }

  • 相关阅读:
    蓝桥杯备赛第五篇(动态规划)
    数据结构 六 理解二叉搜索树的实现
    图像语义分割 pytorch复现DeepLab v1图像分割网络以及网络详解(骨干网络基于VGG16)
    云原生Kubernetes:K8S安全机制
    算法刷题小结---几数之和
    供求两端的对接将不再是依靠互联网时代的平台和中心来实现的
    算法基础——时间复杂度和空间复杂度
    机器学习-K近邻(KNN)算法详解
    面试系列之-如何选择外包与自研公司
    实用调试技巧,debug
  • 原文地址:https://blog.csdn.net/qq_62615329/article/details/134319454