题面:

传送门

B. Hoofball

Input file: standard input
Output file: standard output
Time limit: 5 second
Memory limit: 512 megabytes
 
In preparation for the upcoming hoofball tournament, Farmer John is drilling his N cows (conveniently numbered 1 . . . N, where 1 ≤ N ≤ 100) in passing the ball. The cows are all standing along a very long line on one side of the barn, with cow i standing xi units away from the barn (1 ≤ xi ≤ 1000). Each cow is standing at a distinct location.
At the beginning of the drill, Farmer John will pass several balls to different cows. When cow i receives a ball, either from Farmer John or from another cow, she will pass the ball to the cow nearest her (and if multiple cows are the same distance from her, she will pass the ball to the cow farthest to the left among these). So that all cows get at least a little bit of practice passing, Farmer John wants to make sure that every cow will hold a ball at least once. Help him figure out the minimum number of balls he needs to distribute initially to ensure this can happen, assuming he hands the balls to an appropriate initial set of cows.
 
Input
The first line of input contains N. The second line contains N space-separated integers, where the ith integer is xi .
 
Output
Please output the minimum number of balls Farmer John must initially pass to the cows, so that every cow can hold a ball at least once.
 
Example
Input
5
7 1 3 11 4
Output
2
 
Note
In the above example, Farmer John should pass a ball to the cow at x = 1 and pass a ball to the cow at x = 11. The cow at x = 1 will pass her ball to the cow at x = 3, after which this ball will oscillate between the cow at x = 3 and the cow at x = 4. The cow at x = 11 will pass her ball to the cow at x = 7, who will pass the ball to the cow at x = 4, after which this ball will also cycle between the cow at x = 3 and the cow at x = 4. In this way, all cows will be passed a ball at least once (possibly by Farmer John, possibly by another cow).
It can be seen that there is no single cow to whom Farmer John could initially pass a ball so that every cow would eventually be passed a ball.
 

题目描述:

有n头奶牛,农夫会让不同的奶牛拿到一个球,然后凡是拿到球的奶牛会都会把球传给离它最近的奶牛,传来传去。农夫想找到最少的球,使每个牛都能拿到一次球。
 

题目分析:

这道题我们可以通过对题目的数据进行排序,然后从1到N头牛遍历一遍就能得到每头牛的“方向”(往左或往右走),第一头奶牛肯定是向右走的,第N头奶牛肯定是向左走的。我们用一个数组记录每头牛的方向后,怎样花费最少的球?我们可以对这些箭头的情况进行分类讨论:
第一种:这时只需要放一个球在左端或右端就行了
 
第二种:最左端放一个球

第三种:最右端放一个球

第四种:左右各放一个

为什么会分成这四类情况呢?其实我们可以发现,分成这四种情况后,每种情况通过箭头把这些点连起来就变成了一个图,比如第一种情况通过箭头连接点就变成了这样:

变成了一个图。我们对不同的图进行讨论。如果两个相邻的球不是同一个图,那么就分开讨论左图和有图,也就是“断开了”,比如像这样:

从1-N遍历一次数组记录的方向就可以解决了。
 
 
AC代码:
 1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 #include <cmath>
5 #include <set>
6 #include <algorithm>
7 using namespace std;
8 int x[105];
9 char s[105];
10 int mmap[105];
11
12 int main(){
13 int n;
14 cin >> n;
15 for(int i = 0; i < n; i++) cin >> x[i];
16
17 sort(x, x+n);
18 for(int i = 0; i < n; i++){
19 if(i == 0) s[i] = '>'; //开始方向为右
20 else if(i == n-1) s[i] = '<'; //结尾方向为左
21 else{
22 int a, b;
23 a = abs(x[i]-x[i-1]);
24 b = abs(x[i]-x[i+1]);
25 if(a > b){
26 s[i] = '>';
27 }
28 else if(a <= b){
29 s[i] = '<';
30 }
31 }
32 }
33
34 s[n] = '>'; //方便判断用,建议先看下面再上来理解这个
35 int cnt = 0; //球的数量
36 for(int i = 0; i < n; ){
37 int flag = 0; //箭头向右的数量
38 while(s[i] == '>' && i < n) {i++; flag++;}
39 //循环说明s[i] == '<'或者遍历完了
40 if(s[i+1] == '>') {cnt++; i++;} //如果下一个点是另一个图,进行计数,然后到下一个点(这个包含第一种和第二种情况)
41 else{
42 while(s[i] == '<' && i < n) i++;
43 if(flag > 1) cnt += 2; //两个以上向右的箭头(此时这个图向左的箭头超过了两个才会到这一步),就是第四种情况
44 else cnt++; //第三种情况
45 }
46 }
47
48 cout << cnt << endl;
49 return 0;
50 }
 
 

