Nazrin the Greeeeeedy Mouse(2023牛客暑期多校训练营5)

链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
 

There're nn cheeses in the house. The ii-th cheese is between point ii and point i+1i+1. The ii-th cheese's size is aiai​and its weight is bibi​.
房子里有 nn 奶酪。 ii -th奶酪介于点和点 ii i+1i+1 之间。 ii 奶酪的大小和 aiai​ 重量是 bibi​ 。
Nazrin is at point 11 in the beginning and wants to steal some cheeses. She has mm chances to take the cheeses:
纳兹林 11 一开始就想偷一些奶酪。她有机会 mm 拿奶酪:
In the ii-th time, Nazrin brings a bag of size sziszi​. Since she's greedy, for i>1i>1, szi≥szi−1szi​≥szi−1​ always holds. She can travel from point 11 and take some cheeses. She can only travel from point xx to x+1x+1, or backwards. If she wants to travel from point xx to point x+1x+1, she has to dig a hole on the xx-th cheese or take it. She can't take a cheese with a hole on it. Of course, the sum of the cheeses' size she takes in the ii-th time can't be larger than sziszi​. After taking the cheeses, she needs to go back to point 11.
在 ii -第一次,纳兹林带来了一个大小的 sziszi​ 袋子.既然她贪婪,因为 i>1i>1 , szi≥szi−1szi​≥szi−1​ 永远持有。她可以从点 11 出发,拿一些奶酪。她只能从一个点到 x+1x+1 另一个点 xx 或向后移动。如果她想从一个点到另一个点 xx x+1x+1 旅行,她必须在奶酪上 xx 挖一个洞或拿走它。她不能拿一个有洞的奶酪。当然,她第-次吃 ii 的奶酪大小的总和不能大于 sziszi​ .吃完奶酪后,她需要回到点 11 。
Please maximize the sum of the weights of taken cheeses and output it.
请最大化所采奶酪的重量总和并输出它。

输入描述:

The first line contains two integer nn (1≤n≤2001≤n≤200) and mm (1≤m≤1051≤m≤105).
For the following nn lines, the ii-th line contains two integers aiai​ (1≤ai≤2001≤ai​≤200) and bibi​ (1≤bi≤1051≤bi​≤105).
The next line contains mm integers, the ii-th one is sziszi​ (1≤szi≤2001≤szi​≤200). Notice that for i>1i>1, szi≥szi−1szi​≥szi−1​.

输出描述:

One integer, the maximum sum of the weights of taken cheeses.
示例1

输入

复制
5 3
2 10
2 5
1 22
3 7
6 8
1 3 7

输出

复制
44

备注:

In the example, Nazrin can take actions below to maximize the weight of taken cheeses:

In the first time, Nazrin stays at point 11 and gives up the first chance to take cheese.

In the second time, Nazrin takes the cheese between point 11 and point 22 and returns to point 11.

In the third time, Nazrin takes all cheeses between point 22 and point 55, and goes back to point 11.

思路:

        首先考虑到sizei+1>sizei,所以当m>n时,只用保留最后n次即可。(前面的操作都取不到奶酪,可以去除 ) ,其次再预处理 l----r区间内空间为k的最大价值。随后再通过01背包进行选取就可以了

Ps:赛时是一点都没有想到,可恶啊


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include 
//#include
#include
#include
#include
#include
#define dbug cout<<"hear!"<=c;a--)
#define no cout<<"NO"<,greater >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair PII;
typedef pair PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 1e6+ 10;
const ll mod1 =998244353;
const ll mod2 =1e9+7;
const ll hash_num = 3e9+9;
ll n,m,ca, k,ans;
ll arr[N],brr[N],crr[N];
 //ll h[N],ne[N],e[N],w[N],book[N],idx;

ll dp[300][300];
ll b[300][300][300];

void solve()
{
 cin >> n >> m;
 rep(i,1,n)cin >> arr[i] >> brr[i];
 rep(i,1,m)cin >> crr[i];
 int f=0;
 if(m>n)
 {
    f=1;
    rep(i,1,n)
    {
        crr[i]=crr[m-(n-i)];
    }
 }
 rep(k,1,200)
 {
    rep(l,1,n)
    {
        rep(r,l,n)
        {
            b[l][r][k]=b[l][r-1][k];
            if(k >= arr[r])
            {
                b[l][r][k] = max(b[l][r][k], b[l][r-1][k-arr[r]]+brr[r]);
            }
        }
    }
 }
 ll ans=0;
 ll dai;
 if(f==1)dai=200;
 else dai=m;
 rep(i,1,dai)
 {
    rep(j,1,n)
    {
        rep(k,0,j-1)
        {
            dp[i][j]=max(dp[i][j],dp[i-1][k]+b[k+1][j][crr[i]]);
            ans=max(ans,dp[i][j]);
        }
    }
 }
 cout << ans<>_;
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}

你可能感兴趣的:(算法,c++,数据结构)