(The 11th Blue Bridge Cup Finals) H: Answers (greedy)

Topic link: "Blue Bridge Cup" Practice System

 

Analysis: I feel that there is a problem in the description of the meaning of this question. The title says that a classmate needs to prepare for s seconds to enter the office, and it takes e seconds to leave the office. He only said that it takes s seconds to prepare for entering the office, but did not say that in the previous After a classmate sent a message and before leaving the office, I couldn't prepare, so I couldn't come up with the right idea. I also hope that everyone will pay attention to this.

The following is the correct thinking analysis

Assuming that the optimal order of answering questions is 1, 2, 3..., n, each number represents a classmate

Then the moment when classmate No. 1 sends a message is s1+a1

The moment when classmate No. 2 sends a message is s1+a1+e1+s2+a2

The moment when classmate No. 3 sends a message is s1+a1+e1+s2+a2+e2+s3+a3

……

The moment when classmate n sends a message is s1+a1+e1+s2+a2+e2+s3+a3+e3+...+s(n-1)+a(n-1)+e(n-1)+sn +an

Add up the time when the n students send messages \sum_{i=1}^{n}((n-i+1)*(si+ai))+\sum_{i=1}^{n}((n-i)*ei), after simplification \ sum_ {i = 1}^{n} ((ni)*(si+ai+ei))+\ sum_ {i = 1}^{n} (si+ai), the summation value on the right is fixed and will not change with the change of order, and si+ai+ei on the left is also for each number. It is fixed, but the number of times each is calculated is different. The more times it is calculated, the more times it is calculated. Therefore, the smaller number of the sum of the three numbers s, a and e should be placed in the front . This is the topic of this topic. greedy strategy

Here is the code:
 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=1300;
struct node{
	int s,a,e;
}p[N];
bool cmp(node a,node b)
{
	return (a.a+a.e+a.s)<(b.a+b.e+b.s);
}
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		scanf("%d%d%d",&p[i].s,&p[i].a,&p[i].e);
	sort(p+1,p+n+1,cmp);
	long long ans=0;
	for(int i=1;i<=n;i++)
		ans+=(n-i)*(p[i].a+p[i].e+p[i].s)+p[i].a+p[i].s;
	cout<<ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/AC__dream/article/details/124025236