poj3253Fence Repair 解题题解

原创 2015年07月09日 22:22:01
Fence Repair
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30807   Accepted: 9947

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of the N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer N, the number of planks 
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8. 
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
#include<stdio.h>
#include<stdlib.h>
void Insert(int *a,int len,int Count)
{
	int i,small,count;
	count = Count;
	a[count] = len;
	while(count != 0)
	{
		i = (count-1)/2;
		if(a[count] < a[i])
		{
			small = a[count];
			a[count] = a[i];
			a[i] = small;
			count = i;
		}
		else
			break;
	}
}

int Deletemin(int *a,int Count)
{
	int count = Count;
	int i = 0,bigger,j,k,t;
	t = a[0];
	a[0] = a[count --];
	while((2*i+1) <= count)
	{
		j = 2 * i + 1;
		if(a[j+1] < a[j]&&j < count ) 
			j = j+1;
		if(a[i] > a[j])
		{
			bigger = a[i];
			a[i] = a[j];
			a[j] = bigger;
			i = j;
		}
		else
			break;
	}
	return t;
}
int main(void)
{
	int N,l,i = 0,count = 0;
	long long S = 0,sum = 0;
	int len[20010];
	scanf("%d",&N);
	while(count < N)
	{
		scanf("%d",&l);
		Insert(len,l,count);
		count ++;
		S += l;
	}
	count --;
	while(count > 1)
	{
		sum = Deletemin(len,count);
		count --;
		sum += Deletemin(len,count);
		Insert(len,sum,count);
		S += sum;
	}
	printf("%lld\n",S);

	return 0;
}


noi2017解题报告(部分)

D1T1: 每30位压成一个int,然后压位线段树就可以过了。#include #define gc getchar() #define N 4500009 #define mid (l+r>>1...
  • wzf_2000
  • wzf_2000
  • 2017年08月07日 09:28
  • 853

POJ3253Fence Repair【贪心+优先队列 】

Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissio...
  • R1986799047
  • R1986799047
  • 2015年08月01日 16:50
  • 363

noi2016解题报告

D1T1: 首先转化成统计AA型字符串有几种。 st[i]表示从i位置开始的AA型字符串有几个,ed[i]表示到i结束的有几个。 ans=∑st[i]*ed[i-1] 然后枚举A的长度L(AA...
  • wzf_2000
  • wzf_2000
  • 2017年05月21日 19:38
  • 469

NOIP2016提高组复赛 解题报告

Day1T1按照题意模拟就行了,水。 代码:#include #include #include using namespace std; #define N 100005int n,m,d,num...
  • Clove_unique
  • Clove_unique
  • 2016年12月01日 21:52
  • 7415

COGS 260. [NOI2002] 银河英雄传说 解题报告

260. [NOI2002] 银河英雄传说 【问题描述】     公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓...
  • Janis_z
  • Janis_z
  • 2016年09月28日 11:21
  • 489

NOIP2017提高组 题解&游记

Day0嗯,虽然成绩还没有出,但因为有山寨数据和成绩的问题嘛,我也大概知道什么情况了。就写写酱油记吧。 因为学校体艺节正在举办,不用上课,我就把所有的时间都放在机房了。当天早上,一直在打模板,什么会的...
  • Fgaoshihao
  • Fgaoshihao
  • 2017年11月15日 13:59
  • 1634

IDF实验室CTF题解——WEB(更新中,已完成5/7)

源IDF实验室CTF训练营-WEB题解1 一种编码而已老题,复制粘贴到console里运行,其实是js代码。 key:WCTF{H3110_J0t4er}2 你关注最新的漏洞吗下载发现是一个数据包,...
  • cd_xuyue
  • cd_xuyue
  • 2015年09月19日 12:39
  • 1944

NOIp2015提高组 解题报告

比赛几个星期前就结束了,玩乐了一会儿,开始学术。 此文非题解。只是我自己的现场解题实录。 Day_0 到宾馆后紧张的要死。晚上写了一堆基础模板:spfa最短路径,prim和kruskal的最...
  • jr_mz
  • jr_mz
  • 2015年12月03日 18:34
  • 3803

(贪心(也可用优先队列))poj3253Fence Repair

Description Farmer John wants to repair a small length of the fence around the pasture. He measures...
  • qq_34624763
  • qq_34624763
  • 2016年10月23日 15:53
  • 113

noip2013 简要题解

day1  P1 ans = (x + m *(10^k )) %n   10^k %n 用快速幂计算即可 #include using namespace std; long long n,m,k...
  • KQZXCMH
  • KQZXCMH
  • 2013年12月04日 22:40
  • 2899
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj3253Fence Repair 解题题解
举报原因:
原因补充:

(最多只允许输入30个字)