UVA 1152 4 Values whose Sum is 0

Source: Internet
Author: User

Thinking about it:

When I saw this problem, I immediately thought of the HDU 1496, the two topics have similarities. My personal hdu1496: Click here

Save all the first and second numbers and A + B, record the number of occurrences of each number, and then calculate each C + D, for each C + D, the query-(c + D) appears several times.

So the subject needs a way to save all A + B's and its occurrences.

The most straightforward approach is to use the map in the STL, but the map is easy to time out when the data is large, so this approach is not ideal here.

Another way is to create a hash table yourself, because and may be negative, so you can add offset, so that all and plus the value of offset is greater than 0, and this value as a key. A hash table can be represented by an array of vectors.

Ps:

Compared to my own previous practice in hdu1496, this time I naturally thought of using vectors to represent the hash array, a lot of convenience. In general, the way you think about it should be easier to impress.

Reference:

0.http://acm.lilingfei.com/71_71

Code:

/** * AC @ SEP 2nd */#include <bits/stdc++.h>using namespace std;typedef long long ll;const int MAXN = 4000 + 5 0;const int hashsize = 999983;     149993-2.528s 299993-2.205s 999983-2.658const int OFFSET = 1 << 30;struct node{int value;    LL CNT;        Node (int V, LL c) {value = V;    CNT = C;  } Node () {}};int lst[maxn][5], n;std::vector<node> table[hashsize];void init () {for (int i=0; i

  

UVA 1152 4 Values whose Sum is 0

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.