Pagini recente » Cod sursa (job #2523072) | Cod sursa (job #2362977) | Cod sursa (job #2364954) | Cod sursa (job #886225) | Cod sursa (job #703725)
Cod sursa(job #703725)
#include <stdio.h>
#include <vector>
using namespace std;
#define MAXN 500005
#define MAXDIFNR 600020
#define MAXNR 30000
vector<pair<int, int> > poz[MAXDIFNR];
int n, k, nrInterv = 1, nrAuxInterv = 1, indice;
pair<int, int> intervale[MAXN];
vector<int> subinterv[MAXN];
pair<int, int> interv;
bool intervBun;
void dfs(int nod)
{
if(subinterv[nod].empty() || subinterv[nod].front() > nrAuxInterv)
{
interv.first = intervale[nod].first + 1;
interv.second = intervale[nod].second - 1;
intervBun = true;
nrInterv++;
subinterv[nod].push_back(nrInterv);
intervale[nrInterv] = pair<int, int> (interv.first - 1, indice);
nrInterv++;
subinterv[nod].push_back(nrInterv);
intervale[nrInterv] = pair<int, int> (indice, interv.second + 1);
return;
}
vector<int>::iterator it;
for(it = subinterv[nod].begin(); it != subinterv[nod].end(); it++)
{
if(intervale[*it].second > indice)
{
if(intervale[*it].second - intervale[*it].first > k)
dfs(*it);
else
return;
}
}
}
int nr[MAXN];
int main()
{
FILE *in = fopen("secventa.in", "r");
FILE *out = fopen("secventa.out", "w");
fscanf(in, "%i %i", &n, &k);
intervale[1] = pair<int, int>(0, n + 1);
int i, aux, j;
for(i = 1; i <= n; i++)
{
fscanf(in, "%i", &aux);
poz[aux + MAXNR].push_back(pair<int,int>(i, -1));
}
vector<pair<int, int> >::iterator it;
int intervMax;
int maxim, st, dr, piv;
vector<int> used;
used.push_back(0);
used.push_back(n + 1);
int n1, size = 2;
for(i = 0; i < MAXDIFNR; i++)
{
if(!poz[i].empty())
{
for(it = poz[i].begin(); it != poz[i].end(); it++)
{
//binary search:::
st = 0;
dr = size - 1;
while(st < dr - 1)
{
piv = (st + dr) / 2;
if(used[piv] < it->first)
st = piv;
else
dr = piv;
}
it->second = st + 1;
if(maxim != i && used[st + 1] - used[st] > k)
{
intervMax = used[st];
maxim = i;
}
/*intervBun = false;
indice = *it;
dfs(1);
if(intervBun == true && gasit == false)
{
gasit = true;
intervMax.first = interv.first;
intervMax.second = interv.second;
maxim = i;
}*/
}
n1 = poz[i].size();
for(it = poz[i].begin(); it != poz[i].end(); it++)
used.insert(used.begin() + it->second, it->first);
size += n1;
}
}
fprintf(out, "%i %i %i\n", intervMax + 1, intervMax + k, maxim - MAXNR);
}