Report problem-solving

author:sdgzy

6.12 Ri

T1 is always feel a false problem, T3 formula pushing seen monotonicity gone.
T2 more interesting:

Title effect: n th item attribute is W, r, i of the selected items, the items will then select a subtracting each value of r.
Seek the greatest value in order to select items.

solution:
Consider for a collection of items that must be chosen (size n)
The answer is: \ (\ SUM w_i - \ SUM r_i * (the n-- i) \)
the sum of the value is \ (\ SUM w_i \) , is a constant value.
Get behind the minimum
that is r from small to large can.
This put order to solve the problem.

Then we can follow from small to large r sort order (this is just thinking, writing to be considered)
no order problem, just simply select the items is a knapsack problem.

After thinking the above end.
Let r from small to large.
And select:
transfer equation
dp [i] [j] = max {dp [i-1] [j], dp [i-1] [j-1] + W [i] -R [i] * (j -1)}
is now very powerful codes force reduction.

#include <bits/stdc++.h>
#define gc getchar()
using namespace std;
const int maxN = 3000 + 7;

inline int gi() {
    int x = 0,f = 1;char c = gc;
    while(c < '0' || c > '9') {if(c == '-')f = -1;c = gc;}
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = gc;}
    return x * f;
}

int f[maxN][maxN];

struct Node {
    int w , r;
}a[maxN];
bool cmp(Node a , Node b) {return a.r > b.r;}

int main() {
    int n = gi();
    for(int i = 1;i <= n;++ i) a[i].w = gi(),a[i].r = gi();
    sort(a + 1,a + n + 1,cmp);
    for(int i = 1;i <= n;++ i) 
        for(int j = 1;j <= i;++ j) 
            f[i][j] = max(f[i - 1][j] , f[i - 1][j - 1] + a[i].w - a[i].r * (j - 1));
    int ans = 0;
    for(int i = 1;i <= n;++ i) ans = max(ans , f[n][i]);
    printf("%d",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/lykkk/p/11009689.html