Jingdong C++ Development Engineer Spring 2020 (Programming 1)

Title description:

The recent economic downturn, Little A is going to sell part of the stocks he holds.
He holds a total of n stocks, limited by the trading platform, he can only sell m stocks at most every day. It
is known that the i-th stock will lose a_i yuan every day, that is, if the stock is sold on the k-th day, the amount of loss It is k* a_i element.
Now he hasn't decided how many stocks to sell, so he will ask you a few questions, that is, if you sell q stocks,
what is the minimum loss for these q stocks.

enter

The first line of input contains two positive integers n and m, indicating the number of stocks held by small A and the maximum number of stocks that can be sold every day. (1<=n, m<=50000)
Input the second line contains n positive integers, the i-th number a_i represents the daily loss amount of the i-th stock. (1<=a_i<=10000)
The next line has a positive integer Q, indicating the number of inquiries. (1<=Q<=50000)
There are Q lines after that, each line has a positive integer q, which means if you want to sell q stocks, what is the minimum amount of loss. (q<=n)

output

For each query, output the corresponding result.

sample input

5 2
1 2 3 4 5
2
3
5

sample output

7
22

hint

Example explanation:
There are 5 stocks in total, and a maximum of 2 stocks can be sold every day.
q is 2, that is, there are two queries.
For the first query 3, 3 stocks need to be sold, and the loss is the smallest when choosing stocks 1, 2, and 3.
Among them, if you sell stocks No. 2 and No. 3 on the first day, the loss is 5 yuan; if you sell No. 1 stock on the second day, the loss is 2*1=2 yuan; the final total loss is 7 yuan.
For the second query 5, stocks No. 1, 2, 3, 4, and 5 need to be sold. Among them, when selling stocks No. 4 and No. 5 on the first day, the loss is 9 yuan; when selling No. 2 and No. 3
stocks on the second day, the loss is 2*2+2*3=10 yuan;
* 1=3 yuan; the final total loss is 22 yuan.

the code
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int question(int m, int qi, vector<int> a) {
    
    

	int day = qi / m;
	int remainder = qi % m, sum = 0;

	if (remainder != 0) {
    
    
		for (int i = 1; i <= remainder; i++)
			sum += a[(day)*m + i - 1] * (day + 1);
	}
	for (int d = 0; d < day; d++) {
    
    
		for (int i = 0; i < m; i++) {
    
    
			sum += a[d * m + i] * (d + 1);
		}
	}

	return sum;
}

int main() {
    
    
	int n, m, Q;
	cin >> n >> m;
	vector<int> a(n + 1, 0);
	for (int i = 1; i <= n; i++) {
    
    
		cin >> a[i];
	}
	cin >> Q;
	vector<int> q(Q + 1, 0);
	for (int i = 1; i <= Q; i++) {
    
    
		cin >> q[i];
	}

	sort(a.begin() + 1, a.end(), less<int>());//小到大排序

	for (int i = 1; i <= Q; i++) {
    
    
		vector<int>ai(a.begin() + 1, a.begin() + 1 + q[i]);//选取前qi个
		sort(ai.begin(), ai.end(), greater<int>());//大到小排序
		cout << question(m, q[i], ai) << endl;
	}

	return 0;
}

Personal ability is limited, there may be mistakes and omissions, please comment to let us know, thank you!

Guess you like

Origin blog.csdn.net/qq_27677599/article/details/105728480