在线评判Pashmak和Flowers给出了运行时错误

时间:2014-11-06 08:20:18

标签: c++

我正试图解决这个Pashmak和鲜花问题http://codeforces.com/problemset/problem/459/B

在线评判给我测试4的运行时错误?

有什么想法吗?

我尝试了很多测试用例..

感谢。

#include <iostream> 
#include <algorithm>
#include <string>

using namespace std;

int main()
{
int a[1000];
int n;
int minimum=0x3f3f3f3f; //if left without that number, the min number will never be saved in the        variable.. it will always have garbage
int maximum;

int big=0;
int small=0;
int count;


cin>>n;

for(int i=0; i<n; i++)
{
    cin>>a[i];  
    minimum=min(a[i],minimum);  
    maximum=max(a[i],maximum); 
}

for(int j=0; j<n; j++) //ex: input 3 1 2 3 1, big=2, small= 2, count= 2x2= 4
{
    if(a[j]==maximum)
    {big++;} 

    if(a[j]==minimum)
    {small++;}
}
count= big * small;

if(minimum==maximum)  //ex: input 1 1 1 1 1 , there are 10 probabilities = (5x4)/2
    count= n*(n-1)/2;  

cout<<maximum-minimum<<" "<<count<<endl; 
return 0;
}

1 个答案:

答案 0 :(得分:1)

问题表明2 <= n <= 2 * 10^5,但您的a数组只能容纳1000个整数!

因此,一旦cin >> a[1000];,您就会有一个超出范围的索引。