【Codeforces 788C】The Great Mixing
http://codeforces.com/contest/788/problem/C
显然如果有两杯一样的酒,把它们当作同一杯就好了。所以k<=1e6毫无意义。
若选的x杯酒的浓度分别为a,b,c,..,z,则由题意得(a+b+c+...+z)/x=n*x,整理可得(a-n)+(b-n)+(c-n)+...+(z-n)=0。
于是问题就转化成了从所有ai-n中选x个数使得和为0,求x的最小值。
然后由我也不知道怎么证明,可知(a-n),(b-n),(c-n),...,(k-n)可以通过调整顺序使任意前缀和均≥0且≤1000,所以BFS+剪枝即可。相当于在一张1001个结点的稠密无权图上跑最短路,所以时间复杂度为O(10012)。
#include <iostream>
#include <queue>
#define maxn 1005
using namespace std;
int n, m;
int dist[maxn];
bool exist[maxn];
queue<int> que;
int main()
{
ios::sync_with_stdio(false);
cin >> m >> n;
int tmp;
for (int i = ; i <= n; i++)
{
cin >> tmp;
exist[tmp] = true;
} que.push();
while (!que.empty())
{
int x = que.front();
que.pop();
for (int i = ; i <= ; i++)
{
int w = x + i - m;
if (exist[i] && w >= && w <= && !dist[w])
{
que.push(w);
dist[w] = dist[x] + ;
if (w == )
{
cout << dist[];
return ;
}
}
}
}
cout << -;
return ;
}
【Codeforces 788C】The Great Mixing的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- Fibonacci again and again(SG函数应用)
Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 关于php的命名空间
php定义命名空间要使用namespace关键字,例:namespace Database 使用命名空间中的类要使用use关键字,也可以在use后面加as给类取别名,例:use Database\SQ ...
- nova创建虚拟机源码分析系列之六 api入口create方法
openstack 版本:Newton 注:博文图片采用了很多大牛博客图片,仅作为总结学习,非商用.该图全面的说明了nova创建虚机的过程,从逻辑的角度清晰的描述了前端请求创建虚拟机之后发生的一系列反 ...
- ⑿bootstrap组件 缩略图 警告框 进度条 基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- ArrayBuffer and Base64
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- JS-类型转换
类型转换 值 转字符串 数字 布尔值 对象 undefined 'undefined' 0 false throws TypeError null 'null' 0 false throws Type ...
- JMeter基础教程3:脚本录制篇
对于一些JMeter初学者来说,录制脚本可能是最容易掌握的技能之一.虽然我不建议录制性能脚本(因为录制的脚本比较混乱,必须要通过二次处理才可正常使用),但有时做总比不做要好,是吧?下面我们详细介绍使用 ...
- 数组去重+indexOf()应用
说起数组去重大家都不陌生,去重也有好多种方法,这里介绍很好理解的两种. 第一种 首先说一下第一种的逻辑,就是先拿第一个去跟第二个比,再跟第三个比,再跟第四个比--只要发现有相等的,可以用splice( ...
- Python中的三种数据结构
Python中,有3种内建的数据结构:列表.元组和字典.1.列表 list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.列表中的项目.列表中的项目应该包括在方括号中,这 ...
- JavaWeb框架SSH_Struts2_(一)
1. Struts2 框架入门及结合Intellj idea完成登陆demo测试 1.1 本章目录: 框架入门 Struts2简介 Struts2入门案例 Struts2执行流程分析 2. 具体 ...