zoukankan      html  css  js  c++  java
  • POJ 3474 Gold Balanced Lineup Hash

    题意一开始不是很明确, 后来发现是每一种特征出现的次数相同

    这样一来就变成简单hash问题了,如果把每个特征看看做是一个(n+1)进制数的话,对奶牛序列求一下前缀和,如果i - j这一段每一种特征出现的次数相同的话,把i - 1点和j点的每一位减去所有位中的最小值之后,必然相等,所以hash判断一下就好。

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <map>
    #include <set>
    #include <vector>
    #include <string>
    #include <queue>
    #include <deque>
    #include <bitset>
    #include <list>
    #include <cstdlib>
    #include <climits>
    #include <cmath>
    #include <ctime>
    #include <algorithm>
    #include <stack>
    #include <sstream>
    #include <numeric>
    #include <fstream>
    #include <functional>
    
    using namespace std;
    
    #define MP make_pair
    #define PB push_back
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef vector<int> VI;
    typedef pair<int,int> pii;
    const int INF = INT_MAX / 3;
    const double eps = 1e-8;
    const LL LINF = 1e17;
    const double DINF = 1e60;
    const int maxn = 100000 + 10;
    const int maxk = 30;
    const int mod = 65536;
    int n,k;
    
    struct Node {
        int data[maxk];
        Node(int val = 0) {
            int pos = 0;
            memset(data,0,sizeof(data));
            while(val) {
                data[pos++] = val & 1;
                val >>= 1;
            }
        } 
    
        bool operator == (const Node &node) const {
            for(int i = 0;i < k;i++) if(node.data[i] != data[i]) return false;
            return true;
        }
    };
    
    Node operator + (Node a,Node b) {
        for(int i = 0;i < k;i++) {
            a.data[i] += b.data[i];
        }
        return a;
    }
    
    
    void con(Node &node) {
        int minval = INF;
        for(int i = 0;i < k;i++)
            minval = min(minval,node.data[i]);
        for(int i = 0;i < k;i++) 
            node.data[i] -= minval;
    }
    
    int gethash(const Node &node) {
        int ret = 0;
        for(int i = 0;i < k;i++) {
            ret = ret * (n + 1) + node.data[i];
        }
        return ret & (mod - 1);
    }
    
    
    Node cow[maxn],val[maxn];
    int head[mod],nxt[maxn],pos[maxn],sz;
    
    int ask(const Node &node,int p) {
        int hc = gethash(node);
        for(int i = head[hc];~i;i = nxt[i]) {
            if(val[i] == node) return pos[i];
        }
        val[sz] = node; pos[sz] = p;
        nxt[sz] = head[hc]; head[hc] = sz++;
        return p;
    }
    
    int main() {
        while(scanf("%d%d",&n,&k) != EOF) {
            memset(head,-1,sizeof(head));
            sz = 0;
            for(int i = 1;i <= n;i++) {
                int tmp; scanf("%d",&tmp);
                cow[i] = cow[i - 1] + Node(tmp);
            }
            for(int i = 1;i <= n;i++) con(cow[i]);
            int ans = 0;
            for(int i = 0;i <= n;i++) {
        //        for(int j = 0;j < k;j++) printf("%d ",cow[i].data[j]); putchar('
    ');
                int pos = ask(cow[i],i);
                ans = max(ans,i - pos);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    27. 移除元素
    LeetCode---9.回文数
    PAT 1098 Insertion or Heap Sort (25)
    PAT 1146 Topological Order
    PAT 1147 Heaps(30 分)
    数据结构 二分查找1
    数据结构 树
    PAT 1126 Eulerian Path
    PAT 1111 Online Map (30)
    PAT 1072 Gas Station (30)
  • 原文地址:https://www.cnblogs.com/rolight/p/3949983.html
Copyright © 2011-2022 走看看