B. Hoofball(dfs)
B. Hoofball
time limit per test
5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
In preparation for the upcoming hoofball tournament, Farmer John is drilling his NN cows (conveniently numbered 1…N1…N, where 1≤N≤1001≤N≤100) in passing the ball. The cows are all standing along a very long line on one side of the barn, with cow ii standing xixi units away from the barn (1≤xi≤10001≤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 ii 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 NN. The second line contains NN space-separated integers, where the iith integer is xixi.
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
Copy
5
7 1 3 11 4
output
Copy
2
Note
In the above example, Farmer John should pass a ball to the cow at x=1x=1 and pass a ball to the cow at x=11x=11. The cow at x=1x=1 will pass her ball to the cow at x=3x=3, after which this ball will oscillate between the cow at x=3x=3 and the cow at x=4x=4. The cow at x=11x=11will pass her ball to the cow at x=7x=7, who will pass the ball to the cow at x=4x=4, after which this ball will also cycle between the cow at x=3x=3 and the cow at x=4x=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个人站在一条直线上,给出距原点的距离。每个人向距离他最近的人传球,若距离相等则向左,每人至少摸一次球,问最少需要给几个球。
题目分析:
dfs 。分情况,球在第一个人,第n个人,2-n-1人之间。
dfs +标记同一颜色。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int n,a[N],vis[N],ans[N];int cnt=1;
void dfs(int pos)
{
if(vis[pos]==cnt)return;
vis[pos]=cnt;
if(pos==1)dfs(2);
else if(pos==n)dfs(n-1);
else if(pos!=1&&pos!=n){
if(a[pos]-a[pos-1]<=a[pos+1]-a[pos])dfs(pos-1);
else dfs(pos+1);
}
}
int main()
{
cin>>n;for(int i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
if(!vis[i])
dfs(i);
cnt++;
}
cnt=0;
for(int i=1;i<=n;i++)ans[vis[i]]=1;
for(int i=1;i<=n;i++)if(ans[i])cnt++;
cout<<cnt;
}
- P3227 [HNOI2013]切糕
- python常见模块之random模块
- P2756 飞行员配对方案问题
- P1151 子数整数
- python常见模块之time模块
- U10783 名字被和谐了
- BZOJ 1174: [Balkan2007]Toponyms
- 1355: [Baltic2009]Radio Transmission
- Equation Group(方程式组织)
- Python中下划线---完全解读
- python常见模块之collections模块
- MYSQL之库操作
- 实战-如何获取安卓iOS上的微信聊天记录、通过Metasploit控制安卓
- lightswitch binding custom control
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- CentOS下使用LibreOffice实现文档格式的转换方式
- 详解在Linux中怎么使用cron计划任务
- Linux系统删除文件夹和文件的命令
- 详解Linux防火墙iptables禁IP与解封IP常用命令
- 在Ubuntu 16.04 Server上安装Zabbix的方法
- Centos7.3安装部署最新版Zabbix3.4的方法(图文)
- Linux系统下移植busybox中mkfs.vfat命令
- Linux服务器配置ip白名单防止远程登录以及端口暴露的问题
- Ubuntu上释放空间的5种简单方法
- Linux下Redis允许远程连接的实现方法
- Linux Socket 编程简介和实现
- Linux (Ubuntu 18.04) 下安装vim编辑器的方法
- 把windows下的字体安装到Linux系统下的方法介绍
- Ubuntu 7.10修改软件源的方法
- Linux 僵尸进程产生原因及解决方法