题目链接
1400的 div 3 E题,难度不大。

题目大意:给出一个字符串,“ * ” 表示羊," . "是空地。让你用最小的操作数把所有的羊排成一排。每次操作可以让一只羊左移或者右移(当然前提是移动到的地方是空地)。

分析:题目题意好理解,思路也不难,稍微一推就出来了。
可以设排好序的第一个位置是x,第二个位置就是x + 1… 并且每只羊不能越过另一只羊。所以有了初始位置和末尾位置就可以列出来式子:(以sample1为例:**.*…) ∣1−x∣+∣2−(x+1)∣+∣4−(x+2)∣\left | 1 - x \right | + \left | 2 - (x + 1) \right | + \left | 4 - (x + 2) \right |∣1−x∣+∣2−(x+1)∣+∣4−(x+2)∣,即∣1−x∣+∣1−x∣+∣2−x∣\left | 1 - x \right | + \left | 1 - x \right | + \left | 2 - x \right |∣1−x∣+∣1−x∣+∣2−x∣。很明显,当x为1,1,2的中位数时,该式子最小。(注意开long long)

code:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
const int N = 1e5 + 100;
const int INF = 0x3f3f3f3f;void solve() {int n;vector<int> ve;string s;cin >> n >> s;for (int i = 0; i < n; i++) {if (s[i] == '*') {ve.push_back(i + 1 - (ve.size()));}}if (ve.size() == 0) {cout << 0 << endl;return;}sort(ve.begin(), ve.end());int len = ve.size();int ans = ve[len / 2];int res = 0;for (int i = 0; i < len; i++) {res += abs(ve[i] - ans);}cout << res << endl;
}signed main(int argc, char const *argv[]) {int t;cin >> t;while (t--) solve();return 0;
}

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. Day 5 E. Arranging The Sheep

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

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

    题目:codeforces 1520E. Arranging The Sheep(1400) 链接:https://codeforces.ml/problemset/problem/1520/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. Gartner发布2020年数据与分析领域的十大技术趋势
  2. 提高PHP编程效率的53个要点(转)
  3. 7天学会spring cloud教程
  4. 深度梳理这10个国家的AI发展战略
  5. php unable to save,php – Laravel save()未定义
  6. mysql 禁止插入重复数据_防止MySQL重复插入数据的三种方法
  7. c语言马秀丽,C语言程序计 清华大学出版社 马秀丽 刘志妩科后习题9答.doc
  8. 利用FPGA加速实现高性能计算
  9. 第二次尝试修复Hbase2出现Region不一致,使用 HBCK2 - 2021.11.15
  10. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---36
  11. 【电脑运用及修理】连接不到网站服务器解决方案【win10】
  12. Java汽车销售系统
  13. 在windows 2003系统上安装诺基亚pc套件的方法
  14. 系统性总结了 Pandas 所有知识点
  15. python词云图绘制
  16. arcgis分析道路节点中心性degree,closeness,betweenness
  17. 生产系统执行下线操作--泪奔
  18. 【金猿产品展】拍乐云——新一代实时音视频云服务,构建云上的每一次美好互动...
  19. java锁源码分析-1获取锁的过程
  20. 小米8se怎么解屏幕锁_小米8 SE怎样解锁?人脸指纹双解锁安全又快捷!

热门文章

  1. pg_repack 处理表和索引的膨胀
  2. cs架构交互_架构,功能和交互
  3. 微信红包 php算法,基于PHP微信红包的算法探讨
  4. STM32项目总结--物联网毕设使用
  5. gcat广告电商云南百收涉及传销新模式:社交电商消费返利
  6. AD16弹出错误对话框
  7. 18 个 Python 高效编程小技巧
  8. WARNING: too many parse errors
  9. ODT(old driver tree)详解(带例题)
  10. 华为笔记本软件商店_华为应用市场