HDU5233 Gunner II 离散化的各种方法

题目链接:

HDU5233

题意:

n棵树依次排好,每棵树都有一个高度,树的顶端有一只鸟。

猎人会打M枪,每一枪都能从高度为X的树上打下一只鸟,问每一枪打下的鸟是从  编号多少的树 上掉下来的

题解思路:

因为树的高度能达到(10^9)  而树的数量最多10^5  所以离散化   将所有高度为X的树离散化为 高度为第X高的树

有多种方法。

1  stl去重+set版:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#define MAXN  100050
using namespace std;
int h[MAXN];
int q[MAXN];
int s[MAXN*2];
set<int>ans[MAXN*2];
int main()
{
    int n,m,cnt;
    while(~scanf("%d%d",&n,&m))
    {
        cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&h[i]);
            s[cnt++]=h[i];
        }
        for(int j=0;j<m;j++)
        {
            scanf("%d",&q[j]);
            s[cnt++]=q[j];
        }
        sort(s,s+cnt);
        cnt=unique(s,s+cnt)-s;
        for(int i=0;i<cnt;i++)
            ans[i].clear();
        for(int i=0;i<n;i++)
        {
            int d=lower_bound(s,s+cnt,h[i])-s;
            ans[d].insert(i+1);
        }
        for(int j=0;j<m;j++)
        {
            int d=lower_bound(s,s+cnt,q[j])-s;
            if(ans[d].empty())
                printf("-1\n");
            else
            {
                printf("%d\n",*ans[d].begin());
                ans[d].erase(ans[d].begin());
            }
        }
    }
    return 0;
}

土方法离散:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define maxn  100010
struct H
{
    int v,id;
}num[2*maxn];

int w[2*maxn];
int vis[maxn<<1];

bool cmp(H a,H b)
{
    return a.v<b.v;
}
vector <int> vc[maxn<<1];

int main()
{
    int n,m;
    int cnt;
    while(~scanf("%d%d",&n,&m)){
        cnt=0;
        for(int i=0;i<n;i++)
        {
            int xx;
            scanf("%d",&xx);
            num[i].v=xx;num[i].id=i;
        }
        for(int i=n;i<n+m;i++)
        {
            int xx;scanf("%d",&xx);
            num[i].v=xx;num[i].id=i;
        }
        sort(num,num+n+m,cmp);
        int tot=0;w[num[0].id]=0;
        for(int i=1;i<n+m;i++)
        {
            if(num[i].v==num[i-1].v){
                w[num[i].id]=tot;
            }
            else w[num[i].id]=++tot;
        }
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            vis[w[i]]++;vc[w[i]].push_back(i+1);
        }
        for(int i=n;i<n+m;i++)
        {
            if(vis[w[i]]>0)
            {
                int le=vc[w[i]].size()-vis[w[i]];
                printf("%d\n",vc[w[i]][le]);
                vis[w[i]]--;
            }
            else printf("-1\n");
        }
    }
    return 0;
}

set+map:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#define read freopen("q.in","r",stdin)
#define LL long long
#define maxn 100005
using namespace std;
set<int>  mp[maxn];
map<int,int> c;
//http://www.2cto.com/kf/201108/100912.html
//http://blog.csdn.net/tyzhaoqi2004/article/details/6882660
int main()
{
    //read;
   int n,m;
   while(scanf("%d%d",&n,&m)!=EOF)
   {
       int i,j,q,x,t=0;
       for(i=1;i<=n;i++)
        mp[i].clear();
        c.clear();
       for(i=0;i<n;i++)
       {
          scanf("%d",&x);
          if(!c[x])c[x]=++t;
          mp[c[x]].insert(i+1);

         // cout<<c[x]<<" ";
       }
       //for(i=1;i<=t;i++)cout<<c[i]<<" ";
    //   cout<<endl;
       for(i=0;i<m;i++)
       {
          scanf("%d",&q);
          if(mp[c[q]].size()==0)
          {
             printf("-1\n");
          }
          else
          {
            //  cout<<mp[q].size()<<" %% "<<endl;
             printf("%d\n",*(mp[c[q]].begin()));
             mp[c[q]].erase(mp[c[q]].begin());
            // cout<<mp[q].size()<<" %% "<<endl;
          }
       }

    }
}
时间: 2024-08-26 19:39:38

HDU5233 Gunner II 离散化的各种方法的相关文章

hdu 5233 Gunner II 离散化

Gunner II Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5233 Description 很久很久以前,有一个叫Jack的枪手.他非常喜欢打猎.一天,他去了一个小树林.那儿有n只鸟,还有n棵树.第i只鸟站在第i棵树的顶端.这些树从左到右排成一条直线.每一棵树都有它的高度.Jack站在最左边那棵树的左边.当Jack在高度为H的地方向右发射一棵子弹时,站在高度为

hdu5233 Gunner II

Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are n birds and n trees. The i-th bird stands on the top of the i-th tree. The trees stand in straight line

HDU5233——离散化——Gunner II

http://acm.hdu.edu.cn/showproblem.php?pid=5233 /* 用map和set进行离散化,离散化就是对于大数据进行重新编号 */ /************************************************ * Author :Powatr * Created Time :2015-8-12 18:27:42 * File Name :HDU5233 Gunner II.cpp *****************************

hdu 5233 Gunner II (bc #42 B)

Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1433    Accepted Submission(s): 540 Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ver

Gunner II(二分,map,数字转化)

Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1740    Accepted Submission(s): 635 Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ve

STL HDOJ 5233 Gunner II

题目传送门 1 /* 2 题意:查询x的id,每次前排的树倒下 3 使用lower_bound ()查找高度,f[i]记录第一棵高度为x树的位置,查询后+1(因为有序) 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e5 + 10; 11 const int INF = 0x3f3f3f3

hdu4509 湫湫系列故事——减肥记II (离散化思想)

Problem Description http://acm.hdu.edu.cn/showproblem.php?pid=4509 虽然制定了减肥食谱,但是湫湫显然克制不住吃货的本能,根本没有按照食谱行动! 于是,结果显而易见- 但是没有什么能难倒高智商美女湫湫的,她决定另寻对策--吃没关系,咱吃进去再运动运动消耗掉不就好了? 湫湫在内心咆哮:"我真是天才啊~\(≧▽≦)/~" 可是,大家要知道,过年回家多忙啊--帮忙家里做大扫除,看电影,看小说,高中同学聚餐,初中同学聚餐,小学同学

leetCode 47.Permutations II (排列组合II) 解题思路和方法

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

leetCode 40.Combination Sum II(组合总和II) 解题思路和方法

Combination Sum II Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination. Note: All numbers (including t