Problem:
You are playing the game “Arranging The Sheep”. The goal of this game is to make the sheep line up. The level in the game is described by a string of length n, consisting of the characters ‘.’ (empty space) and ‘*’ (sheep). In one move, you can move any sheep one square to the left or one square to the right, if the corresponding square exists and is empty. The game ends as soon as the sheep are lined up, that is, there should be no empty cells between any sheep.

For example, if n=6 and the level is described by the string " * * . * . . ",then the following game scenario is possible:

the sheep at the 4 position moves to the right, the state of the level: " * * . .* . ";
**the sheep at the 2 position moves to the right, the state of the level: " * . * . * . " ;
the sheep at the 1 position moves to the right, the state of the level: " . * * . * . ";
the sheep at the 3 position moves to the right, the state of the level: " . * . * * . ";
the sheep at the 2 position moves to the right, the state of the level: " . . * * * . ";
the sheep are lined up and the game ends.
For a given level, determine the minimum number of moves you need to make to complete the level.

Input
The first line contains one integer t (1≤t≤10^4). Then t test cases follow.
The first line of each test case contains one integer n (1≤n≤10^6).
The second line of each test case contains a string of length n, consisting of the characters ‘.’ (empty space) and ‘*’ (sheep) — the description of the level.
It is guaranteed that the sum of n over all test cases does not exceed 10^6.

Output
For each test case output the minimum number of moves you need to make to complete the level.

Example
input
5

6
* * . * . .

5
* * * * *

3
. * .

3
. . .

10
* . * . . . * . * *

output
1

0

0

0

9

题目大致意思为:给你一串长为n 仅由 *和.组成的字符串 求至少经过多少次变换 *号能排成一行 每次变换只能左右移动一个 *的位置 这里我用到了中位数定理…
中位数定理
背景:如果有一个数轴 数轴上有若干个点 现要在数轴上找一点 使得它到各个点的距离之和最短。
结论: 中位数就是最优解
中位数有这样的性质: 所有数与中位数的绝对差之和最小
中位数是数列中间的那个数 或者是中间的那两个数之一.
在这道题目中 中位数就是中间 *的位数

代码如下

#include <iostream>
#define ll long long
using namespace std;
string s;
ll f(int x)//计算最少变换次数
{ll cnt = 0;ll mid = 0;for (int i = 0; i < s.length(); i++)//获取中间*的位数 赋值给mid{if (s[i] == '*'){cnt++;}if (cnt == x){mid = i;break;}}ll ans = 0;ll cur = 0;for (int i = mid - 1; i >= 0; i--)//在中间*的前面的*所需要变换次数{if (s[i] == '.'){cur++;}if (s[i] == '*'){ans += cur;}}cur = 0;for (int i = mid + 1; i < s.length(); i++)//在中间*的后面的*所需要变换次数{if (s[i] == '.'){cur++;}if (s[i] == '*'){ans += cur;}}return ans;
}
int main()
{int t;cin >> t;while (t--){int n;cin >> n;cin >> s;ll ans = 0;ll cnt = 0;for (int i = 0; i < n; i++){if (s[i] == '*'){cnt++;}}if (cnt % 2 == 1)//找到中间*{ans = f(cnt / 2 + 1);}else{ans = min(f(cnt / 2), f(cnt / 2 + 1));}cout << ans << endl;}return 0;
}

Day 5 E. Arranging The Sheep相关推荐

  1. Arranging The Sheep

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

  2. CF1520E Arranging The Sheep

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

  3. Arranging The Sheep(移动思维)

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

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

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

  5. E. Arranging The Sheep

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

  6. codeforces 1520E Arranging The Sheep

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

  7. Arranging The Sheep 中位数定理

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

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

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

  9. Codeforces Round #719 (Div. 3)/ Codeforces Round #720 (Div. 2)

    A. Do Not Be Distracted! 题意: 一件事情一但开始,只能做完才能做别的事,当出现一件事不连续出现时,教师会怀疑 题目: Polycarp has 26 tasks. Each ...

最新文章

  1. spring获取上下文的方式
  2. 用机器学习分析美国新财年1万个国防项目,7054亿军费都投给了哪些技术?
  3. 分布式系统设计的概念
  4. Struts2学习8--文件上传(单个文件上传)
  5. 浅谈CruiseControl的部署
  6. 使用selenium爬取某东的手机商品信息
  7. 不起眼的小动作可能葬送安全措施
  8. java jdbc实验,实验八 Java-JDBC编程
  9. ThinkPHP 3.2 DEMO案例系列【phpmailer批量发送邮件】
  10. python获取音频音量大小_如何在python中规范化音频文件的音量:当前可用的任何包?...
  11. 记录一次多表关联查询
  12. java 错误代码2503_错误代码:11300001 数据集配置错误
  13. VMware虚拟机迁移
  14. 微信小程序ios倒计时时间不显示,安卓能正常显示,小程序倒计时兼容问题
  15. ECCV 2022 | 谷歌提出:k-means Mask Transformer
  16. FFmpeg学习(三)-- libavutil 代码组成
  17. html_09网页超链接
  18. 耦合式是什么意思_在电路中,耦合是什么意思?
  19. C++(30)—奇偶数判断
  20. 台式机开机黑屏一直闪小横杠,键盘灯鼠标均正常,主板亮

热门文章

  1. 在Win32API窗体下实现透明背景
  2. a豆的使命:每一位年轻人都值得珍重
  3. 套利[题目][j2]
  4. Visio 去掉页边距和空白页的方法
  5. java 鼠标变成手型_当鼠标放在按钮上时让指针变为手型
  6. 下载keep运动软件_Keep下载_Keep安卓版下载_Keep app下载-太平洋下载中心
  7. DOSBox+MASM,汇编语言环境搭建
  8. Lab3 Report
  9. 什么是下一代防火墙NGFW(Next Generation Firewall)?
  10. 消息队列RabbitMQ入门与PHP实战