2021.3.19 The second week of summary accumulation-greedy algorithm (to be continued)

ACM Weekly Summary-Greedy Algorithm


1. Question thinking training 1. When there are many restrictions on the problem and no ideas for solving the problem, first remove some of the restrictions to find ideas;
2. After completing the above code, add the removed restrictions;
3. Perform code optimization (better, faster, and more Short)-use stl and so on.
Note: In the greedy algorithm, it is mostly to find the variables to be sorted for sorting, and then complete the problem-solving code according to the requirements of the problem.
2. Topic
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. Submit compile error+runtime error error solution

#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. Solution
(1) Original understanding: There may be errors such as slow reading of cin, insufficient code, and incorrect calculation results, that is, the wrong idea of ​​solving the problem;
C language can be used, and ios::sync_with_stdio(false) can be added before the code. ;, Change the data type, find a more concise problem-solving idea.
(2) Runtime error means that the program crashes when the program runs halfway.
There are several reasons:
①divide by zero
②array out of bounds: int a[3]; a[10000000]=10;
③ pointer out of bounds: int * p; p=(int *)malloc(5 * sizeof(int)) ; *(p+1000000)=10;
④Use the freed space: int * p; p=(int *)malloc(5 * sizeof(int));free§; *p=10;
⑤The array is too open large, beyond the scope of the stack, causing stack overflow: int a [100000000]; in general, do questions on oj are put into an array of global variables , reducing 5 may occur.
Specific link:
https://blog.csdn.net/dreambyday/article/details/54880616
3.ac code
(1) c language implementation

#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. Error code implemented by c++: time limit exceeded
Link:
(1) Reason for error 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;
}

3. Personal summary of errors frequently encountered during the compilation of c language
https://blog.csdn.net/m0_53164390/article/details/115021914

Guess you like

Origin blog.csdn.net/m0_53164390/article/details/115018393