USACO 2018 February Contest, Bronze Hoofball

2024-01-03 13:18

本文主要是介绍USACO 2018 February Contest, Bronze Hoofball,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 Hoofball

时间限制: 1 Sec   内存限制: 128 MB
提交: 166   解决: 27
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

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 ii standing xi units away from the barn (1≤xi≤1000). Each cow is standing at a distinct location.
At the begiNing 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.

输入

The first line of input contains N. The second line contains N space-separated integers, where the ith integer is xi.

输出

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.

样例输入

5

7 1 3 11 4

样例输出

2

提示

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.

来源

USACO 2018 February Contest, Bronze 



【题意】

    扑朔迷离的英文题; 不, 是扑朔迷离的英语水平, 看不懂题意;;;;;;

    是这样的, 某个点传球, 传两边,  传给距离最小的, 其次,当距离相等时 传给左边;

    问最少需要几个球 让所有的人都曾经摸过球。


【思路】 

    想着类似于 波浪线这种, 有上升有下降, 取顶点,  对于 平台  取奇数的 ,  但是实现起来特别麻烦。

    用 环标记,   把 某个数 存左边还是右边  建立 关系,  构成 几个环, 然后DFS 搜一下环,

    最后 没有标记的点,就属于平台部分了,  然后/2  加上环的个数


【代码实现】

/*
* Date: 4/3/2018
* Tile: UPC 补题
* Category: DFS
* AU: siz
* WA: 1
* Attation: 标记后, 进行DFS 搜索,,最后扫描, 平台的情况
*/
#include <iostream>
#include <bits/stdc++.h>
typedef long long ll;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+5;using namespace std;int head[MAXN],vis[MAXN];
int in[MAXN];
int cot,n,m;
struct node{int v,next;
}edge[MAXN];
void init()
{cot=0;memset(head,-1,sizeof(head));memset(vis,0,sizeof(vis));memset(in,0,sizeof(in));
}
void add(int u,int v)
{edge[++cot].next=head[u];edge[cot].v=v;head[u]=cot;
}
void dfs(int s)
{vis[s]=1;for(int i=head[s];i!=-1;i=edge[i].next){int v=edge[i].v;if(!vis[v])dfs(v);}
}int a[MAXN];
int main()
{int ans1=0,ans2=0;cin>>n;init();for(int i=1;i<=n;i++)cin>>a[i];sort(a+1,a+n+1);a[0]=a[n+1]=-INF;if(n==1){cout<<1<<endl;return 0;}for(int i=1;i<=n;i++){if(abs(a[i]-a[i-1]) <= abs(a[i]-a[i+1]))add(i,i-1),in[i-1]++;elseadd(i,i+1),in[i+1]++;}for(int i=1;i<=n;i++){if(!in[i]){dfs(i);ans1++;}}for(int i=1;i<=n;i++)if(!vis[i])ans2++;cout<<ans1+(ans2/2)<<endl;return 0;
}

这篇关于USACO 2018 February Contest, Bronze Hoofball的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/565819

相关文章

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

usaco 1.3 Calf Flac(暴搜)

思路是暴搜。 需要注意的地方是输入的方法,以及输出时的换行。 代码: /*ID: who jayLANG: C++TASK: calfflac*/#include<stdio.h>#include<string.h>#include<math.h>int main(){freopen("calfflac.in","r",stdin);freopen("calfflac.ou

usaco 1.3 Barn Repair(贪心)

思路:用上M块木板时有 M-1 个间隙。目标是让总间隙最大。将相邻两个有牛的牛棚之间间隔的牛棚数排序,选取最大的M-1个作为间隙,其余地方用木板盖住。 做法: 1.若,板(M) 的数目大于或等于 牛棚中有牛的数目(C),则 目测 给每个牛牛发一个板就为最小的需求~ 2.否则,先对 牛牛们的门牌号排序,然后 用一个数组 blank[ ] 记录两门牌号之间的距离,然后 用数组 an

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

usaco 1.2 Palindromic Squares(进制转化)

考察进制转化 注意一些细节就可以了 直接上代码: /*ID: who jayLANG: C++TASK: palsquare*/#include<stdio.h>int x[20],xlen,y[20],ylen,B;void change(int n){int m;m=n;xlen=0;while(m){x[++xlen]=m%B;m/=B;}m=n*n;ylen=0;whi

usaco 1.2 Name That Number(数字字母转化)

巧妙的利用code[b[0]-'A'] 将字符ABC...Z转换为数字 需要注意的是重新开一个数组 c [ ] 存储字符串 应人为的在末尾附上 ‘ \ 0 ’ 详见代码: /*ID: who jayLANG: C++TASK: namenum*/#include<stdio.h>#include<string.h>int main(){FILE *fin = fopen (

usaco 1.2 Milking Cows(类hash表)

第一种思路被卡了时间 到第二种思路的时候就觉得第一种思路太坑爹了 代码又长又臭还超时!! 第一种思路:我不知道为什么最后一组数据会被卡 超时超了0.2s左右 大概想法是 快排加一个遍历 先将开始时间按升序排好 然后开始遍历比较 1 若 下一个开始beg[i] 小于 tem_end 则说明本组数据与上组数据是在连续的一个区间 取max( ed[i],tem_end ) 2 反之 这个

usaco 1.2 Transformations(模拟)

我的做法就是一个一个情况枚举出来 注意计算公式: ( 变换后的矩阵记为C) 顺时针旋转90°:C[i] [j]=A[n-j-1] [i] (旋转180°和270° 可以多转几个九十度来推) 对称:C[i] [n-j-1]=A[i] [j] 代码有点长 。。。 /*ID: who jayLANG: C++TASK: transform*/#include<

usaco 1.1 Broken Necklace(DP)

直接上代码 接触的第一道dp ps.大概的思路就是 先从左往右用一个数组在每个点记下蓝或黑的个数 再从右到左算一遍 最后取出最大的即可 核心语句在于: 如果 str[i] = 'r'  ,   rl[i]=rl[i-1]+1, bl[i]=0 如果 str[i] = 'b' ,  bl[i]=bl[i-1]+1, rl[i]=0 如果 str[i] = 'w',  bl[i]=b

BUUCTF靶场[web][极客大挑战 2019]Http、[HCTF 2018]admin

目录   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 [web][HCTF 2018]admin 考点:弱密码字典爆破 四种方法:   [web][极客大挑战 2019]Http 考点:Referer协议、UA协议、X-Forwarded-For协议 访问环境 老规矩,我们先查看源代码