[BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing
来源:互联网 发布:西安航空学院网络教育 编辑:程序博客网 时间:2024/05/20 14:20
题意
戳这里
题解
就是给出一些数,求取最少的数使得平均数等于m。每种数无限个。
用平均数的套路,吧所有数减m,题面转换成取最少的数使得加和为
然后怎么做呢?考虑答案有多大,可以发现我们随便选
我们还可以发现,对于任意某个答案
这样分析之后就除去了很多没用的东西,就可以直接暴力
时间复杂度
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int maxn=2005,maxe=4000005;int n,m,a[1000005];int fir[maxn],nxt[maxe],son[maxe],tot;void add(int x,int y){ son[++tot]=y; nxt[tot]=fir[x]; fir[x]=tot;}queue<int> que;int d[maxn];int main(){ //freopen("cf788C.in","r",stdin); //freopen("cf788C.out","w",stdout); scanf("%d%d",&m,&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]), a[i]-=m; sort(a+1,a+1+n); n=unique(a+1,a+1+n)-(a+1); for(int i=0;i<=2000;i++) for(int j=1;j<=n;j++) if(0<=i+a[j]&&i+a[j]<=2000) add(i,i+a[j]); memset(d,63,sizeof(d)); int INF=d[0]; que.push(1000); d[1000]=0; while(!que.empty()){ int x=que.front(); que.pop(); for(int j=fir[x];j;j=nxt[j]){ if(son[j]==1000) return printf("%d\n",d[x]+1),0; if(d[son[j]]==INF) d[son[j]]=d[x]+1, que.push(son[j]); } } printf("-1\n"); return 0;}
阅读全文
0 0
- Codeforces Round #407 (Div. 1) C. The Great Mixing(bfs)
- [BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing
- Codeforces Round #407 (Div. 2) E. The Great Mixing [bfs]
- Codeforces Round #407 (Div. 2)-E-The Great Mixing-滚动数组或者dfs
- 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>
- (codeforces) The Great Mixing
- Codeforces 789E The Great Mixing【Bfs+dp】
- CF 788C(The Great Mixing-背包)
- Codeforces 789E The Great Mixing (数推倒公式 + bfs + 剪枝)
- Codeforces Round #294 (Div.2) E Shaass the Great
- Codeforces Round #153 (Div. 1) C Number Transformation bfs
- CF788C:The Great Mixing(背包bitset & bfs)
- The Great Mixing
- codeforces788C The Great Mixing
- CodeForces-540C//Codeforces Round #301 (Div. 2)C BFS
- tomcat优化
- The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the argum
- 1001. A + B Format(20)
- JavaIO流原理之常用字节流和字符流详解以及Buffered高效的原理
- hff如何开启windows远程
- [BFS] Codeforces Round #407 (Div. 1) 788C. The Great Mixing
- ApplicationThread 代理启动
- VS中常用C#代码段快速输入
- golang常见的几种并发模型框架
- 【PAT】【Advanced Level】1078. Hashing (25)
- uva11248isap模板(超级版)
- apache web server-性能测试和负载均衡
- 滴滴笔试:最大连续子数组
- ODI(Oracle Data Integrator)基本使用教程(1)