P1441 砝码称重(搜索+队列dp)
题目链接:传送门
题目大意:
给你n个砝码ai,从中去掉m个后求最多的砝码可表示的重量。
n≤20,m≤4,m<n,ai≤100。
思路:
用dfs搜掉m个砝码,然后用队列dp跑出答案,维护答案。
时间复杂度是O(Cnm * na)。
#include <bits/stdc++.h> using namespace std; int N, M, sum;
int a[];
bool vis[];
bool f[]; int ans; void dp()
{
queue <int> Q;
Q.push();
for (int i = ; i < N; i++) if (!vis[i]) {
int len = Q.size();
while (len--) {
int cur = Q.front(); Q.pop();
int nxt = cur + a[i];
if (!f[nxt]) {
f[nxt] = true;
Q.push(nxt);
}
Q.push(cur);
}
}
int cnt = ;
for (int i = ; i <= sum; i++) {
if (f[i]) {
cnt++;
f[i] = false;
}
}
ans = max(cnt, ans);
} void dfs(int dep, int cur)
{
if (dep == M) {
dp();
}
for (int i = cur; i < N; i++) {
if (!vis[i]) {
vis[i] = true;
sum -= a[i];
dfs(dep+, i+);
sum += a[i];
vis[i] = false;
}
}
} void init()
{
cin >> N >> M;
sum = ;
ans = ;
memset(f, false, sizeof f);
memset(vis, false, sizeof vis);
for (int i = ; i < N; i++) {
scanf("%d", a+i);
sum += a[i];
}
sort(a, a+N);
} int main()
{
init();
dfs(, );
cout << ans << endl;
return ;
}
P1441 砝码称重(搜索+队列dp)的更多相关文章
- [P1441]砝码称重 (搜索+DP)
对于我这种蒟蒻,是很不错的一题了. dfs搜索当前状态 满足时DP 比较坑的地方就是起始的地方 我一开始从1开始,搜索写的是从0开始. 后来就统一用0开始的了. #include<bits/st ...
- P1441 砝码称重 DFS回溯+DP
题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 请注意,砝码只能放在其中一边. 输入输出格式 输入格式: 输入文件weig ...
- 洛谷P1441 砝码称重(搜索,dfs+dp)
洛谷P1441 砝码称重 \(n\) 的范围为 \(n \le 20\) ,\(m\) 的范围为 \(m \le 4\) . 暴力遍历每一种砝码去除情况,共有 \(n^m\) 种情况. 对于剩余砝码求 ...
- 洛谷P1441 砝码称重(搜索,dfs+bitset优化)
洛谷P1441 砝码称重 \(n\) 的范围为 \(n \le 20\) ,\(m\) 的范围为 \(m \le 4\) . 暴力遍历每一种砝码去除情况,共有 \(n^m\) 种情况. 对于剩余砝码求 ...
- 洛谷P1441 砝码称重
P1441 砝码称重 题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 输入输出格式 输入格式: 输入文件weight.in ...
- 7行代码解决P1441砝码称重(附优化过程)
先贴上最终代码感受一下: #include <bits/stdc++.h> using namespace std; int i, N, M, wi[21], res = 0; int m ...
- 洛谷 P1441 砝码称重
题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 输入输出格式 输入格式: 输入文件weight.in的第1行为有两个整数n ...
- P1441 砝码称重
题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 输入输出格式 输入格式: 输入文件weight.in的第1行为有两个整数n ...
- [Luogu] P1441 砝码称重
题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 题目分析 因为读错题WAWA大哭. 先dfs枚举选的砝码,满足条件时进行d ...
随机推荐
- dynamic load jar and init spring
public class SpringLoader { private Map<String, Class<?>> classMap = new HashMap<> ...
- 微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
几天不动代码,再运行成这样了, {errMsg: "getLocation:fail Error: tunneling socket could not…d, cause=connect E ...
- 把旧系统迁移到.Net Core 2.0 日记(11) -- Authentication 认证 claimsIdentity 对比 之前的FormAuthentication
实现最简单的认证,类似之前的FormAuthentication 在 Startup 的 ConfigureServices() 方法中添加 Authentication 的配置: 这个CookieA ...
- css3 min-content,max-content,fit-content, fill属性
css3里有四个属性,用来实现以内容为主的尺寸计算方式,intrinsic sizing min-content max-content fit-content fill 其中 fill 关键字,需要 ...
- jquery ready&&load用法
ready和load那一个先执行 DOM文档加载的步骤 (1) 解析HTML结构 (2) 加载外部脚本和样式表文件 (3) 解析并执行脚本代码 (4) 构造HTML DOM模型 //ready (5) ...
- Instruments leak黑魔法定位内存泄漏
leak是一款很赞的内存检查的工具,但在使用的过程中有点繁琐,至少有些底层的泄漏笔者还是不知道如何下手 下面介绍一下简单leak的使用: 首先你要确认你的target不会被拒绝,确保profile是d ...
- C# 表达式树学习笔记
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 十. Python基础(10)--装饰器
十. Python基础(10)--装饰器 1 ● 装饰器 A decorator is a function that take a function as an argument and retur ...
- capjoint conversations with Chenweiwen
This event is quite small for teleseismic stations, which means it will be more strongly affected by ...
- kbmMWUnidac直接SQLServer
UniDAC支持SQLServer直联了,当时就测试过在kbmMW中用直联方式,结果不尽人意,kbmMWServer在执行sql时会出地址错误,就一直没有进一步测试.今天听xalion说,是因为当直联 ...