[Problem background]
One day, SXC, zsx, WL went to login to take the motorboat. They had an appointment with other people, and they were put up with pigeons, 3 people only have one person to pay for X yuan to go To the motorboat (very expensive ). When I sat down, I found that it would be no longer fun if there were too many seats for the motorboat. Three people seem to be the most interesting, but are they?
[Problem description] assume that N people are going to take one motorboat. The happiness level of a single motorboat is CI. If multiple people are there, their happiness level will be less than di, the sum of request happiness reaches the maximum limit. (Assuming the capacity of the motor boat is large enough ).
[File input]
There are three lines in the input file: Row 1st is an integer N, and row 2nd has n integers, which indicate the happiness level Ci (1 <= CI <= 10000) of each person taking the boat in turn ); there are n integers in the row 3rd, indicating the decrease of happiness (1 <= di <= 10) for every one more person in turn ).
[File output]
Two rows should be output: One integer in the second row, which is the sum of the greatest happiness; one integer in the second row, is the number of people on the motorboat corresponding to the sum of the greatest levels of happiness (if there are multiple solutions, the most people will be output ).
[Example input]
6
10 10 10 10 10 9
2 2 2 2 3
[Sample output]
18
3
[Example] when the first three people go to the motorboat, the sum of the happiness levels reaches the maximum. The happiness levels of each person are 10-2*2 = 6, and the sum is 18.
[Data range] for 30% of data, n <= 20; for 100% of data, n <= 1000.
Such a question can easily be determined to be greedy, but DP is not good, because the question should be considered to be ineffective; (and I personally feel that it is hard to write such a question)
But how greedy?
In my first test, all of them were WA =. Maybe it was because the write Greed had already formed a formula. I always thought about sorting first and then messing around with things.
So this question is also a lesson for me.
Let's talk about the idea of this question.
First, we can't judge who is the best choice.
Because the value of happiness is large, the value of decrease may be large. Similarly.
Therefore, each time we choose, we can determine who is the best;
It's actually quite simple... (I was too stupid to think)
But it is simulation + greedy thoughts;
Let's enumerate the number of people sitting in the motorboat, calculate the remaining happiness level of each person under the current number of people I, and calculate the total happiness level of each person;
Update the answer;
Code attached:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int maxn=1001;int n,m;struct node{int c,d;}a[maxn];int f[maxn];int ans,tot=1;bool cmp(int x,int y){return x>y?1:0;}int main(){//freopen("data.txt","r",stdin);freopen("launch.in","r",stdin);freopen("launch.out","w",stdout);scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d",&a[i].c),ans=max(ans,a[i].c);for(int i=1;i<=n;i++) scanf("%d",&a[i].d);for(int i=1;i<=n;i++){ int temp=0;for(int j=1;j<=n;j++) f[j]=a[j].c-(i-1)*a[j].d;sort(f+1,f+n+1,cmp);for(int j=1;j<=i;j++){temp+=f[j];}if(ans<=temp){ans=temp;tot=i;}}cout<<ans<<"\n"<<tot;return 0;}
Noip recommendation series: motorboats [greedy]