2019 GDUT Rating Contest II : Problem B. Hoofball的更多相关文章

  1. 2019 GDUT Rating Contest II : Problem F. Teleportation

    题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...

  2. 2019 GDUT Rating Contest II : Problem G. Snow Boots

    题面: G. Snow Boots Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  3. 2019 GDUT Rating Contest II : Problem C. Rest Stops

    题面: C. Rest Stops Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  4. 2019 GDUT Rating Contest III : Problem D. Lemonade Line

    题面: D. Lemonade Line Input file: standard input Output file: standard output Time limit: 1 second Memo ...

  5. 2019 GDUT Rating Contest II : A. Taming the Herd

    题面: A. Taming the Herd Input file: standard input Output file: standard output Time limit: 1 second Me ...

  6. 2019 GDUT Rating Contest I : Problem H. Mixing Milk

    题面: H. Mixing Milk Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  7. 2019 GDUT Rating Contest I : Problem A. The Bucket List

    题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...

  8. 2019 GDUT Rating Contest I : Problem G. Back and Forth

    题面: G. Back and Forth Input file: standard input Output file: standard output Time limit: 1 second Mem ...

  9. 2019 GDUT Rating Contest III : Problem E. Family Tree

    题面: E. Family Tree Input file: standard input Output file: standard output Time limit: 1 second Memory ...

随机推荐

  1. leetcode_二叉树篇_python

    主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...

  2. jupyter-notebook kernel died

    问题 在floydhub上跑个github上面的项目, 开了notebook模式运行, 一运行一会儿就kernel died了... 解决 我这儿的问题, 后来发现是出在: 在notebook中, 对 ...

  3. TypeScript constructor public cause duplicate bug

    TypeScript constructor public cause duplicate bug constructor public const log = console.log; // con ...

  4. Vue Component Registration All In One

    Vue Component Registration All In One Vue 注册自定义组件 <template> <div class="back-to-top-c ...

  5. Introduction to JavaScript Source Maps

    下载jquery时候发现:jquery.min.map  这什么鬼呀? https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/core.js http ...

  6. 使用 js 实现十大排序算法: 插入排序

    使用 js 实现十大排序算法: 插入排序 插入排序 // 双重循环 refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  7. Electron in Action

    Electron in Action $ yarn add -D electron@latest # OR $ npm i -D electron@latest https://www.electro ...

  8. React LifeCycle Methods & re-learning 2019

    React LifeCycle Methods & re-learning 2019 v16.9.0 https://reactjs.org/docs/react-component.html ...

  9. 呼叫河马——搭建在NGK公链上的去中心化智能合约DAPP

    基于区块链技术发展的DAPP是一种分布式应用生态系统.目前最受DAPP欢迎的区块链有以太坊.EOS.波场等公链. 但由于当前 EOS资源模型的局限性,使得其使用成本较高.尽管 EOS的DPOS共识机制 ...

  10. spring扩展点整理

    本文转载自spring扩展点整理 背景 Spring的强大和灵活性不用再强调了.而灵活性就是通过一系列的扩展点来实现的,这些扩展点给应用程序提供了参与Spring容器创建的过程,好多定制化的东西都需要 ...