HDU 5199 Gunner (Hash | | sort + dichotomy)

Source: Internet
Author: User
Tags cmath

Test instructions: A tree of n different heights is lined up with 1 birds on each tree.

Then M asked, each time asked can be knocked out H[i] (1<=i<=m) height of all the birds, asked m each height of the inquiry can be knocked out how many birds.

Analysis:

1, the topic gave the hint to "fast read into" Baa ~

2. Sorting + two points

Here can double two points (Lower_bound find the Nether, Upper_bound find upper bound)

You can also merge + two points (merge same number, record count)

g++ submit to use #include<stdio.h> do not use #include<cstdio>

The latter has an efficiency problem, will tle (⊙o⊙) Oh ~

two points when the mark does not have to open the array to see the number used no, because the number is very big AH. This will be super memory!

The direct tag upper and lower bounds determine whether the height of this lookup has been accessed.

1 //#include <cstdio>2#include <stdio.h>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <cstdlib>7#include <cmath>8#include <vector>9#include <queue>Ten#include <map> One#include <Set> A  - using namespacestd; -  thetypedefLong Longll; -  - inth[1000010]; -map<int,int>CNT; + BOOLflag[1000010]; -  + intNextint () A { at     Charc =GetChar (); -     intt =0; -      while(c>='0'&& c<='9') -     { -t = t*Ten+c-'0'; -c =GetChar (); in     } -     returnT; to } +  - intMain () the { *     intn,m; $      while(~SCANF ("%d%d",&n,&m))Panax Notoginseng     { - GetChar (); the cnt.clear (); +          for(intI=0; i<n;i++) A         { theH[i] =nextint (); +cnt[h[i]]++; -         } $Sort (h,h+n); $memset (Flag,0,sizeof(flag)); -         intEn = Unique (h,h+n)-H,ret; -          for(intI=1; i<=m;i++) the         { -             intQ =nextint ();Wuyi             intp = lower_bound (h,h+en,q)-h; the             if(p!=n && h[p]==q &&!flag[p])//flag[] used to mark the upper and lower bounds -             { WuRET =Cnt[q]; -FLAG[P] =true; About             } $             ElseRET =0; -printf"%d\n", ret); -         } -     } A     return 0; +}
View Code

You can also use map hash to do BAA ~

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cstdlib>6#include <cmath>7#include <vector>8#include <queue>9#include <map>Ten#include <Set> One  A using namespacestd; -  -typedefLong Longll; the  -Map<ll,int>CNT; -ll h[1000010],q; -  + ll GetInt () - { +ll t =0; A     Charc =GetChar (); at //While (c> ' 9 ' | | | c< ' 0 ') - //C = GetChar (); -      while(c>='0'&& c<='9') -     { -t = t*Ten+c-'0'; -c =GetChar (); in     } -     returnT; to } +  - intMain () the { *     CharC; $     intn,m;Panax Notoginseng      while(~SCANF ("%d%d",&n,&m)) -     { the GetChar (); + cnt.clear (); A          for(intI=1; i<=n;i++) the         { +H[i] =getInt (); -cnt[h[i]]++; $         } $Map<ll,int>:: iterator iter; -          for(intI=1; i<=m;i++) -         { theQ =getInt (); -             //iter = cnt.find (q);Wuyi             if(Cnt.count (q)) the             { -printf"%d\n", Cnt[q]); WuCNT[Q] =0; -             } About             Elseprintf"0\n"); $         } -     } -     return 0; -}
View Code

Hdu 5199 Gunner (Hash | | sort + binary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.