51nod-1110-Distance and minimum V3

Source: Internet
Author: User

ACM Templates Description

Solving

W[i] As the number of x[i] points, that is, a weight of w[i] x[i] points to w[i] x[i] point, and then to X[i] sort, and the last pair of paired children subtraction can be.

What needs to be emphasized here is to avoid data overflow, which pits me for half an hour ...
Because of their own definition of p[a]. Both P and power are int, so in the process of matching, there is an int * int overflow, and I foolishly assign it to a long long sum, but ignore it-there is an overflow vulnerability in itself ... O (>﹏<) o Don't be so careless again. It's all for the trouble of being taken for granted. Code

 #include <iostream> #include <algorithm> using namespace std;

const int MAXN = 1e4 + 10;
    struct point {int P;
int power;

} P[MAXN];

BOOL CMP (point A, point B) {return A.P < B.P;}
    int main (int argc, const char * argv[]) {int N;

    Cin >> N; for (int i = 0; i < N; i++) {cin >> p[i].
    P >> P[i].power;

    } sort (p, p + N, CMP);
    A long long sum = 0;
    int flag = N-1;
    Long long power;
    for (int i = 0; i < flag;) {power = p[flag].power > P[i].power?
        P[i].power:p[flag].power; Sum + = (P[flag]. P-p[i].    P) * POWER;
        The BUG prevents an int * int from overflowing, so power is defined as long long p[flag].power-= power;
        P[i].power-= power; if (! (
        P[flag].power)) {flag--; } if (! (
        P[i].power)) {i++;
    }} std::cout << sum << ' \ n ';
return 0; }
Reference

51Nod 1108 distance and minimum V2

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.