zoukankan      html  css  js  c++  java
  • poj 2785 4 Values whose Sum is 0

    http://poj.org/problem?id=2785

      简单hash,不过不能用set,而且数组不能开太大。

      才几十行的代码,当时个人赛居然不会... - -

    View Code
     1 #include <cstdlib>
     2 #include <cstring>
     3 #include <cstdio>
     4 
     5 using namespace std;
     6 
     7 const int maxn = 4001;
     8 const int mod = 15999997;
     9 int hash[maxn * maxn], cnt[maxn * maxn];
    10 int rec[maxn][4];
    11 
    12 void insert(int n){
    13     int p = (n << 2) % mod;
    14 
    15     if (p < 0) p += mod;
    16     while (cnt[p] && hash[p] != n) p++;
    17     hash[p] = n;
    18     cnt[p]++;
    19 }
    20 
    21 int count(int n){
    22     int p = (n << 2) % mod;
    23 
    24     if (p < 0) p += mod;
    25     while (cnt[p] && hash[p] != n) p++;
    26     return cnt[p];
    27 }
    28 
    29 int deal(int n){
    30     memset(cnt, 0, sizeof(cnt));
    31     for (int i = 0; i < n; i++){
    32         for (int j = 0; j < n; j++){
    33             insert(rec[i][0] + rec[j][1]);
    34         }
    35     }
    36     int ret = 0;
    37 
    38     for (int i = 0; i < n; i++){
    39         for (int j = 0; j < n; j++){
    40             ret += count(- rec[i][2] - rec[j][3]);
    41         }
    42     }
    43 
    44     return ret;
    45 }
    46 
    47 int main(){
    48     int n;
    49 
    50     while (~scanf("%d", &n)){
    51         for (int i = 0; i < n; i++){
    52             for (int j = 0; j < 4; j++){
    53                 scanf("%d", &rec[i][j]);
    54             }
    55         }
    56         printf("%d\n", deal(n));
    57     }
    58 
    59     return 0;
    60 }

    ——written by Lyon

  • 相关阅读:
    关键词提取算法TextRank
    我的博文目录整理
    Windows Azure一些小技巧集合
    js数组和树互转
    this.props.form.validateFields回调不执行问题
    d3的4.x与3.x版本的区别
    d3提示框,虚线,选择区域
    d3布局
    d3文件导入和导出
    d3交互
  • 原文地址:https://www.cnblogs.com/LyonLys/p/poj_2785_Lyon.html
Copyright © 2011-2022 走看看