链接:
https://codeforces.com/problemset/problem/1520/E

题意:

给一个字符串,'*'代表羊,'.'代表空,让所有的羊站成一排,意为每个羊中间都没有空,每次只能让一只羊动一步,问最少多少步可以让所有的羊站成一排。

本题只要记录所有羊的位置,然后所有羊都向中间的羊靠拢即可,如果有偶数只羊,那选两只羊的哪一只都可以。

代码如下:

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<string.h>
#include<random>
using namespace std;
typedef long long ll;
int position[1000003];
int main() {int T;cin >> T;while (T--) {int n;cin >> n;string s;cin >> s;int cntshp = 0;for (int i = 0; i < n; i++) {if (s[i] == '*') {position[cntshp++] = i;}}int center = (cntshp >> 1);ll ans = 0;for (int i = 0; i < cntshp; i++) {if (i == center) {continue;}else {ans += (abs(position[center] - position[i]) - abs(center - i));}}cout << ans;cout << endl;}
}

codeforces 1520E Arranging The Sheep相关推荐

  1. codeforces 1520E. Arranging The Sheep(1400)

    题目:codeforces 1520E. Arranging The Sheep(1400) 链接:https://codeforces.ml/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. Linux卸载unity,你如何删除Unity?
  2. SQUAD的rnet复现踩坑记
  3. 聊天秒回的人都是生命之光 诉言网
  4. 在Tableau中去除选择高亮效果
  5. 并发新特性—Executor 框架与线程池
  6. TensorFlow:实战Google深度学习框架(一)计算、数据、运行模型
  7. 面包房算法 java_java处理买面包事件
  8. 关于SIGPIPE导致的程序退出
  9. 今日头条电脑版官方版_雷霆拯救电脑版-雷霆拯救pc版下载v4.0.9 官方版
  10. 学习python内一般函数知识
  11. Python学习基础系列----了解python
  12. HTML+CSS静态网页制作:电影网站设计——漫威电影(2页)
  13. Outlook/Microsfot邮件配置:QQ邮箱/腾讯企业邮箱
  14. 基于ThinkPHP6+Layui后台开发框架
  15. 学生类输入输出姓名学号
  16. 选择代码覆盖工具的 10 个标准
  17. 超级记忆力训练法简介
  18. Kafka服务器命令总结
  19. android查询cpu信息
  20. execve 执行遇到的问题-已解决

热门文章

  1. 华为鸿蒙太空人壁纸,华为太空人动态壁纸下载-华为太空人动态壁纸图片高清版-丫丫安卓网...
  2. A+B 问题【OJ 示例题目】
  3. 分析报告 | 在线教育APP的市场到底有多大?
  4. git gc error: failed to run repack message
  5. I2C协议---I2C时序图解析
  6. 订单表的字段类型 mysql_Mysql数据库下订单表如何设计?
  7. python networkx 导入CSV文件画关系网络图
  8. IIS6 日志删除脚本(t00ls)
  9. CSS 3D动态立体图形
  10. Classloader整理