6886 - Golf Bot
2015-08-30 13:31
281 查看
6886 Golf Bot
Do you like golf? I hate it. I hate golf so much that I
decided to build the ultimate golf robot, a robot that
will never miss a shot. I simply place it over the ball,
choose the right direction and distance and, flawlessly,
it will strike the ball across the air and into the hole.
Golf will never be played again.
Unfortunately, it doesn’t work as planned. So, here
I am, standing in the green and preparing my first
strike when I realize that the distance-selector knob
built-in doesn’t have all the the distance options! Not ev-
erything is lost, as I have 2 shot.
Given my current robot, how many holes will I beable to complete in 2 strokes or less? The ball must be
always on the right line between the tee and the hole.
It isn’t allowed to overstep it and come back.
Input
The input file contains several test cases, each of them
as described below.
The first line has one integer: N, the number of
different distances the Golf Bot can shoot. Each of
the following N lines has one integer, ki
, the distance
marked in position i of the knob.
Next line has one integer: M, the number of holes in this course. Each of the following M lines has
one integer, dj, the distance from Golf Bot to hole j.
Constraints:
1 <= N; M <= 200 000
1 <= ki; dj <= 200 000
Output
For each test case, you should output a single integer, the number of holes Golf Bot will be able to
complete. Golf Bot cannot shoot over a hole on purpose and then shoot backwards.
Sample Output Explanation
Golf Bot can shoot 3 different distances (1, 3 and 5) and there are 6 holes in this course at distances
2, 4, 5, 7, 8 and 9. Golf Bot will be able to put the ball in 4 of these:
• The 1st hole, at distance 2, can be reached by striking two times a distance of 1.
• The 2nd hole, at distance 4, can be reached by striking with strength 3 and then strength 1 (or
vice-versa).
• The 3rd hole can be reached with just one stroke of strength 5.
• The 5th hole can be reached with two strikes of strengths 3 and 5.
Holes 4 and 6 can never be reached.
Sample Input
3
1
3
5
6
2
4
5
7
8
9
Sample Output
4
直接暴力会超时,简单hash一下即可。
#include <iostream> #include <cstdio> #include <cstring> #include <map> #include <cmath> #include <vector> #include <algorithm> using namespace std; typedef long long ll; #define MAX 1005 #define PI acos(-1.0) int a[200005],b[200005]; int f[200005]; int main(){ int n,m; while(~scanf("%d", &n)){ memset(f,0,sizeof(f)); for(int i = 0;i < n;i++){ scanf("%d", a+i); f[a[i]] = 1; } scanf("%d", &m); sort(a,a+n); int cnt = 0; for(int i = 0;i < m;i++){ int num; scanf("%d", &num); if(f[num]){ cnt ++; continue; } for(int i = 0;i < n;i++){ if(2*a[i] > num){ break; } if(f[num-a[i]]){ cnt ++; break; } } } printf("%d\n", cnt); } return 0; }
相关文章推荐
- 【英语】Bingo口语笔记(61) - mind系列
- 【英语】Bingo口语笔记(60) - 口语中的浊化发音
- 360 搜索到底拐走了 Google 多少流量?
- Google学术指数2015版
- django 1.8 官方文档翻译: 14-4-1 重定向应用
- django 1.8 官方文档翻译: 3-4-5 内建基于类的视图的API
- django 1.8 官方文档翻译: 2-1-4 Model 类参考
- django 1.8 官方文档翻译: 13-1-1 Django 中的用户认证
- django开发博客(1) 入门
- ubuntu安装Google Chrome
- leetcode: (9) Palindrome Number
- Goods:登录页面之登录功能的实现
- django分页技术paginator
- EM算法(Expectation Maximization Algorithm)
- django Q查询
- 安装go语言
- django开发博客(1) 入门
- django开发博客(1) 入门
- Django最佳实践(中文版)
- HDU1533 Going Home(最小费用最大流 spfa模版)