Pagini recente » Cod sursa (job #749723) | Cod sursa (job #2478910) | Cod sursa (job #701579) | Cod sursa (job #2508279) | Cod sursa (job #196923)
Cod sursa(job #196923)
#include <stdio.h>
#include <vector>
using namespace std;
#define NMAX 1000010
int N, nr = 0;
int a[NMAX];
vector <int> poz[NMAX];
int tata[NMAX];
int father(int x)
{
if (tata[x] == x) return x;
return tata[x] = father(tata[x]);
}
void unite(int x, int y)
{
if (!tata[x] || !tata[y]) return;
x = father(x); y = father(y);
if (x == y) return;
nr--;
if (rand() & 1) tata[x] = y;
else tata[y] = x;
}
int main()
{
int i;
freopen("operatii.in", "r", stdin);
freopen("operatii.out", "w", stdout);
scanf("%d", &N);
for (i = 1; i <= N; i++) {
scanf("%d", &a[i]);
poz[a[i]].push_back(i);
}
long long rez = 0;
for (i = 100000; i >= 1; i--) {
for (vector <int> :: iterator it = poz[i].begin(); it != poz[i].end(); ++it) {
tata[*it] = *it; nr++;
unite(*it, *it + 1);
unite(*it, *it - 1);
}
rez += nr;
}
printf("%lld\n", rez);
return 0;
}