传送门

Description

奶牛想证明它们是聪明而风趣的。为此,贝西筹备了一个奶牛博览会,她已经对N 头奶牛进行了面试,确定了每头奶牛的智商和情商。

贝西有权选择让哪些奶牛参加展览。由于负的智商或情商会造成负面效果,所以贝西不希望出展奶牛的智商之和小于零,或情商之和小于零。

满足这两个条件下,她希望出展奶牛的智商与情商之和越大越好,请帮助贝西求出这个最大值。

Input

  • 第一行:单个整数N,
  • 第二行到第N + 1 行:第i + 1 行有两个整数:Si 和Fi,表示第i 头奶牛的智商和情商。

Output

  单个整数:表示情商与智商和的最大值。贝西可以不让任何奶牛参加展览,如果这样做是最好的,输出0

Sample Input

-
-
- - -

Sample Output


Hint

选择第一头,第三头,第四头奶牛,智商和为−5+6+2 = 3,情商和为7−3+1 = 5。再加

入第二号奶牛可使总和提升到10,不过由于情商和变成负的了,所以是不允许的

1 ≤ N ≤ 400;−1000 ≤ Si; Fi ≤ 1000

Solution

  一眼看出这是个背包,然后…………然后呢???

  最初想法是些bool型背包,发现爆空间(8000000*8000000)直接GG,于是放弃,想到之前的栈做法写了一个栈,使用map判重,详见代码一。然后发现map的复杂度过高,并且占据的空间依然很大,于是GG。

  考虑状态压缩。

  由于数组的下标可以存储一定的信息,我们使用f[i]的值存储s=i时最大的f值,由于要求结果最大,s一定时f显然越大越好。于是满足无后效性。注意要对下标进行平移,使用maxup和maxdown记录当前更新的上限和下限,每次进行转移。

  剪枝:如果s和f都小于零,那么你们今日知罪又有何用?显然他们对答案不会有任何好的贡献,直接continue即可。

Code

#include<map>
#include<cstdio>
#include<algorithm>
#define maxn 10010
#define maxm 4000010 inline void qr(int &x) {
char ch=getchar();int f=;
while(ch>''||ch<'') {
if(ch=='-') f=-;
ch=getchar();
}
while(ch>=''&&ch<='') x=(x<<)+(x<<)+(ch^),ch=getchar();
x*=f;
return;
} inline int max(const int &a,const int &b) {if(a>b) return a;else return b;}
inline int min(const int &a,const int &b) {if(a<b) return a;else return b;}
inline int abs(const int &x) {if(x>) return x;else return -x;} inline void swap(int &a,int &b) {
int c=a;a=b;b=c;return;
} struct Frog {
int s,f;
Frog() {s=f=;}
Frog(int x,int y) {s=x;f=y;}
};
Frog frog[maxm];
int top=;
inline bool operator <(const Frog &a,const Frog &b) {
if(a.s^b.s) return a.s<b.s;
return a.f<b.f;
} bool ins[maxn][maxn];
Frog temp; int n,a,b; inline bool cmp(const Frog &a,const Frog &b) {
int sa=a.s+a.f,sb=b.s+b.f;
return sa<sb;
} int main() {
qr(n);
for(int i=;i<=n;++i) {
a=b=;qr(a);qr(b);
if(a<=&&b<=) continue;
int t=top;
for(int j=;j<=t;++j) {
int da=frog[j].s+a,db=frog[j].f+b;
temp=Frog(da,db);
if(ins[da+][db+]) continue;
ins[da+][db+]=true;
frog[++top]=temp;
}
}
std::sort(frog+,frog++top,cmp);
for(int i=top;i;--i) {
if(frog[i].s>=&&frog[i].f>=) {
printf("%d\n",frog[i].s+frog[i].f);return ;
}
}
putchar('');putchar('\n');
return ;
}

代码1 map 45分

#include<cstdio>
#include<cstring>
#define maxn 800005 inline void qr(int &x) {
char ch=getchar();int f=;
while(ch>''||ch<'') {
if(ch=='-') f=-;
ch=getchar();
}
while(ch>=''&&ch<='') x=(x<<)+(x<<)+(ch^),ch=getchar();
x*=f;
return;
} inline int max(const int &a,const int &b) {if(a>b) return a;else return b;}
inline int min(const int &a,const int &b) {if(a<b) return a;else return b;}
inline int abs(const int &x) {if(x>) return x;else return -x;} inline void swap(int &a,int &b) {
int c=a;a=b;b=c;return;
} int n,frog[maxn],a,b,ans,maxup=,maxdown=;
const int away=; int main() {
qr(n);
std::memset(frog,-0x3f,sizeof frog);frog[away]=;
for(int i=;i<=n;++i) {
a=b=;qr(a);qr(b);
if(a<=&&b<=) continue;
int dd=maxdown+a;
if(a>) {
for(int j=maxup+a;j>=dd;--j)
frog[j]=max(frog[j],frog[j-a]+b);
maxup+=a;
}
else {
for(int j=dd;j<=maxup;++j)
frog[j]=max(frog[j],frog[j-a]+b);
maxdown+=a;
}
}
for(int i=away;i<=maxup;++i)
{
if(frog[i]>=) ans=max(frog[i]+i-away,ans);
}
printf("%d\n",ans);
return ;
}

Summary

  1、使用map且结构体为第一关键字时,需要重载合法的小于号供map使用,如果需要规则不同的sort,需要手写比较函数,不能重载运算符。

   2、二维dp且空间较大的时候可以考虑状压,把其中一维作为下标处理。

   3、这题是怎么被评到绿题去的??我怎么做着和黑题一样

最新文章

  1. Lesson 5 No wrong numbers
  2. 在AngularJs中怎么设置请求头信息(headers)及不同方法的比较
  3. 黑马程序员——【Java高新技术】——案例:银行业务调度系统
  4. ELK&mdash;&mdash;Elasticsearch 搭建集群经验
  5. c++ 复习内容
  6. 使用MyEclipse可视化开发Hibernate实例
  7. fzu 1675 The Seventy-seven Problem
  8. C#的checked和unchecked
  9. PhpStorm创建Drupal模块项目开发教程(4)
  10. TOGAF架构开发方法(ADM)之需求管理阶段
  11. 《JS权威指南学习总结--4.13运算符》
  12. Java将List/JavaBean转成Json
  13. Docker - 生成镜像
  14. win10 uwp 自定义控件 SplitViewItem
  15. 将Excel表中的数据导入到数据库
  16. 网站搭建 (第06天) Ckeditor编辑器
  17. url、querystring模块获取请求request.url中的不同部分图解
  18. Linux安装配置Redis,CentOS7下安装Redis教程
  19. windows下安装ElasticSearch的Head插件
  20. 分发系统介绍 expect脚本远程登录 expect脚本远程执行命令 expect脚本传递参数

热门文章

  1. Python3开启Http服务
  2. C#-返回相对时间函数
  3. 【WXS数据类型】Object
  4. Spring Cloud(十):服务网关 Zuul(路由)【Finchley 版】
  5. [Clr via C#读书笔记]Cp6类型和成员基础
  6. sql STUFF 分组
  7. hibernate 异常a different object with the same identifier value was already associated with the session
  8. VBA基础之Excel 工作薄(Book)的操作(三)
  9. 《javascript模式--by Stoyan Stefanov》书摘--字面量和构造函数
  10. java利用POI实现读取Word并获取指定样式的文本