Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)

【题目链接】 http://codeforces.com/problemset/problem/788/C

【题目大意】

  给出一些浓度的饮料,要求调出n/1000浓度的饮料,问最少需要多少升饮料

【题解】

  设浓度为a,现在要求出系数x1,x2,x3……,使得x1*a1+x2*a2+x3*a3+……=n*(x1+x2+x3+……)
  得a1*(x1-n)+a2*(x2-n)+a3*(x3-n)+……=0
  假设现在有x1-n和x2-n,设其数值为x和y,那么一定有(x)*y+(-y)*x=0,
  x+y=x-(-y)<1000,更多物品的答案不会超过两个物品,所以答案最多1000,
  那么现在就是背包问题容量0是否能被覆盖到,因为状态只有两种,是否被覆盖,
  所以我们可以用bitset优化。

  同时,我们发现这个最小答案相当于求最短路的过程,每次可以有一定的步长,
  问到达目标状态的最小值,所以可以将每种物品作为步长,用bfs来解决背包问题。

【代码】

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <vector>
using namespace std;
typedef long long LL;
int x,n,k;
int main(){
	  scanf("%d%d",&n,&k);
	  vector<bool> vis(1001,0);
	  for(int i=1;i<=k;i++){
	      scanf("%d",&x);
	      vis[x]=1;
	  }vector<int> v;
	  for(int i=0;i<=1000;i++)if(vis[i])v.push_back(i-n);
	  bool flag=0;
	  bitset<2010>cur,nxt;
	  cur[1000]=1;
	  for(int i=1;i<=1000&&!flag;i++){
	      nxt.reset();
	      for(int x=0;x<v.size();x++){
	          if(v[x]>0)nxt|=cur<<v[x];
	          else nxt|=cur>>-v[x];
	      }swap(cur,nxt);
	      if(cur[1000]){flag=1;printf("%d\n",i);}
	  }if(!flag)puts("-1");
    return 0;
}
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
int n,k,x;
int main(){
    scanf("%d%d",&n,&k);
    vector<bool> vis(1001,0);
    for(int i=1;i<=k;i++){
        scanf("%d",&x);
        vis[x]=1;
    }vector<int> vs;
    for(int i=0;i<=1000;i++)if(vis[i])vs.push_back(i-n);
    vector<int> dp(2010,-1),q;
    for(int i=0;i<vs.size();i++){
        int u=vs[i]+1000;
        dp[u]=1;
        q.push_back(u);
    }
    for(int i=0;i<q.size();i++){
        int u=q[i];
        for(int x=0;x<vs.size();x++){
            int v=u+vs[x];
            if(v>=0&&v<=2000&&dp[v]<0){
                dp[v]=dp[u]+1;
                q.push_back(v);
            }
        }
    }printf("%d\n",dp[1000]);
    return 0;
}

  

时间: 2024-07-29 01:29:40

Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)的相关文章

hdu 5036 Explosion bitset优化floyd

http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么1能联通的顶点就可以直接走过去,其他不和1连通的,就需要炸坏.问需要炸弹的期望. 比如一副图是1-->2-->3的.那么期望是11 / 6 假如从1号点开始,1/3概率选中1号点开始,那么需要炸弹数是1(炸开一号),贡献是1/3 假如从2号点开始,1/3概率选中2号点开始,那么需要炸开2号点,然后

hdu 5036 Explosion (bitset优化的传递闭包求解概率)

Explosion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 142    Accepted Submission(s): 25 Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a g

XJOI NOIP2015模拟赛Day1 T2 ctps bitset优化 或 排序+cdq分治+树状数组+平衡树

题意: 4维空间中有1个点集A,|A|=n,用(a,b,c,d)表示每个点. 共有m个询问,每次询问输入一个点(a,b,c,d),求最大的S,其中S={p|p∈A且ap<=a,bp<=b,cp<=c,dp<=d},输出|S| 输入格式: 第一行n 接下来n行有n个4维点对 第n+2行有一个数m 再接下来m行每行有一个四维点对,表示每个询问 输出格式: 对于每个询问输出一个数 **方法:**bitset优化 或 排序+cdq分治+树状数组+平衡树 解析: 神题,考场不会,暴力骗40,

hdu5313Bipartite Graph(二分图染色+DP(bitset优化))

题意:给n个点m条边,问最多可以添加几条边使图为完全二分图 分析:如果二分图没有限制,看到是两边分别为n/2个点和n-n/2个点的最优但是可   能出现大于此点的情况,比如n=4,m=3,边为1 2,1 3,1 4.此时完全二分图边最多为3,所以要求得二分图左边或者右边可达到的离n/2最近的点数是多少为最优解,于是采用染色分别求出各个联通快的2种颜色的各个点数,然后用推出能达到的点数,这里dp[i]的下一个状态是dp[i+1]+d[i+1][0]和dp[i+1][1](dp[i]为前i个二分图的

HDU5890:Eighty seven(Bitset优化背包)

Mr. Fib is a mathematics teacher of a primary school. In the next lesson, he is planning to teach children how to add numbers up. Before the class, he will prepare NN cards with numbers. The number on the ii-th card is aiai. In class, each turn he wi

HDU - 6268: Master of Subgraph (分治+bitset优化背包)

题意:T组样例,给次给出一个N节点的点权树,以及M,问连通块的点权和sum的情况,输出sum=1到M,用0或者1表示. 思路:背包,N^2,由于是无向的连通块,所以可以用分治优化到NlgN. 然后背包可以用bitset优化.注意不要想着背包合并背包,背包只能合并单点. #include<bits/stdc++.h> #define pb push_back #define rep(i,a,b) for(int i=a;i<=b;i++) #define Gv G[u][i] #defin

01二维背包+bitset优化——hdu5890

口胡一种别的解法: 三重退背包,g1[j]k]表示不选x的选了j件物品,体积为k的方案数,g[0][0] = 1 , g1[j][k]=dp[j][k]-g1[j-1][k-a[x]] 然后按这样再退三层,最后看g3[10][87]的方案数是否非0即可,这样复杂度是O(50*50*50*10*87) 如果直接枚举删掉的数,然后用可行性二维01背包做 复杂度是O(50*50*50)*O(n*10*87) 再加上bitset优化第二维 复杂度/32 ,由于n比较小,所以也差不多 /* 在n个数里找到

Codeforces 789e The Great Mixing (bitset dp 数学)

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

CodeForces - 963D:Frequency of String (bitset暴力搞)

You are given a string ss. You should answer nn queries. The ii-th query consists of integer kiki and string mimi. The answer for this query is the minimum length of such a string tt that tt is a substring of ss and mimi has at least kiki occurrences