dexter04's blog

By dexter04, history, 7 years ago, In English

following code does not work for input 1 10000. but it works when I use p as loop breaker why is that...

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define pll pair<long long,long long>

vector<pll>v;
int main()
{
    int n;
    cin>>n;
    ll a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n,greater<ll>());
    ll ans=0;

    for(int i=0;i<n-1;i++)
    {
        if(a[i]-a[i+1]<2)
        {
            v.push_back(pll(a[i],a[i+1]));
            i++;
        }
    }
    int p=v.size()-1;
    for(int i=0;i<v.size()-1;i+=2)
    {
        pll x=v[i],y=v[i+1];
        ans+=x.second*y.second;
    }
    cout<<ans;
}

Full text and comments »

  • Vote: I like it
  • -7
  • Vote: I do not like it