首页 技术 正文
技术 2022年11月13日
0 收藏 713 点赞 5,158 浏览 1151 个字

[JOISC2014]ストラップ

题目大意:

有\(n(n\le2000)\)个挂饰,每个挂饰有一个喜悦值\(b_i(|b_i|\le10^6)\),下面有\(b_i(b_i\le10^6)\)个挂钩,可以用来挂别的挂饰。一开始只有一个挂钩,问喜悦值总和的最大值。

思路:

\(f[i][j]\)表示考虑前\(i\)个挂饰,还多\(j\)个钩子时,喜悦值总和的最大值。

考虑同样的一堆挂件,先挂\(a_i\)大的可以尽可能避免钩子不够用的情况。因此需要先将所有挂件按\(a_i\)从大到小排序。

时间复杂度\(\mathcal O(n^2)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<algorithm>
#include<functional>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
const int N=2001;
struct Node {
int a,b;
bool operator > (const Node &rhs) const {
return a>rhs.a;
}
};
Node p[N];
int f[N][N];
inline void upd(int &a,const int &b) {
a=std::max(a,b);
}
int main() {
const int n=getint();
for(register int i=1;i<=n;i++) {
p[i].a=getint();
p[i].b=getint();
}
std::sort(&p[1],&p[n]+1,std::greater<Node>());
std::fill(&f[0][0],&f[0][n]+1,INT_MIN);
f[0][1]=0;
for(register int i=1;i<=n;i++) {
std::copy(&f[i-1][0],&f[i-1][n]+1,f[i]);
for(register int j=1;j<=n;j++) {
if(f[i-1][j]==INT_MIN) continue;
upd(f[i][std::min(j-1+p[i].a,n)],f[i-1][j]+p[i].b);
}
}
int ans=0;
for(register int i=0;i<=n;i++) {
upd(ans,f[n][i]);
}
printf("%d\n",ans);
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849