题目描述
杂货店出售一种由 N(3<=N<=12)种不同颜色的颜料,每种一瓶(50ML),
组成的颜料套装。你现在需要使用这 N 种颜料;不但如此,你还需要一定
数量的灰色颜料。杂货店从来不出售灰色颜料——也就是它不属于这 N 种
之一。幸运的是,灰色颜料是比较好配置的,如果你取出三种不同颜色的
颜料各 x ml,混合起来就可以得到 xml 的灰色颜料(注意不是 3x)。
现在,你知道每种颜料各需要多少 ml。你决定买尽可能少的“颜料套装”,
来满足你需要的这 N+1 种颜料。那么你最少需要买多少个套装呢?
输入描述
输入包含若干组测试数据。每组数据一行:第一个数 N, 3<=N<=12, 含
义如上;接下来 N+1 个数,分别表示你需要的 N+1 种颜料的毫升数。
最后一种是灰色。所有输入的毫升数<=1000.
注意:输入中不存在每个颜料套装的毫升数。由题意可知,每种各 50ml,
即一共 50N ml
输出描述
每组数据输出一行,最少需要的套装数。
样例输入
3 40 95 21 0
7 25 60 400 250 0 60 0 500
4 90 95 75 95 10
5 0 0 0 0 0 333
0
样例输出 2
8 2 4
数据范围及提示 对于 30%的数据 N=3
对于 100%的数据 3 <= N <= 100
数据最多不超过 10 组
【2017.10.10考试t1】
死活没看出来可以用优先队列
大概是个贪心的过程。先不考虑灰色,算出最低标准。然后用现有的量减掉每种颜色各自需要的量,就是每种颜色可以提供给灰色的量。具体思路在代码中。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=101;
int a[maxn],b[maxn];
int n;
priority_queue<int>q;
int main()
{
while(~scanf("%d",&n))
{
if(!n) break;
while(!q.empty()) q.pop();//将上一次剩余的数弹出
int maxx=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
maxx=max(maxx,a[i]);//最大值
}
scanf("%d",&a[n+1]);
int tot=maxx/50;//不考虑灰色需要的颜料套数
if(tot*50<maxx) tot++;//不能整除50
for(int i=1;i<=n;i++)
{
a[i]=tot*50-a[i];//每种颜料去掉自己需要的,可以用来配制灰色颜料的量
q.push(a[i]);//放入大根堆
}
while(a[n+1]>0)
{
int x=q.top();q.pop();
int y=q.top();q.pop();
int z=q.top();q.pop();
if(x<=0||y<=0||z<=0)//如果最大的三个数中有一个减没了
{
tot++;//再加一套
int cnt=0;
while(!q.empty())
{
cnt++;
b[cnt]=q.top();
q.pop();
}
x+=50,y+=50,z+=50;
for(int i=1;i<=cnt;i++)//cnt=n-3 x,y,z不包含在b数组内(上面已经弹出过了)
{
b[i]+=50;
q.push(b[i]);
}
q.push(x),q.push(y),q.push(z);
}
else
{
x--,y--,z--,a[n+1]--;
q.push(x),q.push(y),q.push(z);
}
}
printf("%d\n",tot);
}
return 0;
}
模拟一下也可以水过,,只不过考场上模拟失败,误打误撞拿了70,奇奇怪怪。
下面是学长的代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=15;
int a[maxn],b[maxn];
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int n,m,k;
while(scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
sort(a,a+n);
if(a[n-1]%50==0) k=a[n-1]/50;
else k=a[n-1]/50+1;
int t=0;
for(int i=0;i<n;i++)
b[i]=k*50-a[i];
while(t<m)
{
if(b[2]==0)
{
k+=1;
for(int i=0;i<n;i++)
b[i]+=50;
}
else
{
t++;
b[0]--,b[1]--,b[2]--;
sort(b,b+n,cmp);
}
}
cout<<k<<endl;
}
return 0;
}