E. Arranging The Sheep
题目链接
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相关推荐
- Arranging The Sheep
You are playing the game "Arranging The Sheep". The goal of this game is to make the sheep ...
- CF1520E Arranging The Sheep
题目描述 You are playing the game "Arranging The Sheep". The goal of this game is to make the ...
- Arranging The Sheep(移动思维)
You are playing the game "Arranging The Sheep". The goal of this game is to make the sheep ...
- Day 5 E. Arranging The Sheep
Problem: You are playing the game "Arranging The Sheep". The goal of this game is to make ...
- codeforces 1520E. Arranging The Sheep(1400)
题目:codeforces 1520E. Arranging The Sheep(1400) 链接:https://codeforces.ml/problemset/problem/1520/E 题目 ...
- codeforces 1520E Arranging The Sheep
链接: https://codeforces.com/problemset/problem/1520/E 题意: 给一个字符串,'*'代表羊,'.'代表空,让所有的羊站成一排,意为每个羊中间都没有空, ...
- Arranging The Sheep 中位数定理
CodeForces - 1520E 题意:移动多少步,可以使所有的*相邻 中位数定理 :数列上所有数,到中位数的距离和最小 #include<stdio.h> #include<m ...
- Codeforces Round #719 (Div. 3) E. Arranging The Sheep
翻译: 你正在玩"安排羊"游戏.这个游戏的目标是让羊排好队.游戏中的关卡是由长度为
- Codeforces Round #719 (Div. 3)/ Codeforces Round #720 (Div. 2)
A. Do Not Be Distracted! 题意: 一件事情一但开始,只能做完才能做别的事,当出现一件事不连续出现时,教师会怀疑 题目: Polycarp has 26 tasks. Each ...
最新文章
- Gartner发布2020年数据与分析领域的十大技术趋势
- 提高PHP编程效率的53个要点(转)
- 7天学会spring cloud教程
- 深度梳理这10个国家的AI发展战略
- php unable to save,php – Laravel save()未定义
- mysql 禁止插入重复数据_防止MySQL重复插入数据的三种方法
- c语言马秀丽,C语言程序计 清华大学出版社 马秀丽 刘志妩科后习题9答.doc
- 利用FPGA加速实现高性能计算
- 第二次尝试修复Hbase2出现Region不一致,使用 HBCK2 - 2021.11.15
- 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---36
- 【电脑运用及修理】连接不到网站服务器解决方案【win10】
- Java汽车销售系统
- 在windows 2003系统上安装诺基亚pc套件的方法
- 系统性总结了 Pandas 所有知识点
- python词云图绘制
- arcgis分析道路节点中心性degree,closeness,betweenness
- 生产系统执行下线操作--泪奔
- 【金猿产品展】拍乐云——新一代实时音视频云服务,构建云上的每一次美好互动...
- java锁源码分析-1获取锁的过程
- 小米8se怎么解屏幕锁_小米8 SE怎样解锁?人脸指纹双解锁安全又快捷!