D "cloud computing"

Technology industrial revolution also spring breeze blew Guangxi University, our innovation and entrepreneurship projects have gradually embraced awareness of cloud computing, the major manufacturers ECS 1 yuan / month student computer team who are widely purchased and used commercially, this is a very chestnuts.

We all know that cloud computing is run elsewhere, as our next issue will need to make the transfer difficult problem to solve, like you smart.

There are nnn triplets (a, b, c) ( a, b, c) (a, b, c), you need to arrange them in any order in a one-dimensional array, assuming the array to index 111 start, for the number of columns you after a good arrangement, we define his value is:

\sum_{i=1}^{n} [a_i(i + 1) + b_i(n - i) + c_i(i + 2)]

How does this series all possible, he minimum values ​​should be?

Ideas:

First deforming the above equation can be obtained

\sum_{i=1}^{n}i(a_i - b_i + c_i) + (a_i + nb_i + 2c_i)

 Since it is a sum, and for each group  a_i, b_i, c_i are  (a_i - nb_i + 2c_i) the same, and it exists in the summation formula, this means that the value will not change and does not affect the final result

For  i(a_i - b_i + c_i) , apparently  (a_i - b_i + c_i) unchanged, which means that the results related to the coefficient, based on greed, will only need to be calculated in descending order

The final process is to calculate  (a_i - b_i + c_i) the ranking calculation

Data is large, pay attention to long long

#include <bits/stdc++.h>
using namespace std;
int n;
class p {
	private:
		int a, b, c, A, B;
	public:
	p() {

	}
	p(int a, int b, int c) {
		this->a = a;
		this->b = b;
		this->c = c;
		A = (a - b + c);
		B = a + n * b + 2 * c;
	}
	int getA() {
		return A;
	}
	int getB() {
		return B;
	}
};
bool cmp(p a, p b) {
	return a.getA() > b.getA();
}
p arr[100100];
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	int sum = 0;
	for (int i = 1; i <= n; i++) {
		int a, b, c;
		cin >> a >> b >> c;
		arr[i] = p(a, b, c);
		sum += arr[i].getB();
	}
	sort(arr + 1, arr + 1 + n, cmp);
	for (int i = 1; i <= n; i++) {
		sum += arr[i].getA() * i;
	}
	cout << sum << "\n";
	return 0;
}

 

Published 143 original articles · won praise 11 · views 8206

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/103500544