zoukankan      html  css  js  c++  java
  • Gunner II--hdu5233(map&vector/二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5233

    题意:有n颗树,第 i 棵树的高度为 h[i],树上有鸟,现在这个人要打m次枪,每次打的高度是 q[i], 求每次

    打枪能打下鸟的编号,否则输出-1 ;

    STL中的map和vector:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <map>
    
    using namespace std;
    
    #define met(a, b) memset(a, b, sizeof(a))
    #define INF 0x3f3f3f3f
    #define N 100010
    
    int dir[8][2] = { {1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {1,-1}, {-1,1}, {-1,-1} };
    
    
    int n, m, h[N], q[N];
    
    map<int, vector<int> > mp;
    
    int main()
    {
        while(scanf("%d %d", &n, &m) != EOF)
        {
            mp.clear();
            
            for(int i=1; i<=n; i++)
    
                scanf("%d", &h[i]);
        
            for(int i=n; i>=1; i--)
                
                    mp[h[i]].push_back(i);
                
            for(int i=1; i<=m; i++)
            {
                scanf("%d", &q[i]);
                
                if(mp[q[i]].size()>=1)
                {
                    printf("%d
    ", mp[q[i]].back());
                    
                    mp[q[i]].pop_back();
                }
                else
                
                    printf("-1
    ");
            }
        }
        return 0;
    }
    View Code

    二分:

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <map>
    
    using namespace std;
    
    #define met(a, b) memset(a, b, sizeof(a))
    #define INF 0x3f3f3f3f
    #define N 100010
    
    int dir[8][2] = { {1,0}, {-1,0}, {0,1}, {0,-1}, {1,1}, {1,-1}, {-1,1}, {-1,-1} };
    
    struct node
    {
        int num, Id;
    }h[N];
    
    int n, m, q[N], vis[N];
    
    int cmp(node a, node b)
    {
        if(a.num!=b.num)
            return a.num < b.num;
        return a.Id < b.Id;
    }
    
    int BeSearch(int L, int R, int num)
    {
        int ans = -1;
        
        while(L <= R)
        {
            int Mid = (L+R)/2;
            
            if(num == h[Mid].num)
            {
                if(!vis[Mid])
                {
                    ans = Mid;
                    
                    R = Mid - 1;///先输出最左边的;
                }
                else
                    L = Mid + 1;
            }
            else if(h[Mid].num > num)
                
                R = Mid - 1;
            else
                
                L = Mid + 1;
        }
        if(ans != -1)
        {
            vis[ans] = 1;
            return h[ans].Id;
        }
        return -1;
    }
    
    int main()
    {
        while(scanf("%d %d", &n, &m) != EOF)
        {
            met(vis, 0);
            met(h, 0);
            met(q, 0);
    
            for(int i=0; i<n; i++)
            {
                scanf("%d", &h[i].num);
               
                h[i].Id = i+1;
            }
    
            sort(h, h+n, cmp);
    
            for(int i=1; i<=m; i++)
            {
                scanf("%d", &q[i]);
    
                int pos = BeSearch(0, n, q[i]);
    
                printf("%d
    ", pos);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    AOSP 设置编译输出目录
    android stadio 编译报错:download fastutil-7.2.0.jar
    Ubuntu adb 报错:no permissions (user in plugdev group; are your udev rules wrong?);
    Ubuntu 18启动失败 Started Hold until boot procss finishes up
    算法---------两数之和
    Windows 显示环境变量
    Android ObjectOutputStream Serializable引发的血案
    (AOSP)repo checkout指定版本
    如果看懂git -help
    Android stado 运行项目,apk does not exist on disk.
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5248515.html
Copyright © 2011-2022 走看看