Codeforces 789E The Great Mixing (数推倒公式 + bfs + 剪枝)
来源:互联网 发布:淘宝买东西受骗怎么办 编辑:程序博客网 时间:2024/05/20 14:24
题意:有k个数,是1/1000的倍数,问能否选取任意个数,每个数也能选任意次,使他们的均值为n/1000
题解:假设选了m个数 (s1+s2+...sm)/1000/m=n/1000,化简后得到(s1-n)+(s2-n)+....+(sm-n)=0,原题转化成从k个数中选m个数使他们的和为0,且选出来的数的和的范围必然是在[-1000,1000],这样用bfs来写,就可以得到最少需要选几个数,O(2001*min(k,1001))
题目就转换成:有1个序列, 每个数字可以选多次,问最少取几个数字能组成0。。。。
那么就是一个简单的搜索题,注意一下剪枝就行啦~~~~~
Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th type is characterised by its carbon dioxide concentration . Today, on the party in honour of Sergiy of Vancouver they decided to prepare a glass of Coke with carbon dioxide concentration . The drink should also be tasty, so the glass can contain only integer number of liters of each Coke type (some types can be not presented in the glass). Also, they want to minimize the total volume of Coke in the glass.
Carbon dioxide concentration is defined as the volume of carbone dioxide in the Coke divided by the total volume of Coke. When you mix two Cokes, the volume of carbon dioxide sums up, and the total volume of Coke sums up as well.
Help them, find the minimal natural number of liters needed to create a glass with carbon dioxide concentration . Assume that the friends have unlimited amount of each Coke type.
The first line contains two integers n, k (0 ≤ n ≤ 1000, 1 ≤ k ≤ 106) — carbon dioxide concentration the friends want and the number of Coke types.
The second line contains k integers a1, a2, ..., ak (0 ≤ ai ≤ 1000) — carbon dioxide concentration of each type of Coke. Some Coke types can have same concentration.
Print the minimal natural number of liter needed to prepare a glass with carbon dioxide concentration , or -1 if it is impossible.
400 4100 300 450 500
2
50 2100 25
3
In the first sample case, we can achieve concentration using one liter of Coke of types and : .
In the second case, we can achieve concentration using two liters of type and one liter of type: .
#include<bits/stdc++.h>using namespace std;map<int,int>vis,vis1;const int N = 1e6 + 100;const int inf = 0x3f3f3f3f;struct P{ int x,step; P(int x,int step):x(x),step(step){} P(){}};int a[N];int bfs(){ queue<P>que; que.push(P(0,0)); int i,j; while(!que.empty()){ P now = que.front(); // cout<<now.x<<endl; que.pop(); for(i=-1000;i<=1000;i++) { if(vis[i]) { if(now.x>0 && i>0) continue; if(now.x<0 && i<0) continue; if(now.x+i==0) return now.step+1; if(vis1[now.x+i]) continue; vis1[now.x+i]=1; que.push(P(now.x+i,now.step+1)); } } }}int main(){ ios::sync_with_stdio(false); int n,k,i,Min,Max,zero; cin>>n>>k; Min=inf,Max=-inf,zero=0; for(i=1;i<=k;i++) { cin>>a[i]; a[i]-=n; if(a[i]==0) zero=1; vis[a[i]]=1; Min=min(Min,a[i]); Max=max(Max,a[i]); } if(zero) { cout<<"1"<<endl; } else if(Min*Max>0) { cout<<"-1"<<endl; } else { int ans = bfs(); cout<<ans<<endl; } return 0;}
- Codeforces 789E The Great Mixing (数推倒公式 + bfs + 剪枝)
- Codeforces 789E The Great Mixing【Bfs+dp】
- Codeforces Round #407 (Div. 2) E. The Great Mixing [bfs]
- (codeforces) The Great Mixing
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- BFS (图)——Codeforces 788 C. The Great Mixing
- codeforces 788C The Great Mixing( BFS / dp+bitset优化 )
- (CF 788)C. The Great Mixing <BFS>
- CF788C:The Great Mixing(背包bitset & bfs)
- [BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing
- Codeforces Round #407 (Div. 2)-E-The Great Mixing-滚动数组或者dfs
- The Great Mixing
- codeforces788C The Great Mixing
- CF 788C(The Great Mixing-背包)
- Codeforces Round #294 (Div.2) E Shaass the Great
- 【树形DP】 CodeForces 294E Shaass the Great
- codeforces 294E Shaass the Great (树形dp,好题)
- 有一个二维数组. 数组的每行从左到右是递增的,每列从上到下是递增的. 在这样的数组中查找一个数字是否存在。 时间复杂度小于O(N)
- Android on the transplant ghostscript-9.04 static compiler
- 单例模式实现:枚举单例
- C Datastructure 5 ---- stack
- 剑指offer 求1+2+3+...+n
- Codeforces 789E The Great Mixing (数推倒公式 + bfs + 剪枝)
- fortify——J2EE Bad Practices: Non-Serializable Object Stored in Session
- 在Android Studio中进行单元测试和UI测试
- $.parser.setParserOptions()的实现
- 深度增强学习:走向通用人工智能之路
- 1、uboot的目录分析
- 从GitHub上下载单个文件
- KNN(K-Nearest Neighbor)算法Matlab实现
- ord()和chr()对中文字符的应用