(File IO): input: sum.in output: sum.out
time limit: 1000 ms space limitations: 65536 KB specific restrictions
Goto ProblemSet
Title description
given
number
, the absolute value of the minimum sum of two numbers
i.e. seek
(
is not equal to
) minimum
Enter
a number of the first row
the next line
number
Output
line a number
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
% of the data,
.
• For
% of the data, n-$ <= 10. 5, -10. 6 <AI = <= 10. 6 ^.
• For
% of the data,
。
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);
}