[hdu5199]统计数据的水题

题意:统计一个数出现了多少次,统计后删去它所有的出现。思路:乱搞。。自己没事写的hash,不过赶脚效率有点低。

  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 
 17 using namespace std;
 18 
 19 #define mem0(a) memset(a, 0, sizeof(a))
 20 #define lson l, m, rt << 1
 21 #define rson m + 1, r, rt << 1 | 1
 22 #define define_m int m = (l + r) >> 1
 23 #define Rep(a, b) for(int a = 0; a < b; a++)
 24 #define lowbit(x) ((x) & (-(x)))
 25 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 26 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 27 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 28 
 29 typedef double db;
 30 typedef long long LL;
 31 typedef pair<int, int> pii;
 32 typedef multiset<int> msi;
 33 typedef multiset<int>::iterator msii;
 34 typedef set<int> si;
 35 typedef set<int>::iterator sii;
 36 typedef vector<int> vi;
 37 
 38 const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
 39 const int dy[8] = {0, -1, 0, 1, -1, 1, 1, -1};
 40 const int maxn = 1e6 + 7;
 41 const int maxm = 1e5 + 7;
 42 const int maxv = 1e7 + 7;
 43 const int max_val = 1e6 + 7;
 44 const int MD = 1e9 +7;
 45 const int INF = 1e9 + 7;
 46 const double PI = acos(-1.0);
 47 const double eps = 1e-10;
 48 
 49 inline int ReadInt() {
 50     char c = getchar();
 51     while(!isdigit(c)) c = getchar();
 52 
 53     int x = 0;
 54     while(isdigit(c)) {
 55         x = x * 10 + c - '0';
 56         c = getchar();
 57     }
 58     return x;
 59 }
 60 
 61 inline void WriteInt(int i) {
 62     int p = 0;
 63     static int buf[10];
 64     if(i == 0) buf[p++] = 0;
 65     else while(i) {
 66         buf[p++] = i % 10;
 67         i /= 10;
 68     }
 69     for(int j = p - 1; j >= 0; j--) putchar('0' + buf[j]);
 70 }
 71 template<class T> struct HashList {
 72     vector<T> hash[max_val];
 73     HashList<T>() { }
 74     void clear() { mem0(hash); }
 75     void insert(T x) { hash[x % max_val].push_back(x); }
 76     void erase(T x) {
 77         int node = x % max_val;
 78         for (int i = 0; i < hash[node].size(); i++) {
 79             if (hash[node][i] == x) {
 80                 hash[node].erase(hash[node].begin() + i, hash[node].begin() + i + 1);
 81                 return ;
 82             }
 83         }
 84     }
 85     void erase_all(T x) {
 86         int node = x % max_val;
 87         for (int i = 0; i < hash[node].size(); i++) {
 88             if (hash[node][i] == x) {
 89                 hash[node].erase(hash[node].begin() + i, hash[node].begin() + i + 1);
 90                 i--;
 91             }
 92         }
 93     }
 94     bool find(T x) {
 95         int node = x % max_val;
 96         for (int i = 0; i < hash[node].size(); i++) {
 97             if (hash[node][i] == x) return true;
 98         }
 99         return false;
100     }
101     int find_cnt(T x) {
102         int node = x % max_val, cnt = 0;
103         for (int i = 0; i < hash[node].size(); i++) {
104             if (hash[node][i] == x) cnt++;
105         }
106         return cnt;
107     }
108 };
109 
110 HashList<int> Hash;
111 
112 int main() {
113     //freopen("in.txt", "r", stdin);
114     int n, m;
115     while (cin >> n >> m) {
116         Hash.clear();
117         for (int i = 0; i < n; i++) {
118             Hash.insert(ReadInt());
119         }
120         for (int i = 0, x; i < m; i++) {
121             x = ReadInt();
122             WriteInt(Hash.find_cnt(x));
123             putchar('\n');
124             Hash.erase_all(x);
125         }
126     }
127     return 0;
128 }
View Code

 

posted @ 2015-04-12 05:41  jklongint  阅读(199)  评论(0编辑  收藏  举报