Cod sursa(job #2397079)

Utilizator TheSeekerRobert Cristian Dobra TheSeeker Data 4 aprilie 2019 10:10:22
Problema Oite Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <fstream>
#include <vector>
#define DIM 1000017
using namespace std;

ifstream fin ("oite.in");
ofstream fout("oite.out");

int n,i,j,l,a[1030],aux,ind,k,ok,p;
vector< pair< int, int > > v[DIM];

int main(){
    fin>>n>>l;
    for (i=1;i<=n;i++)
        fin>>a[i];
    for (i=0;i<DIM;i++)
        v[i].push_back({0,0});
    for (i=2;i<=n-1;i++){
        for (j=i+1;j<=n;j++){
            aux=a[i]+a[j];
            ind=aux%DIM;
            if (v[ind][0].first){
                for (ok=0,p=0;v[ind][p].first!=0 && !ok;p++)
                    if (v[ind][p].first==aux)
                        ok=v[ind][p].second;
                k+=ok;
            }
        }
        for (j=1;j<i;j++){
            aux=a[i]+a[j];
            ind=aux%DIM;
            if (!v[ind][0].first){
                v[ind][0].first=aux;
                v[ind][0].second=1;
                v[ind].push_back({0,0});
            }
            else{
                p=0;ok=1;
                while (!v[ind][p].first && ok){
                    if (v[ind][p].first==aux){
                        v[ind][p].second++;
                        ok=0;
                    }
                    p++;
                }
                if (ok){
                    v[ind][p].first=aux;
                    v[ind][p].second=1;
                    v[ind].push_back({0,0});
                }
            }
        }
    }
    fout<<k/2;
    fin.close();
    fout.close();
    return 0;
}