1. 程式人生 > >UVA12879 UVALive6886 Golf Bot【水題】

UVA12879 UVALive6886 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 distance options! Not everything is lost, as I have 2 shots.
    Given my current robot, how many holes will I be able 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

問題連結UVA12879 UVALive6886 Golf Bot
問題簡述
    高爾夫機器人一杆能打的距離有n種,給出m個洞的位置,問最多打兩杆(不能往回打)的話能打進幾種洞。
問題分析
    一種解法是暴力法,先標記一杆和兩杆能打的距離位置,然後對於m個詢問做個統計就得到答案了,但是其計算複雜度為O(n^2),有可能出現TLE。
    位運算似乎可以解決問題,程式都AC了。
    還有一種方法是使用快速福利也傅利葉變化,計算出2個卷及卷積中數相加的所有可能性。
程式說明:(略)
參考連結:(略)
題記:(略)

AC的C++語言程式如下:

/* UVA12879 UVALive6886 Golf Bot */

#include <iostream>
#include <bitset>
#include <stdio.h>

using namespace std;

const int N = 200000;
bitset<N + 1> h1, h2;
int k[N + 1] = {0}, d;

int main()
{
    int n, m;
    while(scanf("%d", &n) != EOF) {
        h1.reset();
        h2.reset();
        for(int i = 1; i <= n; i++) {
            scanf("%d", &k[i]);
            h1[k[i]] = 1;
        }
        scanf("%d", &m);
        for(int i = 1; i <= m; i++) {
            scanf("%d", &d);
            h2[d] = 1;
        }
        for(int i = 0; i <= n; i++)
            h2 ^= (h2 & (h1 << k[i]));

        printf("%d\n", m - (int)h2.count());
    }

    return 0;
}

TLE的C++語言程式(UVA中AC,UVALive中TLE)如下:

/* UVA12879 UVALive6886 Golf Bot */

#include <bits/stdc++.h>

using namespace std;

const int N = 200000;
int h[ N + N], k[N], d;

int main()
{
    int n, m;
    while(scanf("%d", &n) != EOF) {
        memset(h, 0, sizeof(h));
        for(int i = 0; i < n; i++) {
            scanf("%d", &k[i]);
            h[k[i]] = 1;
        }
        for(int i = 0; i < n; i++)
            for(int j = i; j < n; j++)
                h[k[i] + k[j]] = 1;

        int ans = 0;
        scanf("%d", &m);
        for(int i = 0; i < m; i++) {
            scanf("%d", &d);
            if(h[d])
                ans++;
        }

        printf("%d\n", ans);
    }

    return 0;
}