Problem p04021 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
struct A {
  int value, st, ed;
} a[10005];
bool cmp(A a, A b) { return a.value < b.value; }
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i].value);
      a[i].st = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; ++i)
      a[i].ed = i;
    int cnt = 0;
    // printf("**\n");
    for (int i = 1; i <= n; ++i)
      if (abs(a[i].st - a[i].ed) & 1)
        cnt++;
    // printf("%d %d %d\n",a[i].value,a[i].st,a[i].ed);
    printf("%d\n", cnt / 2);
  }
  return 0;
}

/*
    #include<bits/stdc++.h>
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef double db;
    typedef long double ldb;
    #define rep(i,a,b) for(i=a;i<b;i++)
    #define repu(i,a,b) for(i=a;i<=b;i++)
    #define repd(i,a,b) for(i=a;i>=b;i--)
    #define repc(i,a,b,c) for(i=a;i<b;i+=c)
    #define repcu(i,a,b,c) for(i=a;i<=b;i+=c)
    #define repcd(i,a,b,c) for(i=a;i>=b;i-=c)
    #define FIN freopen("input.in","r",stdin)
    #define FOUT freopen("output.out","w",stdout)
    #define CC(a) memset(a,0,sizeof(a))
    #define MST(a,b) memset(a,b,sizeof(a))
    #define PII pair<int,int>
    #define PLL pair<long long,long long>
    #define VI vector<int>
    #define VL vector<long long>
    #define VII vector<PII>
    #define VLL vector<PLL>
    #define all(x) x.begin(),x.end()
    #define mp make_pair
    #define pb push_back
    #define pf push_front
    #define fi first
    #define se second

    template<typename T>inline void gn(T &x){
        x=0;T f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
        x*=f;
    }
    template<typename T>inline void gt(T x){
        if(!x)return;
        gt(x/10);putchar(x%10+'0');
    }

    const LL N=100003;
    struct NODE{
        db o,r,d;
    };
    LL t,n,m,ans;
    LL MOD=1e9+7,inf=0x1f1f1f1f;
    db eps=1e-5;
    LL a[N];

    int main()
    {
        ios_base::sync_with_stdio(false);cin.tie(0);
        LL i,j,k,l,ca=0,u,v,d;
        cin>>n;
        repu(i,1,n)cin>>a[i];

        repu(i,1,n){
                ans+=a[i]/2,a[i]%=2;
                if(a[i]&&a[i+1]){
                        ans++;
                        a[i]=0;
                        a[i+1]-=1;
                }
        }
        cout<<ans<<endl;
        return 0;
    }


*/


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
struct A {
  int value, st, ed;
} a[100005];
bool cmp(A a, A b) { return a.value <= b.value; }
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i].value);
      a[i].st = i;
    }
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; ++i)
      a[i].ed = i;
    int cnt = 0;
    // printf("**\n");
    for (int i = 1; i <= n; ++i)
      if (abs(a[i].st - a[i].ed) & 1)
        cnt++;
    // printf("%d %d %d\n",a[i].value,a[i].st,a[i].ed);
    printf("%d\n", cnt / 2);
  }
  return 0;
}

/*
    #include<bits/stdc++.h>
    //#pragma comment(linker, "/STACK:102400000,102400000")
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef double db;
    typedef long double ldb;
    #define rep(i,a,b) for(i=a;i<b;i++)
    #define repu(i,a,b) for(i=a;i<=b;i++)
    #define repd(i,a,b) for(i=a;i>=b;i--)
    #define repc(i,a,b,c) for(i=a;i<b;i+=c)
    #define repcu(i,a,b,c) for(i=a;i<=b;i+=c)
    #define repcd(i,a,b,c) for(i=a;i>=b;i-=c)
    #define FIN freopen("input.in","r",stdin)
    #define FOUT freopen("output.out","w",stdout)
    #define CC(a) memset(a,0,sizeof(a))
    #define MST(a,b) memset(a,b,sizeof(a))
    #define PII pair<int,int>
    #define PLL pair<long long,long long>
    #define VI vector<int>
    #define VL vector<long long>
    #define VII vector<PII>
    #define VLL vector<PLL>
    #define all(x) x.begin(),x.end()
    #define mp make_pair
    #define pb push_back
    #define pf push_front
    #define fi first
    #define se second

    template<typename T>inline void gn(T &x){
        x=0;T f=1;char ch=getchar();
        while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
        while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}
        x*=f;
    }
    template<typename T>inline void gt(T x){
        if(!x)return;
        gt(x/10);putchar(x%10+'0');
    }

    const LL N=100003;
    struct NODE{
        db o,r,d;
    };
    LL t,n,m,ans;
    LL MOD=1e9+7,inf=0x1f1f1f1f;
    db eps=1e-5;
    LL a[N];

    int main()
    {
        ios_base::sync_with_stdio(false);cin.tie(0);
        LL i,j,k,l,ca=0,u,v,d;
        cin>>n;
        repu(i,1,n)cin>>a[i];

        repu(i,1,n){
                ans+=a[i]/2,a[i]%=2;
                if(a[i]&&a[i+1]){
                        ans++;
                        a[i]=0;
                        a[i+1]-=1;
                }
        }
        cout<<ans<<endl;
        return 0;
    }


*/