(Jizhong) 2192. Sum

(File IO): input: sum.in output: sum.out
time limit: 1000 ms space limitations: 65536 KB specific restrictions
Goto ProblemSet


Title description
given n n number a 1.. a n a1..an , the absolute value of the minimum sum of two numbers
i.e. seek a i + a j | ai + aj | ( i i is not equal to j j ) minimum


Enter
a number of the first row n n
the next line n n number a 1.. a n a1..an

Output
line a number a n s years minimum value, the absolute value of the sum of two numbers


Sample input
. 5
-2. 6. 7. 7 -8

Sample output
1


Data range limit
• For 40 40 % of the data, n < = 1 0 3 1 0 6 < = a i < = 1 0 6 n <= 10^3,-10^6 <= ai <= 10^6 .
• For 80 80 % of the data, n-$ <= 10. 5, -10. 6 <AI = <= 10. 6 ^.
• For 100 100 % of the data, n < = 1 0 6 1 0 6 < = a i < = 1 0 6 n <= 10^6,-10^6 <= ai <= 10^6


Solving ideas
first with absolute value of each number sequence as a row key, and then every two adjacent numbers together and seek a minimum.


Code

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int n,a[1000000],ans;
bool cmp(int x,int y)
{
	return abs(x)<abs(y);
}
int main()
{
	freopen("sum.in","r",stdin);
    freopen("sum.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1,cmp);
    ans=2147483647;
    for(int i=2;i<=n;i++)
    {
    	if(abs(a[i]+a[i-1])<ans)
    	ans=abs(a[i]+a[i-1]);
	}
    printf("%d",ans);
}
Published 119 original articles · won praise 8 · views 4929

Guess you like

Origin blog.csdn.net/kejin2019/article/details/104694483
sum