【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 ...
随机推荐
- 【转】java事件监听机制
java中的事件机制的参与者有3种角色: 1.event object:事件状态对象,用于listener的相应的方法之中作为参数,一般存在与listerner的方法之中 2.event source ...
- Problem G
Problem Description A relay is a race for two or more teams of runners. Each member of a team runs o ...
- Eclipse 枚举类报错
当你写一个枚举类时,如果没有在枚举类的第一行写有哪些枚举值,那么就会出现编译报错:Syntax error on token "String", strictfp expected ...
- 0_Simple__simpleCallback
学习回调函数的基本概念,并在CUDA的任务流中插入基于CPU的主机函数,作为回调函数使用. ▶ 源代码:没有用到的部分被注释起来了 /*multithreading.h*/ #ifndef MULTI ...
- Hacker Rank: Kingdom Division 不完全报告
原题链接: Kingdom Division 由于树的层次可能很深,所以这里不能使用递归版的DFS.我使用了BFS. BFS确定各结点的父结点和它的孩子数. 用逆拓扑排序确定结点的计算顺序. same ...
- Android 开发笔记___复选框__checkbox
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- css中单位px和em,rem的区别[转载]
PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位: 3. Firefox能够调整px和em,rem,但是96%以上 ...
- ⑨bootstrap组件 按钮式下拉菜单 输入框 使用基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- 一起写框架-Ioc内核容器的实现-基础功能-ComponentScan支持多包扫描(六)
实现功能 1.我们看到@ComponentScan注解一个开始定义就是需要支持,扫描多个包,将多个包的类名获取到.现在就实现这个功能. 实现思路 根据传入的字符串数组,获得多个包下的类全限制名. 实现 ...
- Spring的IOC容器第一辑
一.Spring的IOC容器概述 Spring的IOC的过程也被称为依赖注入(DI),那么对象可以通过构造函数参数,工厂方法的参数或在工厂方法构造或返回的对象实例上设置的属性来定义它们的依赖关系,然后 ...