zoukankan      html  css  js  c++  java
  • hdu 5233 Gunner II 离散化

    题目链接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5233

    题意:

    有n只鸟,还有n棵树。第i只鸟站在第i棵树的顶端。这些树从左到右排成一条直线。每一棵树都有它的高度。Jack站在最左边那棵树的左边。当Jack在高度为H的地方向右发射一棵子弹时,站在高度为H的树上且离Jack最近的鸟儿就会落下来。 Jack会射击多次,他想知道每次射击哪只鸟儿会落下来。

    题解:

    离散化

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long ll;
     4 #define MS(a) memset(a,0,sizeof(a))
     5 #define MP make_pair
     6 #define PB push_back
     7 const int INF = 0x3f3f3f3f;
     8 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
     9 inline ll read(){
    10     ll x=0,f=1;char ch=getchar();
    11     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    12     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    13     return x*f;
    14 }
    15 //////////////////////////////////////////////////////////////////////////
    16 const int maxn = 2e5+10;
    17 
    18 struct node{
    19     int x,id;
    20 }h[maxn];
    21 
    22 vector<int> k,dp[maxn];
    23 map<int,int> H;
    24 int q[maxn],cnt[maxn];
    25 
    26 void init(){
    27     MS(h); MS(q); MS(cnt);
    28     H.clear(); k.clear();
    29     for(int i=0; i<maxn; i++)
    30         dp[i].clear();
    31 }
    32 
    33 int main(){
    34     int n,m;
    35     while(scanf("%d%d",&n,&m)!=EOF){
    36         init();
    37         for(int i=0; i<n; i++){
    38             h[i].x = read();
    39             h[i].id = i+1;
    40             k.push_back(h[i].x);
    41         }
    42         for(int i=0; i<m; i++){
    43             q[i] = read();
    44             k.push_back(q[i]);
    45         }
    46         sort(k.begin(),k.end());
    47         k.erase(unique(k.begin(),k.end()),k.end());
    48         for(int i=0; i<(int)k.size(); i++){
    49             H[k[i]] = i;
    50         }
    51         for(int i=0; i<n; i++)
    52             dp[H[h[i].x]].push_back(h[i].id);
    53         for(int i=0; i<m; i++){
    54             if(cnt[H[q[i]]] >= (int)dp[H[q[i]]].size())
    55                 cout << -1 << endl;
    56             else{
    57                 cout << dp[H[q[i]]][cnt[H[q[i]]]] << endl;
    58                 cnt[H[q[i]]]++;
    59             }
    60         }
    61 
    62     }
    63 
    64     return 0;
    65 }
  • 相关阅读:
    剑指Offer解题报告(Java版)——约瑟夫环 45
    剑指Offer解题报告(Java版)——扑克牌顺子 44
    剑指Offer解题报告(Java版)——n个骰子的点数 43
    基础知识:HashTable和HashMap的区别
    基础知识:Arraylist、vector、Linkedlist的比较
    第7章 SportsStorePeta 一个真实的应用程序
    第24章 捆绑包
    第23章 模型验证
    第22章 模型绑定
    第21章 URL和Ajax辅助器方法
  • 原文地址:https://www.cnblogs.com/yxg123123/p/6827630.html
Copyright © 2011-2022 走看看