zoukankan      html  css  js  c++  java
  • 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了。
    程序说明:(略)
    参考链接:(略)
    题记:(略)

    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
    ", 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
    ", ans);
        }
    
        return 0;
    }
    
  • 相关阅读:
    判断某个元素是否显示/隐藏
    文件file
    文件上传原理--FileReader
    angular搭建
    判断滚动条滚到底部
    bugDone
    webstorm界面主题
    自定义滚动条
    用电脑免费给手机发短信(转)
    c++ 面试常见问题
  • 原文地址:https://www.cnblogs.com/tigerisland/p/10229534.html
Copyright © 2011-2022 走看看