think:
1 今天AC了人生中第一道英文Oj题目,内心也不知如何描述,一开始提交发现wrong answer, 后来借鉴了鑫哥的代码,发现虽然和鑫哥用的方法不同,但注意到了鑫哥对于sum(累加切木头的花费)用的是long long 类型,自己也试了试,发现提交AC了,自己在百度查了查int类型的范围,发现自己之前理解有所偏差,之前理解的16位是指2^16,而不是十进制的16位,从百度上查了查,将查到的附在下面:
1>
nsigned int 0~4294967295
int -2147483648~2147483647
unsigned long 0~4294967295
long -2147483648~2147483647
long long的最大值:9223372036854775807
long long的最小值:-9223372036854775808
unsigned long long的最大值:1844674407370955161
__int64的最大值:9223372036854775807
__int64的最小值:-9223372036854775808
unsigned __int64的最大值:18446744073709551615
2>
int数据类型的取值范围是多少?怎么计算的?
百度了一下,有说最大是32767,有说最大是2147483647,这些都是如何计算的?为何得到不同的结果?和开发环境有关吗?
32位无符号整数 , 其表示范围是2的32次方,最大整数为 2的32次方-1
有符号数则要去除一个符号位,正数最大为2的31次方-1 , 负数最小为负 2的31次方
16位整数同理。
int 在32位系统中为 4字节,也就是32位。在一些16位系统中,int 为2字节,在64位系统中int为8字节
2回归题目,这个题目自己一开始可能还是不自信吧,其实虽然开始时候没有读懂题目,但是题意运气猜对了,哈夫曼树。和树-堆结构练习——合并果子之哈夫曼树这个题目基本一样,只是sum的数据类型得用long long.
3 英文题目可能又是我所遇到的一个阶段性的大挑战吧,估计的用相当长的时间来锻炼自己英文阅读理解能力,哈哈,加油吧,迎接挑战太能赢得未来!
Fence Repair
Time Limit: 2000MS Memory Limit: 65536KB
Problem 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
Example Input
3
8
5
8
Example Output
34
Hint
Author
以下为accepted代码
#include <stdio.h>
#include <stdlib.h>
#define MINDATA -1
typedef struct node
{
int *Data;
int Size;
} MinHeap;
MinHeap * Creat(int MaxSize)//创建最小堆
{
MinHeap *H = (MinHeap *)malloc(sizeof(MinHeap));
H->Data = (int *)malloc((MaxSize + 1)*sizeof(int));
H->Size = 0;
H->Data[0] = MINDATA;
return H;
}
void Insert(MinHeap *H, int x)//最小堆的元素插入
{
int i = ++H->Size;
for(; H->Data[i/2] >= x; i /= 2)
H->Data[i] = H->Data[i/2];
H->Data[i] = x;
}
int Delete(MinHeap *H)//最小堆的删除操作
{
int MinItem, X, parent, child;
MinItem = H->Data[1];
X = H->Data[H->Size--];
for(parent = 1; parent*2 <= H->Size; parent = child)
{
child = parent*2;
if((child != H->Size) && H->Data[child] > H->Data[child+1])
child++;
if(X <= H->Data[child])
break;
else
H->Data[parent] = H->Data[child];
}
H->Data[parent] = X;
return MinItem;
}
void PercDown(MinHeap *H, int p)//最小堆宏观调整的细节操作
{
int X, parent, child;
X = H->Data[p];
for(parent = p; parent*2 <= H->Size; parent = child)
{
child = parent*2;
if((child != H->Size) && H->Data[child] > H->Data[child+1])
child++;
if(X <= H->Data[child])
break;
else
H->Data[parent] = H->Data[child];
}
H->Data[parent] = X;
}
void GetBuild(MinHeap *H)//最小堆的宏观调整
{
for(int i = H->Size/2; i > 0; i--)
PercDown(H, i);
}
int main()
{
int n, i, x1, x2, y;
long long sum;
MinHeap *H1;
while(scanf("%d", &n) != EOF)
{
sum = 0;
H1 = Creat(n);//建树
H1->Size = n;///注意H1->Size的值
for(i = 1; i <= n; i++)
{
scanf("%d", &H1->Data[i]);
}
GetBuild(H1);//有序化调整
for(i = 0; i < n-1; i++)
{
x1 = Delete(H1);//删除操作
x2 = Delete(H1);//删除操作
y = x1 + x2;
sum += y;
Insert(H1, y);//插入操作
}
printf("%lld\n", sum);
}
return 0;
}
/***************************************************
User name: jk160630
Result: Accepted
Take time: 12ms
Take Memory: 172KB
Submit time: 2017-02-13 14:32:09
****************************************************/