当前位置:网站首页>[蓝桥杯2021初赛] 砝码称重
[蓝桥杯2021初赛] 砝码称重
2022-07-06 09:14:00 【%xiao Q】
题目
题目描述
你有一架天平和N 个砝码,这N 个砝码重量依次是W1, W2, … , WN。
请你计算一共可以称出多少种不同的重量?
注意砝码可以放在天平两边。
输入格式
输入的第一行包含一个整数N。
第二行包含N 个整数:W1, W2, W3, … , WN。
对于50% 的评测用例,1 ≤ N ≤ 15。
对于所有评测用例,1 ≤ N ≤ 100,N 个砝码总重不超过100000。
输出格式
输出一个整数代表答案。
输入样例
3
1 4 6
输出样例
10
分析
这道题暴力搜索肯定是会超时的,dfs大概的话,能过50%的点。
这题的正解为dp(奈何菜鸡不会dp),那么接下来就来分析dp的做法:
- 状态表示f[i][j]:表示前i个砝码中选,称出的重量为j的状态,为1表示可以称出,为0则不能称出。
- 状态转移:
- a[i] == j,那么f[i][j]一定可以被称出
- a[i] != j,那么就可以从f[i - 1][j],f[i - 1][abs(j - a[i])](本质为f[i - 1][j - a[i]],为了防止出现负的下标,所以取绝对值),f[i - 1][j + a[i]]这3种情况中转移过来。
f[i - 1][j]:i - 1个砝码能称出的,i个也能称出
f[i - 1][j - a[i]]:加上当前的砝码
f[i - 1][j + a[i]]:减去当前的砝码
满足3种的任意一种,即可判断f[i][j]的重量为j的能否被称出
参考代码1(dfs)---- 过50%的样例(O(3^n))
#include <iostream>
#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 110, M = 1e5 + 10;
int n, ans = 0;
int a[N];
bool st[M];
void dfs(int u, int sum)
{
if(!st[sum] && sum > 0) ans++, st[sum] = true;
if(u == n + 1) return ;
dfs(u + 1, sum + a[u]);
dfs(u + 1, sum - a[u]);
dfs(u + 1, sum);
}
int main()
{
cin >> n;
rep(i, 1, n) cin >> a[i];
dfs(1, 0);
cout << ans << endl;
return 0;
}
参考代码2(dp)---- O(nm)
#include <iostream>
#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 110, M = 1e5 + 10;
int n, m;
int a[N];
int f[N][M];
int main()
{
cin >> n;
rep(i, 1, n) cin >> a[i], m += a[i];
rep(i, 1, n)
rep(j, 1, m)
{
if(a[i] == j) f[i][j] = 1;
else f[i][j] = f[i - 1][j] | f[i - 1][abs(j - a[i])] | f[i - 1][j + a[i]];
}
int ans = 0;
rep(i, 1, m)
if(f[n][i]) ans++;
cout << ans << endl;
return 0;
}
边栏推荐
- Ansible实战系列一 _ 入门
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
- Ubuntu 20.04 安装 MySQL
- npm一个错误 npm ERR code ENOENT npm ERR syscall open
- Install mysql5.5 and mysql8.0 under windows at the same time
- Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
- Armv8-a programming guide MMU (2)
- JDBC principle
- MySQL主从复制、读写分离
- Summary of numpy installation problems
猜你喜欢

Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution

连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
![[C language foundation] 04 judgment and circulation](/img/59/4100971f15a1a9bf3527cbe181d868.jpg)
[C language foundation] 04 judgment and circulation

Postman environment variable settings

error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead

Install mongdb tutorial and redis tutorial under Windows
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP

QT creator support platform
![[download app for free]ineukernel OCR image data recognition and acquisition principle and product application](/img/1b/ed39a8b9181660809a081798eb8a24.jpg)
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application

Postman Interface Association
随机推荐
QT creator uses Valgrind code analysis tool
Did you forget to register or load this tag 报错解决方法
npm一个错误 npm ERR code ENOENT npm ERR syscall open
Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.
Number game
[recommended by bloggers] background management system of SSM framework (with source code)
neo4j安装教程
Django running error: error loading mysqldb module solution
Learning question 1:127.0.0.1 refused our visit
Machine learning -- census data analysis
MySQL完全卸载(Windows、Mac、Linux)
Postman Interface Association
Postman uses scripts to modify the values of environment variables
Did you forget to register or load this tag
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
MySQL主從複制、讀寫分離
Postman environment variable settings
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks