P8806 [蓝桥杯 2022 国 B] 搬砖 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
输入
5 4 4 1 1 5 2 5 5 4 3
输出
10
对于这种,每个对象有两个值,对于某个位置满足前面的一值的和 小于当前 另一个值的题目,都可以采用按照两个值的和来进行从小到大排序
#include <iostream>
#include <bits/stdc++.h>
#include <cstring>
#include <vector>
#include <unordered_map>
#include <queue>
#include <set>
#define int long long
#include <algorithm>
#define x first
#define y second
#define pb emplace_back
#define fu(i,a,b) for(int i=a;i<=b; ++ i)
#define fd(i,a,b) for(int i=a;i>=b; -- i)
#define endl '\n'
#define ms(x,y) memset(x,y,sizeof x)
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<vector<LL>> VVL;
typedef vector<vector<int>> VVI;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef pair<PII,int> PIII;
typedef pair<double,double> PDD;
typedef pair<double,int> PDI;
typedef pair<char,int> PCI;
typedef pair<string,int> PSI;
typedef pair<int,string> PIS;
typedef pair<int,char> PIC;
typedef pair<LL,LL> PLL;
typedef __int128 i128;
typedef unsigned long long ULL;
const int N =1e3 + 10,M = N *25,base =400 ,INF = 0x3f3f3f3f,P = 131;
const double eps = 1e-8;
const int mod = 998244353;
const LL LNF=(LL) INF * INF;
int n,sum;
PII q[N];
int f[M];
// 从 前i个中选 ,当前重量为 j
inline void solve()
{
cin >> n ;
fu(i,1,n)
{
int a,b;cin >> a >> b;
// 读入 重量 和 价值
sum += a;
q[i]={a+b,a};
}
sort(q+1,q+1+n);
int ans =0 ;
fu(i,1,n)
{
// 前面 i-1个物品中选择了的重量 不超过q[i].y;
//当前第i个物品的重量为q[i].x - q[i].y ,那么前i个物品中选择了的重量不超过q[i].x ,超过了就是非法状态
fd(j,q[i].x,q[i].y)
{
f[j] = max(f[j],f[j-q[i].y] + q[i].x - q[i].y);
ans = max(f[j],ans);
}
}
cout << ans << endl;
}
signed main()
{
// freopen("1.txt","r",stdin);
// #define int long long
// init(N-1);
ios
// cout << fixed<<setprecision(2);
int t=1;
// cin>>t;
int now = 1;
while(t -- )
{
// cout<<"Case ";
// cout<<"Case #";
// cout<<"Scenario #";
// cout<< now ++ <<": ";
// cout<< now ++ <<": \n";
solve();
}
return 0;
}