zoukankan      html  css  js  c++  java
  • JZOJ 3508. 【NOIP2013模拟11.5B组】好元素

    Description

    小A一直认为,如果在一个由N个整数组成的数列An中,存在Am + An + Ap = Ai(1 <= m, n, p < i)(m, n, p可以相同)的话,Ai就是一个“好元素”。现在,小A有一个数列,他想知道这个数列中有多少个“好元素”,请你帮帮他。
     

    Input

    第一行只有一个正整数N,意义如上。

    第二行包含N个整数,表示数列An。

    Output

    输出一个整数,表示这个数列中“好元素”的个数。
     

    Sample Input

    输入1:
    2
    1 3
    输入2:
    6
    1 2 3 5 7 10
    输入3:
    3
    -1 2 0

    Sample Output

    输出1:
    1
    输出2:
    4
    输出3:
    1
     

    Data Constraint

    对于10%的数据  1<=N<=10

    对于40%的数据  1<=N<=500    -10^5<=Ai<=10^5

    对于70%的数据  1<=N<=5000   -10^6<=Ai<=10^6

    对于100%的数据 1<=N<=5000   -10^9<=Ai<=10^9
     
    做法:预处理ai + aj可以达到的值,用hash存起来,然后枚举l, k就好了
     
    代码如下:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <string>
     4 #include <iostream>
     5 #include <algorithm>
     6 #define N 5007
     7 #define mo 14150547
     8 #define z 2000000000
     9 #define LL long long
    10 using namespace std;
    11 LL n, a[N], h[mo + 1];
    12 int ans;
    13 bool f;
    14 
    15 LL hash(LL x)
    16 {
    17     LL p = x % mo;
    18     while (h[p] != 0 && h[p] != x - z && p <= mo)    p = (p + 1) % mo;
    19     return p;
    20 }
    21 
    22 void work()
    23 {
    24     for (int i = 1; i <= n; i++)
    25     {
    26         for (int j = 1; j < i; j++)
    27             if (h[hash(a[i] - a[j] + z)] == a[i] - a[j] && a[i] - a[j] != 0)
    28             {
    29                 ans++;
    30                 break;
    31             }
    32             else if (a[i] - a[j] == 0)
    33             {
    34                 if (f)
    35                 {
    36                     ans++;
    37                     break;
    38                 }
    39             }
    40         for (int j = 1; j <= i; j++)
    41         {
    42             LL p = (a[i] + a[j] + z) % mo;
    43             while (h[p] != a[i] + a[j] && h[p] != 0 && p <= mo)    p = (p + 1) % mo;
    44             if (a[i] + a[j] != 0)    h[p] = a[i] + a[j];
    45             else f = true;
    46         }
    47     }
    48 }
    49 
    50 int main()
    51 {
    52     freopen("good.in", "r", stdin);
    53     freopen("good.out", "w", stdout);
    54     scanf("%lld", &n);
    55     for (int i = 1; i <= n; i++)
    56         scanf("%lld", &a[i]);
    57     work();
    58     cout << ans;    
    59 }
    View Code
  • 相关阅读:
    Max Sum Plus Plus HDU
    Monkey and Banana HDU
    Ignatius and the Princess IV HDU
    Extended Traffic LightOJ
    Tram POJ
    Common Subsequence HDU
    最大连续子序列 HDU
    Max Sum HDU
    畅通工程再续
    River Hopscotch POJ
  • 原文地址:https://www.cnblogs.com/traveller-ly/p/9338550.html
Copyright © 2011-2022 走看看