ACM周总结——贪心算法
一、做题思维训练
1.当题目限定条件多没有解题思路时,先去掉部分限定条件找思路;
2.完成以上代码后加入去掉的限定条件;
3.进行代码优化(更好更快更短)——用stl等。
注:在贪心算法中,多为先找好需排序的变量 进行排序,再根据题目要求完成继续的解题代码。
二、题目
Farmer John has received a noise complaint from his neighbor, Farmer Bob, stating that his cows are making too much noise. FJ’s N cows (1 <= N <= 10,000) all graze at various locations on a long one-dimensional pasture. The cows are very chatty animals. Every pair of cows simultaneously carries on a conversation (so every cow is simultaneously MOOing at all of the N-1 other cows). When cow i MOOs at cow j, the volume of this MOO must be equal to the distance between i and j, in order for j to be able to hear the MOO at all. Please help FJ compute the total volume of sound being generated by all N*(N-1) simultaneous MOOing sessions.
Input
Line 1: N
Lines 2…N+1: The location of each cow (in the range 0…1,000,000,000).
Output
There are five cows at locations 1, 5, 3, 2, and 4.
Sample Input
5 1 5 3 2 4
Sample Output
40
Hint
INPUT DETAILS:
There are five cows at locations 1, 5, 3, 2, and 4.
OUTPUT DETAILS:
Cow at 1 contributes 1+2+3+4=10, cow at 5 contributes 4+3+2+1=10, cow
at 3 contributes 2+1+1+2=6, cow at 2 contributes 1+1+2+3=7, and cow at
4 contributes 3+2+1+1=7. The total volume is (10+10+6+7+7) = 40.
1.提交compile error+runtime error错误解
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
cin>>n;
int x[n];
for(int i=0;i<n;i++)
{
cin>>x[i];
}
int t=0,l=0;
int i=0;
int j=0;
//应该都是对的
if(n%2==0)
{
for(i=0;i<n-1;i++)
{
for(j=0;j<n;j++)
{
t+=abs(x[j]-x[i]);
}
}
cout<<2*t;
}
else if(n%2!=0)
{
for(i=0;i<=(n-1)/2;i++)
{
for(j=0;j<n;j++)
{
t+=abs(x[j]-x[i]);
}
}
for(int i=0;i<0;i++)
{
l+=abs(x[((n-1)/2)+1]-x[i]);
}
cout<<2*t+l;
}
return 0;
}
2.解决
(1)原先的理解:可能存在cin读取速度慢、代码不够简洁且计算结果错误即解题思路错误的失误;
可采用c语言、在代码前加上 ios::sync_with_stdio(false);、更换数据类型、寻找更简洁的解题思路。
(2)runtime error (运行时错误)就是程序运行到一半,程序就崩溃了。
有以下几个原因:
①除以零
②数组越界:int a[3]; a[10000000]=10;
③指针越界:int * p; p=(int *)malloc(5 * sizeof(int)); *(p+1000000)=10;
④使用已经释放的空间:int * p; p=(int *)malloc(5 * sizeof(int));free§; *p=10;
⑤数组开得太大,超出了栈的范围,造成栈溢出:int a[100000000];一般来说,在oj上做题都把数组设成全局变量,减少5出现的可能。
具体链接:
https://blog.csdn.net/dreambyday/article/details/54880616
3.ac代码
(1)c语言实现
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int s[10005];
int main()
{
int n,i,j;
long long ans;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&s[i]);
sort(s,s+n);
ans=0;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
ans+=s[i]-s[j];
for(j=i+1;j<n;j++)
ans+=s[j]-s[i];
}
printf("%lld\n",ans);
return 0;
}
4.c++实现的错误代码:time limit exceeded
链接:
(1)错误原因https://blog.csdn.net/zhanshen112/article/details/83302382
(2)https://blog.csdn.net/qq_28301007/article/details/47363993
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int s[10005];
int main()
{ long long t;
ios::sync_with_stdio(false);
int i,j;
int n;
cin>>n;
for(i=0;i<n;i++)
{
cin>>s[i];
}
sort(s,s+n);
t=0;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
t+=s[i]-s[j];
}
for(j=i+1;j<n;j++)
{
t+=s[j]-s[i];
}
}
cout<<t<<endl;
return 0;
}
三、c语言编译过程中常遇到的错误个人总结
https://blog.csdn.net/m0_53164390/article/details/115021914