题目:codeforces 1520E. Arranging The Sheep(1400)
链接:https://codeforces.ml/problemset/problem/1520/E

题目大意:给你一个只有.和*的字符串,用最少的步数把*都凑在一起;

题解:有个东西叫中位数定理(假设有一个数轴,数轴上的点到中间点的距离之和最小……大概)

那这题只需要先找到 在所有 * 处于中间位置的 * ,然后遍历计算;

PS:因为*移动会受到其他*的限制,所以距离还要减去目标和终点之间*的个数;

代码:

#include<bits/stdc++.h>using namespace std;
typedef long long ll;int a[1000005];int main()
{int t;cin >> t;while (t--) {int n;cin >> n;string s;cin >> s;int cnt = 0;for (int i = 0; i < n; i++) {if (s[i] == '*')a[cnt++] = i;}int mid = cnt / 2;ll sum = 0;for (int i = 0; i < cnt; i++){sum += abs(a[mid] - a[i]) - abs(mid - i);}cout << sum << endl;}}

codeforces 1520E. Arranging The Sheep(1400)相关推荐

  1. codeforces 1520E Arranging The Sheep

    链接: https://codeforces.com/problemset/problem/1520/E 题意: 给一个字符串,'*'代表羊,'.'代表空,让所有的羊站成一排,意为每个羊中间都没有空, ...

  2. Arranging The Sheep

    You are playing the game "Arranging The Sheep". The goal of this game is to make the sheep ...

  3. CF1520E Arranging The Sheep

    题目描述 You are playing the game "Arranging The Sheep". The goal of this game is to make the ...

  4. Arranging The Sheep(移动思维)

    You are playing the game "Arranging The Sheep". The goal of this game is to make the sheep ...

  5. Day 5 E. Arranging The Sheep

    Problem: You are playing the game "Arranging The Sheep". The goal of this game is to make ...

  6. Arranging The Sheep 中位数定理

    CodeForces - 1520E 题意:移动多少步,可以使所有的*相邻 中位数定理 :数列上所有数,到中位数的距离和最小 #include<stdio.h> #include<m ...

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

    翻译: 你正在玩"安排羊"游戏.这个游戏的目标是让羊排好队.游戏中的关卡是由长度为

  8. E. Arranging The Sheep

    题目链接 1400的 div 3 E题,难度不大. 题目大意:给出一个字符串," * " 表示羊," . "是空地.让你用最小的操作数把所有的羊排成一排.每次操 ...

  9. excel更改日期格式无效_Excel日期不会更改格式

    excel更改日期格式无效 Have you ever imported data into Excel, from your credit card statement, or somewhere ...

最新文章

  1. AutoML未来可期,工程师的明天何去何从?
  2. 如何实现分享链接到微信朋友圈时显示自定义LOGO以及名称介绍
  3. Linux无法打开sda硬盘,linux – 无法挂载’/ dev / sda’
  4. 团队博客 一 需求分析
  5. idea创建web项目的几种方式(没内容,自用)
  6. 使用SpringTest测试,默认情况事务是不会提交的
  7. java jdk动态代理 cglib动态代理demo
  8. unity 动画 音频播放
  9. 如何查看 MySQL 数据库的引擎
  10. Android SDK怎么救砖,Android手机刷机变砖后无法开机的修复方法
  11. 飞思卡尔单片机DZ60---TPM1\TPM2溢出中断
  12. win10更新后开不了机_win10开机关机正常,重启特别慢问题排查
  13. 云原生|kubernetes|minikube的部署安装完全手册(修订版)
  14. TCP应用层主要协议
  15. 「微服务系列」统一网关Gateway
  16. 行为型模式之观察者模式
  17. 使用scp复制传输windows文件到linux
  18. 使用okhttp3报错 The type okio.ByteString cannot be resolved. It is indirectly referenced
  19. 管家婆怎么删除服务器备份文件夹,​管家婆删除的数据还能找回来吗
  20. A Transformer-based System for English Named Entity Recognition 基于Transformer的英语命名实体识别系统

热门文章

  1. canvas webGL SVG 比较
  2. Testin云测试平台
  3. 大数据行业前景_大数据未来展望
  4. 信道以及信道容量小结
  5. 购物网站的商品推荐算法有哪些?
  6. python语言 表白程序_python程序员实现表白代码的案例
  7. jdk8新特性之出现This inspection finds all usages of methods that have @since tag in their documentation.
  8. outlook技巧-常用email账号设置
  9. 拉新、复购、供应链,双11中小商家“三难”如何破?
  10. 暂时性死区的详解(TDZ)