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
Output
Sample Input
3 8 5 8
Sample Output
34
Hint
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int n; 8 int toDo[40002]; 9 10 int cmp(const void *a, const void *b) { 11 int at = *(int *)a; 12 int bt = *(int *)b; 13 return at - bt; 14 } 15 int main(int argc, char const *argv[]) 16 { 17 //freopen("input.txt","r",stdin); 18 while(scanf("%d",&n) != EOF) { 19 for(int i = 0; i < n; i++) { 20 scanf("%d",&toDo[i]); 21 } 22 int sum = 0; 23 for(int i = 0,j=0; j < n-1; i+= 2,j++) { 24 qsort(&toDo[i], n+j-i, sizeof(int), cmp); 25 toDo[n+j] = toDo[i] + toDo[i+1]; 26 sum = sum + toDo[n+j]; 27 } 28 29 printf("%d\n",sum); 30 } 31 return 0; 32 }
结果超时了
只好改进,每回不进行排序,只去找最小值和次小值,代码如下
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 int n; 8 int toDo[40002]; 9 10 int cmp(const void *a, const void *b) { 11 int at = *(int *)a; 12 int bt = *(int *)b; 13 return at - bt; 14 } 15 16 void swap(int a, int b) { 17 int temp = toDo[a]; 18 toDo[a] = toDo[b]; 19 toDo[b] = toDo[a]; 20 } 21 int main(int argc, char const *argv[]) 22 { 23 //freopen("input.txt","r",stdin); 24 while(scanf("%d",&n) != EOF) { 25 for(int i = 0; i < n; i++) { 26 scanf("%d",&toDo[i]); 27 } 28 int sum = 0; 29 for(int i = 0,j=n; j < 2*n-1; i+= 2,j++) { 30 int mini = i, minil = i+1; 31 if(toDo[minil] < toDo[mini]) { 32 mini = i + 1, minil = i; 33 } 34 for(int p = i+2; p < j; p++) { 35 if(toDo[p] < toDo[mini]) { 36 minil = mini; 37 mini = p; 38 } 39 else if(toDo[p] < toDo[minil]) { 40 minil = p; 41 } 42 } 43 swap(mini, i); 44 swap(minil, i+1); 45 toDo[j] = toDo[i] + toDo[i+1]; 46 sum = sum + toDo[j]; 47 } 48 49 printf("%d\n",sum); 50 } 51 return 0; 52 }
提交,错误,怀疑是溢出了
改成如下代码
#include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; int n; ll toDo[40002]; int cmp(const void *a, const void *b) { int at = *(int *)a; int bt = *(int *)b; return at - bt; } void swap(int a, int b) { ll temp = toDo[a]; toDo[a] = toDo[b]; toDo[b] = temp; } int main(int argc, char const *argv[]) { //freopen("input.txt","r",stdin); while(scanf("%d",&n) != EOF) { for(int i = 0; i < n; i++) { scanf("%lld",&toDo[i]); } ll sum = 0; for(int i = 0,j=n; j < 2*n-1; i+= 2,j++) { int mini = i, minil = i+1; if(toDo[minil] < toDo[mini]) { mini = i + 1, minil = i; } for(int p = i+2; p < j; p++) { if(toDo[p] < toDo[mini]) { minil = mini; mini = p; } else if(toDo[p] < toDo[minil]) { minil = p; } } swap(mini, i); swap(minil, i+1); toDo[j] = toDo[i] + toDo[i+1]; sum = sum + toDo[j]; } printf("%lld\n",sum); } return 0; }
结果依然错误
到底是哪里错了,一开始我百思不得其解
于是修改为如下代码
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long ll; 9 int n; 10 ll toDo[400002]; 11 12 void swap(int a, int b) { 13 ll temp = toDo[a]; 14 toDo[a] = toDo[b]; 15 toDo[b] = temp; 16 } 17 int main(int argc, char const *argv[]) 18 { 19 //freopen("input.txt","r",stdin); 20 while(scanf("%d",&n) != EOF) { 21 for(int i = 0; i < n; i++) { 22 scanf("%lld",&toDo[i]); 23 } 24 ll sum = 0; 25 for(int i = 0,j=n; j < 2*n-1; i+= 2,j++) { 26 if(toDo[i+1] < toDo[i]) { 27 swap(i,i+1); 28 } 29 for(int p = i+2; p < j; p++) { 30 if(toDo[p] < toDo[i]) { 31 swap(i, p); 32 swap(i+1, p); 33 } 34 else if(toDo[p] < toDo[i+1]) { 35 swap(i+1, p); 36 } 37 } 38 toDo[j] = toDo[i] + toDo[i+1]; 39 sum = sum + toDo[j]; 40 } 41 printf("%lld\n",sum); 42 } 43 return 0; 44 }
通过了,说明思路没有问题
又回过头去看原来的代码,断定是
swap(mini, i); swap(minil, i+1);
这两句的问题。
仔细思考,如果minil = i,执行完第一个交换后,minil指向的就不是次小值了,终于找到错误。
更正后的代码如下
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long ll; 9 int n; 10 ll toDo[40002]; 11 12 int cmp(const void *a, const void *b) { 13 int at = *(int *)a; 14 int bt = *(int *)b; 15 return at - bt; 16 } 17 18 void swap(int a, int b) { 19 ll temp = toDo[a]; 20 toDo[a] = toDo[b]; 21 toDo[b] = temp; 22 } 23 int main(int argc, char const *argv[]) 24 { 25 //freopen("input.txt","r",stdin); 26 while(scanf("%d",&n) != EOF) { 27 for(int i = 0; i < n; i++) { 28 scanf("%lld",&toDo[i]); 29 } 30 ll sum = 0; 31 for(int i = 0,j=n; j < 2*n-1; i+= 2,j++) { 32 int mini = i, minil = i+1; 33 if(toDo[minil] < toDo[mini]) { 34 mini = i + 1, minil = i; 35 } 36 for(int p = i+2; p < j; p++) { 37 if(toDo[p] < toDo[mini]) { 38 minil = mini; 39 mini = p; 40 } 41 else if(toDo[p] < toDo[minil]) { 42 minil = p; 43 } 44 } 45 if(minil == i) { 46 swap(minil,i+2); 47 minil = i + 2; 48 } 49 swap(mini, i); 50 swap(minil, i+1); 51 toDo[j] = toDo[i] + toDo[i+1]; 52 sum = sum + toDo[j]; 53 } 54 printf("%lld\n",sum); 55 } 56 return 0; 57 }
另外一种写法是这样
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long ll; 9 int n; 10 ll toDo[400002]; 11 12 void swap(int a, int b) { 13 ll temp = toDo[a]; 14 toDo[a] = toDo[b]; 15 toDo[b] = temp; 16 } 17 int main(int argc, char const *argv[]) 18 { 19 //freopen("input.txt","r",stdin); 20 while(scanf("%d",&n) != EOF) { 21 for(int i = 0; i < n; i++) { 22 scanf("%lld",&toDo[i]); 23 } 24 ll ans = 0; 25 while(n > 1) { 26 int mii1 = 0, mii2 = 1; 27 28 for(int i = 2; i < n; i++) { 29 if(toDo[i] < toDo[mii1]) { 30 mii2 = mii1; 31 mii1 = i; 32 } 33 else if(toDo[i] < toDo[mii2]) { 34 mii2 = i; 35 } 36 } 37 int t = toDo[mii1] + toDo[mii2]; 38 ans += t; 39 if(mii1 == n-1) { 40 swap(mii1, mii2); 41 } 42 toDo[mii1] = t; 43 toDo[mii2] = toDo[n-1]; 44 n--; 45 } 46 printf("%lld\n",ans); 47 } 48 return 0; 49 }
提交依然错误
怎么会,一检查,第26行,i未必就是指向最小值
修改后代码如下
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 using namespace std; 7 8 typedef long long ll; 9 int n; 10 ll toDo[200002]; 11 12 void swap(int a, int b) { 13 ll temp = toDo[a]; 14 toDo[a] = toDo[b]; 15 toDo[b] = temp; 16 } 17 int main(int argc, char const *argv[]) 18 { 19 //freopen("input.txt","r",stdin); 20 while(scanf("%d",&n) != EOF) { 21 for(int i = 0; i < n; i++) { 22 scanf("%lld",&toDo[i]); 23 } 24 ll ans = 0; 25 while(n > 1) { 26 int mii1 = 0, mii2 = 1; 27 if(toDo[mii2] < toDo[mii1]) { 28 swap(mii1,mii2); 29 } 30 for(int i = 2; i < n; i++) { 31 if(toDo[i] < toDo[mii1]) { 32 mii2 = mii1; 33 mii1 = i; 34 } 35 else if(toDo[i] < toDo[mii2]) { 36 mii2 = i; 37 } 38 } 39 int t = toDo[mii1] + toDo[mii2]; 40 ans += t; 41 if(mii1 == n-1) { 42 swap(mii1, mii2); 43 } 44 toDo[mii1] = t; 45 toDo[mii2] = toDo[n-1]; 46 n--; 47 } 48 printf("%lld\n",ans); 49 } 50 return 0; 51 }
如果知道小根堆,用优先队列求解的话则会更快
代码如下
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 using namespace std; 9 10 typedef long long ll; 11 int n; 12 priority_queue <ll, vector<ll>, greater<ll> > que; 13 14 int main(int argc, char const *argv[]) 15 { 16 //freopen("input.txt","r",stdin); 17 while(scanf("%d",&n) != EOF) { 18 while(!que.empty()) { 19 que.pop(); 20 } 21 for(int i = 0; i < n; i++) { 22 ll tmp; 23 scanf("%lld",&tmp); 24 que.push(tmp); 25 } 26 ll sum = 0; 27 while(que.size() > 1) { 28 ll l1, l2; 29 l1 = que.top(); 30 que.pop(); 31 l2 = que.top(); 32 que.pop(); 33 sum = sum + l1 + l2; 34 que.push(l1+l2); 35 } 36 printf("%lld\n",sum); 37 } 38 return 0; 39 }
这道题真实对它又爱又恨那!
原文:http://www.cnblogs.com/jasonJie/p/5791602.html