The last question of the 2018 acm competition in Shandong Province is solved.

When I first started doing it, I thought about the backpack directly, but I didn't expect to be greedy. After I finished writing, the sample was passed, but when I handed it over to oj, I couldn't pass it, and it was uncomfortable. Later, I learned that I have to use greed and backpack together, greedy. The basic idea is to sort first by comparing the size of the cross multiplication, and then do it with a backpack. The abscissa of the backpack represents the number of pieces, and the ordinate represents the time.

Topic link: http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Contest/contestproblem/cid/2495/pid/3903

code show as below:

#include<bits/stdc++.h>
using namespace std;
struct Node
{
    int a,d,c;
}q[2100]; //Used to store the input data
int n,t;
int visited[5100];
bool cmp (Node a,Node b) //Set the comparison condition, when the time of the first descending speed multiplied by the second is greater than the descending speed of the second multiplying the time of the first, the first is in the front, the second a row after
{
    return (ad*bc>bd*ac);
}
int main()
{
    cin>>n>>t;
    for(int i=1;i<=n;i++)
        cin>>q[i ].a;
    for(int i=1;i<=n;i++)
        cin>>q[i].d;
    for(int i=1;i<=n;i++)
        cin>>q[i]. c;
    sort(q+1,q+n+1,cmp);
    for(int i=1;i<=n;i++)
        for(int j=t;j>=q[i].c;j- -)
            visited[j]=max(visited[j],visited[j-q[i].c]+(q[i].a-j*q[i].d));
    int ans=0;
    for(int j=t;j>=1;j--)//寻找最大值
    ans=max(ans,visited[j]);
    cout<<ans<<endl;

}

Note: Assuming that the time to complete the first question is 2, and the given time is 10, the backpack starts to fill the array from 10 to 2. At this time, it is wrong to have empty spaces from 3 to 10, because the score obtained at this time is small. , because the score that should be obtained is the initial score minus (the rate of decline multiplied by the time it takes to complete the question from the start of the game), if the initial time is greater than 2, the score obtained is smaller. So the last number to find the best answer may not be the last number in the array, so you need to find the maximum value in the array at the end.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767960&siteId=291194637