Pagini recente » Cod sursa (job #2439619) | Cod sursa (job #1966458) | Cod sursa (job #3240797) | Cod sursa (job #2613822) | Cod sursa (job #2910286)
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX_N = 2 * 1e3;
const long long MOD = (1LL << 32);
pair<int, int> a[MAX_N + 1];
unsigned int dp[MAX_N + 1], ind[MAX_N + 1], sum[MAX_N + 1];
unsigned int aib[MAX_N + 1][MAX_N + 1];
int n;
void update(long long x, long long y, long long val) {
for (int i = y; i <= MAX_N; i += i & -i) {
aib[x][i] = (aib[x][i] + val) % MOD;
}
}
long long query(long long x, long long y) {
long long answer = 0;
for (int i = y; i >= 1; i -= i & -i) {
answer = (answer + aib[x][i]) % MOD;
}
return answer;
}
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
signed main() {
InParser fin("psir.in");
ofstream fout("psir.out");
fin >> n;
for (int i = 1; i <= n; i++) {
fin >> a[i].first;
a[i].second = i;
}
sort(a + 1, a + n + 1);
int cnt = 2;
ind[a[1].second] = 2;
for (int i = 2; i <= n; i++) {
if (a[i].first != a[i - 1].first) {
cnt++;
}
ind[a[i].second] = cnt;
}
for (int i = 1; i <= n; i++) {
memset(dp, 0, sizeof(dp));
for (int j = 1; j < i; j++) {
dp[ind[j]]++;
}
for (int j = 2; j < i; j++) {
if (ind[j] > ind[i]) {
long long tmp = query(j, ind[i] - 1);
dp[ind[j]] = (dp[ind[j]] + tmp) % MOD;
} else if (ind[j] < ind[i]) {
long long tmp = (sum[j] - query(j, ind[i]) + MOD) % MOD;
dp[ind[j]] = (dp[ind[j]] + tmp) % MOD;
}
}
for (int j = 1; j <= n + 1; j++) {
if (dp[j] > 0) {
update(i, j, dp[j]);
}
sum[i] = (sum[i] + dp[j]) % MOD;
}
}
long long answer = 0;
for (int i = 1; i <= n; i++) {
answer = (answer + sum[i]) % MOD;
}
fout << answer;
return 0;
}