ACM 扫描法 Wine trading in Gergovia
2017-07-23 16:47
260 查看
滴,集训第七天打卡。
今天是紫书第八章训练,是高效算法设计..
就是不用奇技淫巧都会超时...
这里贴一题扫描法。
UVA 11054 Wine trading in Gergovia
![](https://img-blog.csdn.net/20170723163805273)
![](https://img-blog.csdn.net/20170723163810483)
题目大意:直线上有n个等距的村庄,每个村庄要么买酒(ai>0),要么卖酒(ai<0),所有村庄供需平衡,即所有ai之和等于0.把k个单位的酒从一个村庄运到相邻村庄需要k个单元的劳动力。计算最少需要多少劳动力可以满足所有村庄的需求。
思路:用扫描法,从最左边的村庄开始。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
long long ans,a,last;
while(scanf("%d",&n))
{
if(n==0)break;
ans=last=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a);
ans+=abs(last);
last+=a;
}
printf("%lld\n",ans);
}
}
今天是紫书第八章训练,是高效算法设计..
就是不用奇技淫巧都会超时...
这里贴一题扫描法。
UVA 11054 Wine trading in Gergovia
题目大意:直线上有n个等距的村庄,每个村庄要么买酒(ai>0),要么卖酒(ai<0),所有村庄供需平衡,即所有ai之和等于0.把k个单位的酒从一个村庄运到相邻村庄需要k个单元的劳动力。计算最少需要多少劳动力可以满足所有村庄的需求。
思路:用扫描法,从最左边的村庄开始。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i;
long long ans,a,last;
while(scanf("%d",&n))
{
if(n==0)break;
ans=last=0;
for(i=0;i<n;i++)
{
scanf("%lld",&a);
ans+=abs(last);
last+=a;
}
printf("%lld\n",ans);
}
}
相关文章推荐
- UVA - 11054 Wine trading in Gergovia 扫描法
- UVa 11054 Wine trading in Gergovia(扫描)
- UVA 11054 Wine trading in Gergovia(扫描)
- poj 2940 Wine Trading in Gergovia 贪心+扫描
- UVa 11054 - Wine trading in Gergovia(扫描法 水题)
- POJ 2940 Wine Trading in Gergovia(简单贪心)
- uva 11054 wine trading in gergovia (归纳【好吧这是我自己起的名字】)——yhx
- [等价转换]UVa11054 - Wine trading in Gergovia
- 例题8-5 Gergovia的酒交易(Wine trading in Gergovia, UVa 11054)
- poj 2940 Wine Trading in Gergovia
- uva - 11054 - Wine trading in Gergovia(贪心)
- UVA 11054 Wine trading in Gergovia(思维)
- UVa 11054 - Wine trading in Gergovia
- UVA 11504--Wine trading in Gergovia(思维题)
- UVa 11054 - Wine trading in Gergovia
- uva 11054 Wine trading in Gergovia(贪心)
- 【每日一题(11)】Wine Trading in Gergovia POJ - 2940
- 11054 - Wine trading in Gergovia
- uva 11054 - Wine trading in Gergovia
- UVa11054 - Wine trading in Gergovia UVA