01背包 加上每次更新解题数目最多 总用时最少 因为要保证用时最少,要先把时长由小到大排序。

没排序 WA了几小时。。链接

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define N 1010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
int dp[N*][];
struct node
{
int ti,v;
}p[N];
bool cmp(node x,node y)
{
return x.ti<y.ti;
}
int main()
{
int i,j,t,n,nn;
cin>>nn;
while(nn--)
{
memset(dp,,sizeof(dp));
cin>>t>>n;
for(i = ; i <= n; i++)
cin>>p[i].ti;
for(i = ; i <= n; i++)
cin>>p[i].v;
sort(p+,p+n+,cmp);
for(i = ; i <= n ; i++)
{
for(j = t ; j >= p[i].ti ; j--)
{
if(dp[j-p[i].ti][]+p[i].v>dp[j][])
{
dp[j][] = dp[j-p[i].ti][]+p[i].v;
dp[j][] = dp[j-p[i].ti][]+;
dp[j][] = dp[j-p[i].ti][]+j;
}
else if(dp[j-p[i].ti][]+p[i].v==dp[j][]&&dp[j-p[i].ti][]+>=dp[j][])
{
if(dp[j-p[i].ti][]+>dp[j][]||dp[j][]>dp[j-p[i].ti][]+j)
{
dp[j][] = dp[j-p[i].ti][]+p[i].v;
dp[j][] = dp[j-p[i].ti][]+;
dp[j][] = dp[j-p[i].ti][]+j;
}
}
}
}
int ans = ,x=,y=;
for(i = t ; i >= ;i--)
{
if(dp[i][]>=ans)
{
if(dp[i][]==ans&&dp[i][]>=x)
{
if(dp[i][]>x||dp[i][]<y)
{
ans = dp[i][];
x = dp[i][];
y = dp[i][];
}
}
if(dp[i][]>ans)
{
ans = dp[i][];
x = dp[i][];
y = dp[i][];
}
}
}
cout<<ans<<" "<<x<<" "<<y<<endl;
}
return ;
}

zojDakar Rally(01背包)的更多相关文章

  1. UVALive 4870 Roller Coaster --01背包

    题意:过山车有n个区域,一个人有两个值F,D,在每个区域有两种选择: 1.睁眼: F += f[i], D += d[i] 2.闭眼: F = F ,     D -= K 问在D小于等于一定限度的时 ...

  2. POJ1112 Team Them Up![二分图染色 补图 01背包]

    Team Them Up! Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7608   Accepted: 2041   S ...

  3. Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

    传送门 Description Hasan and Bahosain want to buy a new video game, they want to share the expenses. Ha ...

  4. 51nod1085(01背包)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1085 题意: 中文题诶~ 思路: 01背包模板题. 用dp[ ...

  5. *HDU3339 最短路+01背包

    In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. codeforces 742D Arpa's weak amphitheater and Mehrdad's valuable Hoses ——(01背包变形)

    题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: #in ...

  7. POJ 3624 Charm Bracelet(01背包)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34532   Accepted: 15301 ...

  8. (01背包变形) Cow Exhibition (poj 2184)

    http://poj.org/problem?id=2184   Description "Fat and docile, big and dumb, they look so stupid ...

  9. hdu3339 In Action(Dijkstra+01背包)

    /* 题意:有 n 个站点(编号1...n),每一个站点都有一个能量值,为了不让这些能量值连接起来,要用 坦克占领这个站点!已知站点的 之间的距离,每个坦克从0点出发到某一个站点,1 unit dis ...

随机推荐

  1. #include<> 和 #include""的区别

    #include< file >编译程序会先到标准函数库中找文件 #include”file” 编译程序会先从当前目录中找文件 参考原文 转: 在C程序中包含文件有以下两种方法: (1)用 ...

  2. php截取某二个特殊字符串间的某段字符串

    在php开发的过程中,有时候会用到截取某二个特殊字符串间的某个字符串,并对这个字符串做特殊的处理,那么对截取出来的字符串做什么特殊处理我们临时无论.我们今天先讲php截取某二个特殊字符串间的某个字符串 ...

  3. USING REFLECTION

    In this section, you take a closer look at the System.Type class, which enables you to access inform ...

  4. 并不对劲的bzoj4198:loj2132:uoj130:p2168:[NOI2015]荷马史诗

    题目大意 有\(n\)(\(n\leq10^5\))种单词,其中第\(i\)种单词在文章中的出现次数为\(w_i\) 要将每个单词替换成一个字符集为\(k\)(\(k\leq9\))的字符串,使对于任 ...

  5. saltstack源码-启动1

    决定看salt的源码了.干脆就从最基本的看起来,先看它的启动过程开始第一步用/etc/init.d/salt-master start 启动找到那个文件,发现有3种启动方式,suse,debian,c ...

  6. MultipartFile(文件的上传)--CommonsMultipartResolver

    转自:https://www.cnblogs.com/896240130Master/p/6430908.html SpringMVC 中,文件的上传,是通过 MultipartResolver 实现 ...

  7. windows server 2003 修改远程链接端口

    服务器默认的远程链接的端口是3389,只能内网访问,外网不能访问,现映射了8400端口给服务器,内外网都可以访问,因此需要修改服务器的远程链接的端口. 运行中 输入:regedit 选择十进制,将33 ...

  8. UVa 12716 && UVaLive 6657 GCD XOR (数论)

    题意:给定一个 n ,让你求有多少对整数 (a, b) 1 <= b <= a 且 gcd(a, b) = a ^ b. 析:设 c = a ^ b 那么 c 就是 a 的约数,那么根据异 ...

  9. Ruby 数式匹配器

    str = "x^2 + 12317         +X^2 -       Length" str = "      x    ^  2  + y           ...

  10. bzoj 1951: [Sdoi2010]古代猪文 【中国剩余定理+欧拉定理+组合数学+卢卡斯定理】

    首先化简,题目要求的是 \[ G^{\sum_{i|n}C_{n}^{i}}\%p \] 对于乘方形式快速幂就行了,因为p是质数,所以可以用欧拉定理 \[ G^{\sum_{i|n}C_{n}^{i} ...