当前位置:网站首页>[Bluebridge cup 2021 preliminary] weight weighing
[Bluebridge cup 2021 preliminary] weight weighing
2022-07-06 11:26:00 【%xiao Q】
subject
Title Description
You have a balance and N A weight , this N The weights of the weights are W1, W2, … , WN.
Please calculate how many different weights you can weigh ?
Note that the weight can be placed on both sides of the balance .
Input format
The first line of input contains an integer N.
The second line contains N It's an integer :W1, W2, W3, … , WN.
about 50% The evaluation case of ,1 ≤ N ≤ 15.
For all profiling use cases ,1 ≤ N ≤ 100,N The total weight of each weight does not exceed 100000.
Output format
Output an integer to represent the answer .
sample input
3
1 4 6
sample output
10
analysis
The violent search of this problem will definitely timeout ,dfs In general , Yes 50% The point of .
The positive solution of this problem is dp( Why can't vegetable chicken dp), Then let's analyze dp How to do it :
- State means f[i][j]: Before presentation i Choose one of the weights , The weight weighed is j The state of , by 1 It means you can weigh , by 0 Cannot weigh out .
- State shift :
- a[i] == j, that f[i][j] It must be called
- a[i] != j, Then you can go from f[i - 1][j],f[i - 1][abs(j - a[i])]( Essential for f[i - 1][j - a[i]], To prevent negative subscripts , So take the absolute value ),f[i - 1][j + a[i]] this 3 In this case, transfer .
f[i - 1][j]:i - 1 A weight can weigh ,i One can also weigh out
f[i - 1][j - a[i]]: Add the current weight
f[i - 1][j + a[i]]: Subtract the current weight
Satisfy 3 Any one of these , You can judge f[i][j] Weight of j Can it be called
Reference code 1(dfs)---- too 50% Example (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;
}
Reference code 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;
}
边栏推荐
- 打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
- How to build a new project for keil5mdk (with super detailed drawings)
- Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
- Base de données Advanced Learning Notes - - SQL statements
- [蓝桥杯2020初赛] 平面切分
- Codeforces Round #771 (Div. 2)
- AI benchmark V5 ranking
- Principes JDBC
- Aborted connection 1055898 to db:
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
猜你喜欢

Rhcsa certification exam exercise (configured on the first host)

Install mongdb tutorial and redis tutorial under Windows

Vs2019 desktop app quick start

How to build a new project for keil5mdk (with super detailed drawings)

Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
![[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
![[Blue Bridge Cup 2017 preliminary] grid division](/img/e9/e49556d0867840148a60ff4906f78e.png)
[Blue Bridge Cup 2017 preliminary] grid division

Pytorch基础

Neo4j installation tutorial

Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
随机推荐
MySQL与c语言连接(vs2019版)
jS数组+数组方法重构
使用lambda在循环中传参时,参数总为同一个值
Knowledge Q & A based on Apache Jena
快来走进JVM吧
软件测试与质量学习笔记3--白盒测试
保姆级出题教程
L2-007 家庭房产 (25 分)
Django运行报错:Error loading MySQLdb module解决方法
vs2019 使用向导生成一个MFC应用程序
Introduction to the easy copy module
About string immutability
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
解决安装Failed building wheel for pillow
Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.
库函数--(持续更新)
学习问题1:127.0.0.1拒绝了我们的访问
Solution to the practice set of ladder race LV1 (all)
When using lambda to pass parameters in a loop, the parameters are always the same value
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.