当前位置:网站首页>[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;
}
边栏推荐
- 记某公司面试算法题:查找一个有序数组某个数字出现的次数
- What does BSP mean
- 机器学习--人口普查数据分析
- Learn winpwn (2) -- GS protection from scratch
- Leetcode 461 Hamming distance
- Codeforces Round #753 (Div. 3)
- Attention apply personal understanding to images
- Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
- 天梯赛练习集题解LV1(all)
- Knowledge Q & A based on Apache Jena
猜你喜欢

Asp access Shaoxing tourism graduation design website

AI benchmark V5 ranking

软件测试与质量学习笔记3--白盒测试

Why can't I use the @test annotation after introducing JUnit
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP

Software I2C based on Hal Library

MySQL and C language connection (vs2019 version)

引入了junit为什么还是用不了@Test注解

Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.

Neo4j installation tutorial
随机推荐
[蓝桥杯2020初赛] 平面切分
Did you forget to register or load this tag
基于apache-jena的知识问答
01 project demand analysis (ordering system)
L2-007 家庭房产 (25 分)
Test objects involved in safety test
vs2019 使用向导生成一个MFC应用程序
Record a problem of raspberry pie DNS resolution failure
安装numpy问题总结
QT creator custom build process
QT creator runs the Valgrind tool on external applications
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
学习问题1:127.0.0.1拒绝了我们的访问
Vs2019 first MFC Application
When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
Codeforces Round #771 (Div. 2)
neo4j安装教程
JDBC原理
ES6 let 和 const 命令
nodejs 详解