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