Codeforces 788 C. The Great Mixing
题目链接:http://codeforces.com/contest/788/problem/C
一看就不能暴力$DP$,我们可以将浓度的合并操看作为在追逐一高度,每次操作前这个高度都会向上走$n$,每次加入一定浓度的汽水就增加了相应的高度$a_i$,由于每次都是进行相同的转移,所以一旦高度差的绝对值大于了$1000$,这个状态就没有了意义(因为一定会有等价的状态比他更好),剪枝一下。所以每一次转移就是${O(n*min(k,1000))}$的。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<ctime>
#include<map>
using namespace std;
#define maxn 10010
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,k,a[maxn];
bool xian[maxn];
map<llg,bool>ma;
vector<llg>f,nf;
inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} int main()
{
// yyj("C");
cin>>n>>k;
for (llg i=;i<=k;i++)
{
llg x=getint();
if (xian[x]) continue;
xian[x]=;
a[++m]=x;
}
f.push_back();
for (llg tim=;;tim++)
{
if ((double)clock()/CLOCKS_PER_SEC>0.91) break;
ma.clear();
llg w=f.size(); nf.clear();
for (llg i=;i<w;i++)
{
llg val=f[i];
val-=n;
for (llg j=;j<=m;j++)
{
llg nval=val+a[j];
if (nval==)
{
cout<<tim;
return ;
}
if (nval> || nval<-) continue;
if (ma[nval]) continue;
ma[nval]=;
nf.push_back(nval);
}
}
f.clear();
w=nf.size();
for (llg i=;i<w;i++) f.push_back(nf[i]);
}
cout<<-;
return ;
}
Codeforces 788 C. The Great Mixing的更多相关文章
- 【Codeforces 788C】The Great Mixing
http://codeforces.com/contest/788/problem/C 显然如果有两杯一样的酒,把它们当作同一杯就好了.所以k<=1e6毫无意义. 若选的x杯酒的浓度分别为a,b ...
- Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)
[题目链接] http://codeforces.com/problemset/problem/788/C [题目大意] 给出一些浓度的饮料,要求调出n/1000浓度的饮料,问最少需要多少升饮料 [题 ...
- Codeforces 788C The Great Mixing
The Great Mixing 化简一下公式后发现, 问题变成了, 取最少多少数能使其和为1, bitset优化一下背包就好啦. 题解中介绍了一种bfs的方法没, 感觉比较巧妙. #include& ...
- Educational Codeforces Round 88 (Rated for Div. 2) C. Mixing Water(数学/二分)
题目链接:https://codeforces.com/contest/1359/problem/C 题意 热水温度为 $h$,冷水温度为 $c\ (c < h)$,依次轮流取等杯的热冷水,问二 ...
- Educational Codeforces Round 88 (Rated for Div. 2) B、New Theatre Square C、Mixing Water
题目链接:B.New Theatre Square 题意: 你要把所有"." 都变成"*",你可以有两个选择,第一种就是一次铺一个方块(1*1),第二种就是同一 ...
- Codeforces.788C.The Great Mixing(bitset DP / BFS)
题目链接 \(Description\) 有k种饮料,浓度Ai给出,求用最少的体积配成n/1000浓度的饮料. \(Solution\) 根据题意有方程 (A1x1+A2x2+...+Anxn)/[( ...
- 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 typ ...
- CodeForces 445B DZY Loves Chemistry
DZY Loves Chemistry Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64 ...
- CodeForces 445B. DZY Loves Chemistry(并查集)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/problemset/prob ...
随机推荐
- day14 十四、三元运算符,推导式,匿名内置函数
一.三元(目)运算符 1.就是if...else...语法糖 前提:if和else只有一条语句 # 原来的做法 cmd = input('cmd:>>>') if cmd.isdig ...
- vue设置初始对象时为空报错
解决办法:在初始化时提供完整的数据结构
- Xmodem协议简介
1. Xmodem协议 1.1. 简介 在上一章中,BootLoader和APP在串口下的升级其实都用到了一种文件传输协议,即Xmodem协议,该协议因其简单,易实现和使用的特点在很多 ...
- 万能poi导入功能模板
同时支持2007版本和2003版本,空行过滤,纯数字类型数据格式处理,日期格式处理等 package com.yss.db.util; import com.yss.base.common.excep ...
- 合作开发工具——freeze和pipreqs
以后在合作开发的过程中,难免会用到别人开发到一半或者将自己开发的项目交给别人,在转交项目的时候需要让别人知道本项目中用到了哪些模块,这时可以用到一条命令来帮助我们. pip3 freeze # 获取环 ...
- nginx安装lua模块实现高并发
nginx安装lua扩展模块 1.下载安装LuaJIT-2.0.4.tar.gz wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar ...
- cherry-pick时的add by us / both modified / delete by us /delete by themk
简单来说: us=into , them=from 比如你将test分支的某个提交cherry-pick到master分支上,那么us就是master分支,them 就是test分支 参考: http ...
- Cocos Creator两个类相互引用(调用)
如果两个类相互引用,脚本加载阶段就会出现循环引用,循环引用将导致脚本加载出错:///////////Game.jsvar Item = require("Item");var Ga ...
- Cocos Creator cc.Event点击触摸事件详解
cc.Event事件请不要直接创建 cc.Event 对象,因为它是一个抽象类,请创建 cc.Event.EventCustom 对象来进行派发. cc.Class({extends: cc.Comp ...
- Gitlab构建分布式版本控制系统
一 安装依赖 1.sudo yum install curl policycoreutils openssh-server openssh-clients 2.sudo systemctl enabl ...