• Codeforces Round #719 (Div. 3) E. Arranging The Sheep


    翻译:

    你正在玩“安排羊”游戏。这个游戏的目标是让羊排好队。游戏中的关卡是由长度为𝑛的字符串描述的,由角色的’组成。'(空格)和'*'(绵羊)。在一个动作中,你可以移动任何羊向左或向右移动一个方格,如果相应的方格存在并且为空的话。一旦羊群排好队,游戏就结束了,也就是说,羊群之间不应该有空格子。

    例如,如果𝑛=6,并且关卡由字符串“**.*..”描述,那么就可能出现以下游戏场景:

    4位的羊向右移动,水平状态:“**..*.”;
    2位的羊向右移动,水平状态:“*.*.*.”;
    位置1的羊向右移动,水平状态为:“.**.*.”;
    3位的羊向右移动,水平状态:“。*.**.”;
    位置2的羊向右移动,水平状态:“..***.”;
    羊排好队,比赛结束。
    对于一个特定的关卡,你需要确定完成该关卡所需的最小移动数。

    输入
    第一行包含一个整数𝑡(1≤𝑡≤104)。然后是𝑡测试用例。

    每个测试用例的第一行包含一个整数𝑛(1≤𝑛≤106)。

    每个测试用例的第二行包含一个长度为𝑛的字符串,由字符'组成。’(空白)和‘*’(绵羊)——关卡的描述。

    可以保证在所有测试用例中𝑛的总和不超过106。

    输出
    对于每个测试用例,输出完成关卡所需的最小移动数。

    例子
    inputCopy
    5
    6
    * * . * . .
    5
    *****
    3.
    *。
    3.
    ...
    10
    *.*...*.**
    outputCopy
    1
    0
    0
    0
    9

    思路:

    要把羊移动到一起,所以我们可以设一个方向,集体往哪边移动,最左和最右,中间都会有多余不必要的操作,而且两端的移动次数叠加都是最大的,所以我们选中心点,然后来让两边移动。

    代码:

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. using namespace::std;
    15. typedef long long ll;
    16. int n,t;
    17. inline __int128 read(){
    18. __int128 x = 0, f = 1;
    19. char ch = getchar();
    20. while(ch < '0' || ch > '9'){
    21. if(ch == '-')
    22. f = -1;
    23. ch = getchar();
    24. }
    25. while(ch >= '0' && ch <= '9'){
    26. x = x * 10 + ch - '0';
    27. ch = getchar();
    28. }
    29. return x * f;
    30. }
    31. inline void print(__int128 x){
    32. if(x < 0){
    33. putchar('-');
    34. x = -x;
    35. }
    36. if(x > 9)
    37. print(x / 10);
    38. putchar(x % 10 + '0');
    39. }
    40. string s;
    41. void solv(){
    42. cin>>n>>s;
    43. vector<int>q;
    44. int bj=0;
    45. for (int i =0; isize(); i++) {
    46. if (i!=0&&s[i]!=s[i-1]) {
    47. bj=1;
    48. }
    49. if (s[i]=='*') {
    50. q.push_back(i);
    51. }
    52. }
    53. if (!bj) {
    54. printf("0\n");return;
    55. }
    56. int zz=(q.size())/2;
    57. zz=q[zz];
    58. ll ans=0;
    59. int ff=0,se=0;
    60. int l=zz-1,r=zz+1;
    61. while (l>=0) {
    62. if (s[l]=='*') {
    63. ans+=zz-l-1-ff;
    64. ff++;
    65. }
    66. l--;
    67. // printf("%lld\n",ans);
    68. }
    69. while (rsize()) {
    70. if (s[r]=='*') {
    71. ans+=r-zz-1-se;
    72. se++;
    73. }
    74. r++;
    75. // printf("%lld\n",ans);
    76. }
    77. printf("%lld\n",ans);
    78. }
    79. int main(){
    80. ios::sync_with_stdio(false);
    81. cin.tie(); cout.tie();
    82. cin>>t;
    83. while (t--) {
    84. solv();
    85. }
    86. return 0;
    87. }
    88.  

  • 相关阅读:
    SpringBoot的启动流程
    开源:Taurus.Idempotent 分布式幂等性锁框架,支持 .Net 和 .Net Core 双系列版本
    JAVA并发编程--4.3理解CountDownLatch
    Python文件及目录操作(高级文件操作篇)
    java毕业设计人才申报系统源码+lw文档+mybatis+系统+mysql数据库+调试
    接口测试主要测试哪方面?需要哪些技能?要怎么学习?
    Qt Model/View之代理
    DirectExchange交换机简单入门demo
    【Spring】Spring中的DI(依赖注入)Dependence Import
    Go基础语法:指针和make和new
  • 原文地址:https://blog.csdn.net/weixin_63555280/article/details/128115281